Log4net - 多个appender

时间:2018-05-03 08:22:52

标签: c# log4net appender

我目前有两个appender,一个用于登录文件,另一个用于登录数据库。 ado net appender不工作,它没有记录表中的任何内容。然而,滚动文件appender工作得很好。它登录文件。如果我删除滚动文件appender,数据库中的日志记录工作正常。

这是我的app.config文件的一部分:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="common">
  <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
 </configSections>
<connectionStrings>
    <add name="Log4NetDBConnection" connectionString="Data Source=localhost;Initial Catalog=DataProcessing;UID=dpp-user;PWD=dpp-user" providerName="System.Data.SqlClient" />

 <common>
<logging>
  <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1211">
    <arg key="configType" value="INLINE" />
    </factoryAdapter>
   </logging>
   </common>
  <log4net configSource="App.log4net.config" />
    <dependentAssembly>
    <assemblyIdentity name="Common.Logging.Core" publicKeyToken="af08829b84f0328e" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.3.0.0" newVersion="3.3.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.3.0.0" newVersion="3.3.0.0" />
  </dependentAssembly>
</assemblyBinding>

  

这是我的App.Log4net.config文件:

<log4net>
<!-- Needed for default logger behavior to the log table-->
<root>
<level value="ERROR" />
<level value="WARN" />
<appender-ref ref="AdoNetAppender" />
</root>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection,System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionStringName value="DPPLog4NetDBConnection" />
<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="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>

<!-- <root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<level value="ALL" />
<file value="c:/logs/WorkflowEngineApp.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="%date [%thread] %level %logger - 
%message%newline%exception" />
</layout>
</appender> -->

</log4net>

正如您所看到的,我对滚动文件appender进行了注释,否则它只会记录一个文件。我究竟做错了什么?我可以一起使用这两个appender吗?

1 个答案:

答案 0 :(得分:0)

我怀疑你是在正确的轨道上,看起来两个appender需要在同一根中指定,请参阅下面的示例和链接:

<root>
<level value="ERROR" />
<level value="WARN" />
<appender-ref ref="AdoNetAppender" />
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>

File and Database multiple configuration for log4net