如果只是关于 Log4Net,请尝试以下操作:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<log4net>
<appender name="MainAppender" type="log4net.Appender.ColoredConsoleAppender" >
<mapping>
<level value="ALL" />
<foreColor value="White" />
<backColor value="Red, HighIntensity" />
</mapping>
<filter type="log4net.Filter.LoggerMatchFilter">
<LoggerToMatch value="MainApplicationLog" />
</filter>
<filter type="log4net.Filter.DenyAllFilter">
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<appender name="ValueAppender" type="log4net.Appender.ColoredConsoleAppender" >
<mapping>
<level value="ALL" />
<foreColor value="White" />
<backColor value="Green, HighIntensity" />
</mapping>
<filter type="log4net.Filter.LoggerMatchFilter">
<LoggerToMatch value="ValuesLog" />
</filter>
<filter type="log4net.Filter.DenyAllFilter">
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="MainAppender" />
<appender-ref ref="ValueAppender" />
</root>
</log4net>
</configuration>
您可以使用自己的选择替换附加程序。为了完整起见,这是将进行日志记录的代码。
log4net.Config.XmlConfigurator.Configure();
ILog mainAppLog = LogManager.GetLogger("MainApplicationLog");
ILog valuesLog = LogManager.GetLogger("ValuesLog");
mainAppLog.Debug("Calculating values");
valuesLog.InfoFormat("{0}", 0); // do not use ToString, you will use processor time even when not logging