nlog和SQL Server Compact 4.0的示例配置

Ala*_*n T 10 nlog sql-server-ce

如果有人可以发布一个示例nlog.config以便在SQL Server Compact 4.0中使用nlog,我将不胜感激.

我可以输出到控制台和文件确定.我已经尝试了各种dbProviders和connectionStrings,但似乎没有任何工作.

提前致谢.

艾伦T.

Ala*_*n T 17

我想到了.我的测试应用程序是用C#编写的控制台应用程序.以下是我使用的各种文件的内容.

Program.cs中

using NLog;

namespace ConsoleApplication2
{
    class Program
    {
        private static readonly Logger _logger = LogManager.GetCurrentClassLogger( );

        static void Main(string[] args)
        {
            _logger.Debug( "A message" );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

App.config中

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.data>
      <DbProviderFactories>
        <remove invariant="System.Data.SqlServerCe.4.0" />
        <add name="Microsoft SQL Server Compact Data Provider 4.0" 
             invariant="System.Data.SqlServerCe.4.0" 
             description=".NET Framework Data Provider for Microsoft SQL Server Compact" 
             type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
      </DbProviderFactories>
  </system.data>
</configuration>
Run Code Online (Sandbox Code Playgroud)

NLog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >

  <targets>
    <!-- write log message to database -->
    <target xsi:type="Database" name="database">
      <!-- SQL command to be executed for each entry -->
      <commandText>INSERT INTO [LogEntries] (TimeStamp, Message, Level, Logger) VALUES(GETDATE(), @msg, @level, @logger)</commandText>

      <!-- parameters for the command -->
      <parameter name="@msg" layout="${message}" />
      <parameter name="@level" layout="${level}" />
      <parameter name="@logger" layout="${logger}" />

      <!-- connection string -->   
      <dbProvider>System.Data.SqlServerCe.4.0</dbProvider>
      <connectionString>Data Source=${basedir}\logger.sdf</connectionString>
    </target>
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="database" />
  </rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)

数据库创建命令:

CREATE TABLE LogEntries(
id int primary key not null identity(1,1),
TimeStamp datetime,
Message nvarchar(128),
level nvarchar(10),
logger nvarchar(128)) 
Run Code Online (Sandbox Code Playgroud)

希望这有助于其他人尝试使用nLog和SQL Server CE 4.0.