English 中文(简体)
将SQL Server Ce 4.0与log4net结合使用
原标题:Using SQL Server Ce 4.0 with log4net

我正在尝试将SQL Server Compact Edition(v4.0)与log4net一起使用。有一个很棒的指南此处。它使用Compact Edition 3.5,但大多数步骤应该相同。

因此,我在“App_Data”中创建了一个SQL Server CE 4.0数据库,在我的MVC web应用程序中有一个“Log”表,并确保System.Data.SqlServerCe“复制本地”是true。我还更改了连接字符串以说明app_data文件夹。

我的appender如下所示:

<appender name="SqlCeAppender" type="log4net.Appender.AdoNetAppender">
    <connectionType value="System.Data.SqlServerCe.SqlCeConnection, System.Data.SqlServerCe" />
    <connectionString value="Data Source=|DataDirectory|= log4net.sdf " />
    <commandText
    value="INSERT INTO Log 
      ([Date],[Thread],[Level],[Logger],[Message],
      [Exception], [UserName], [Custom]) 
      VALUES 
      (@log_date, @thread, @log_level, @logger, @message, 
      @exception, @username, @custom)" />
    <parameter>
      <parameterName value="@log_date" />
      <dbType value="DateTime" />
      <layout type="log4net.Layout.RawUtcTimeStampLayout" />
    </parameter>
    <parameter>
      <parameterName value="@thread" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%thread" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@log_level" />
      <dbType value="String" />
      <size value="50" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@logger" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%logger" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@message" />
      <dbType value="String" />
      <size value="4000" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@exception" />
      <dbType value="String" />
      <size value="2000" />
      <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
    <parameter>
      <parameterName value="@username" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%username" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@custom" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{Custom}" />
      </layout>
    </parameter>
  </appender>

和什么也没发生——没有日志。当我切换到我的文本文件appender时,我会得到日志,所以这与这个appender有关——但我不知道是什么。有人能帮忙吗?

最佳回答

好吧,我最终放弃了这一点,并决定通过编写一个自定义的Appender来完成,该Appender使用实体框架来编写日志。实际上,这对我来说更好,因为我可以在自定义的Appander中添加更多关于错误的信息。

问题回答

我相信你遇到的问题是bufferSize没有设置。将bufferSize选项设置为如下所示:

<bufferSize value="1" />

把它放在你的阑尾里,你就可以走了。

事实上,我也遇到过你同样的问题,但还是设法解决了。

我在你的例子中发现了几个问题:

<connectionString value="Data Source=|DataDirectory|= log4net.sdf " />

必须更改为:

<connectionString value="Data Source=|DataDirectory|log4net.sdf" />

我还删除了那些不在默认log4net表模式中的自定义字段(用户名和自定义),因此查询变成:

INSERT INTO Log 
      ([Date],[Thread],[Level],[Logger],[Message],
      [Exception]) 
      VALUES 
      (@log_date, @thread, @log_level, @logger, @message, 
      @exception)

当然,我删除了2个相关的<;参数>块。

我还添加了<;缓冲器大小>节点,如@BiggsTRC所建议。

然后一切都开始运转了。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...