我正在尝试将错误消息和调试消息记录到两个不同的文件中。我正在使用两个不同的文件附加程序来执行此操作。
这是我的 app.config 文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="DebugFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="DebugLog.log" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date %level %logger - %message %newline" />
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="DEBUG"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="ErrorFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="ErrorLog.log" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date %level %logger %type %exception %line %location - %message %newline" />
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="ERROR"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="DebugFileAppender" />
</root>
<logger name="ErrorFileAppender" >
<level value="ERROR"/>
<appender-ref ref="ErrorFileAppender"/>
</logger>
调试消息工作正常,但是当我更改程序以包含异常错误时,错误不会写入文件。
这是我用来捕获异常的代码:
catch (Exception e)
{
logger.Error(e);
}