配置log4net以在数据库上写入

时间:2015-01-19 12:06:26

标签: c# log4net log4net-configuration

我正在尝试配置log4net来登录数据库,它已经配置为写入文件,但是当我将appender更改为数据库时,它没有做任何事情。与数据库的连接是可以的,因为如果我更改密码,我可以在事件查看器中看到一个条目,说密码错误。 这是我的app.config:

 <log4net>   
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra Info: %property{testProperty}%newline%exception"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO"/>
        <levelMax value="FATAL"/>
      </filter>
    </appender>    
    <securitycontext type="log4net.Util.WindowsSecurityContext">
      <credentials value="Process">
      </credentials>
    </securitycontext>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="100" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="data source=PCKMA0348\SQLEXPRESS;initial catalog=dbServicesLog;User ID='rb';Password='Test1234';multipleactiveresultsets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
      <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </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="2000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="1000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
    </appender>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="mylogfile.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="FATAL"/>
      <appender-ref ref="AdoNetAppender"/>
    </root>
    <logger name="Log4NetTest.OtherClass">
      <level value="DEBUG"/>
      <appender-ref ref="AdoNetAppender"/>
    </logger>
  </log4net>

有什么猜测? 我是否需要手动创建数据库和表格,否则将自动创建?

1 个答案:

答案 0 :(得分:2)

您需要自己创建数据库和日志表。

如果要诊断log4net可能存在的问题,可以通过将其添加到.config文件来创建诊断跟踪:

<system.diagnostics>
  <trace autoflush="true">
    <listeners>
      <add
        name="textWriterTraceListener"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="C:\whatever\your\path\is\log4net.txt" />
    </listeners>
  </trace>
</system.diagnostics>

并添加调试开关:

<appSettings>
  <add key="log4net.Internal.Debug" value="true"/>
</appSettings>