我已经查看了许多有关此问题的帖子,我似乎无法理解为什么我的代码无效。我正在登录到控制台和fileappender,但数据库日志永远不会工作。
我的app.config设置为:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/>
</configSections>
<!-- Log4net Logging Setup -->
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<threshold value="Debug" />
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="Info" />
<file value="C:\\Sample.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="250KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<threshold value="Error" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<connectionString value="Connection_String"/>
<commandText value="MyStoredProc" />
<commandType value="StoredProcedure" />
<parameter name="AgentId">
<parameterName value="@AgentId"/>
<dbType value="String"/>
<size value="6"/>
<layout type="log4net.Layout.PatternLayout" value="%property{AgentId}" />
</parameter>
<parameter name="customerId">
<parameterName value="@customerId"/>
<dbType value="String"/>
<size value="15"/>
<layout type="log4net.Layout.PatternLayout" value="%property{customerId}" />
</parameter>
<parameter>
<parameterName value="@log"/>
<dbType value="String"/>
<size value="4000"/>
<layout type="log4net.Layout.PatternLayout" value="%message"/>
</parameter>
<parameter>
<parameterName value="@ErrorDate"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}"/>
</parameter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="Console" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
C#代码是:
log4net.Config.BasicConfigurator.Configure();
log4net.ILog Log = log4net.LogManager.GetLogger(typeof(T));
log4net.LogicalThreadContext.Properties["AgentId"] = agentId;
log4net.LogicalThreadContext.Properties["customerId"] = custId;
Log.Error(String.Format("Non fatal error: {0} ({1})", ex.Message, ex.InnerException), ex);
答案 0 :(得分:0)
您应该使用log4net.Config.XmlConfigurator.Configure()方法而不是log4net.Config.BasicConfigurator.Configure();
XmlConfigurator将读取您的配置并使您能够按配置登录到数据库。 BasicConfigurator只会登录到您的控制台。