A sample configuration for nlog and SQL Server Compact 4.0

被刻印的时光 ゝ 提交于 2020-01-12 03:21:22

问题


I would be grateful if someone could post me a sample nlog.config for using nlog with SQL Server Compact 4.0.

I can output to the console and a file OK. I've tried various dbProviders and connectionStrings, but nothing seems to work.

Thanks in advance.

Alan T


回答1:


I figured it out. My test application is a console application written in C#. Below is the contents of the various files I used.

Program.cs

using NLog;

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

        static void Main(string[] args)
        {
            _logger.Debug( "A message" );
        }
    }
}

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>

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>

Database create command:

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

Hope this helps others trying to use nLog and SQL Server CE 4.0.



来源:https://stackoverflow.com/questions/6113467/a-sample-configuration-for-nlog-and-sql-server-compact-4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!