我正在尝试在 WCF 服务中使用 Log4net 登录到 SQL Server。我正在使用 Windows 身份验证并且与数据库的连接很好。下面是配置文件和代码。文件日志正确发生。
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net debug ="true">
<root>
<level value="ALL" />
<appender-ref ref="AdoNetAppender_SqlServer" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=V01KUMAVX\\SQLEXPRESS;initial catalog=dbLog;integrated security=SSPI;" />
<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>
</appender>
</log4net>
</configuration>
and the code :
public class Service1 : IService1
{
private static readonly log4net.ILog log;
static Service1()
{
log4net.Config.XmlConfigurator.Configure();
log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
public string GetData(int value)
{
log4net.Config.XmlConfigurator.Configure();
log.Debug(string.Format("You entered: {0}", value));
if (!log4net.LogManager.GetRepository().Configured)
{
// log4net not configured
foreach (log4net.Util.LogLog message in log4net.LogManager.GetRepository().ConfigurationMessages)
{
// evaluate configuration message
}
}
return string.Format("You entered: {0}", value);
}
}
如何记录 Log4net 静默错误以及如何查看它们?