nlog和SQL Server Compact 4.0的示例配置 [英] A sample configuration for nlog and SQL Server Compact 4.0

查看:53
本文介绍了nlog和SQL Server Compact 4.0的示例配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

提前谢谢.

艾伦·T

推荐答案

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

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

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

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

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>

数据库创建命令:

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

希望这可以帮助其他尝试使用nLog和SQL Server CE 4.0的人.

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

这篇关于nlog和SQL Server Compact 4.0的示例配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆