我最近在这里的一个用户介绍下了log4net。无论如何,我希望将每个Console.Write()和Debug.Write()都自动记录到我配置的单个日志文件中。此外,我使用许多类库,它们也有与log4net无关的Console/Debug语句。这些是否可以根据配置自动记录到日志文件中?这些都可能吗?
我想在滚动日志文件中记录的内容:
Console.WriteLine("console statement");
Debug.WriteLine("debug statement");
实例化日志记录器:
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static void InitializeLogger()
{
XmlConfigurator.Configure();
}
整个 app.config 文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="SubToolsPortServerTest.log"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>