C# log4net日志
引用log4net.dll库
[log4net.dll库下载地址][1]
AssemblyInfo.cs
最后一行添加如下代码:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
log4net.config
注意把这个文件的属性”复制到输出目录”值改为”始终复制”, 不然会不生效
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name= "File" value= "D:\App_Log\servicelog\"/>
<param name= "AppendToFile" value= "true"/>
<param name= "MaxSizeRollBackups" value= "10"/>
<param name= "StaticLogFileName" value= "false"/>
<param name= "DatePattern" value= "yyyy-MM-dd".read.log""/>
<param name= "RollingStyle" value= "Date"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="[%date]-[%thread]-[%-p]-[%logger]-[%M] -> %message%newline" />
</layout>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="DEBUG" />
<foreColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="DEBUG" />
<foreColor value="Green" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="Info" />
<param name="LevelMax" value="Fatal" />
</filter>
</appender>
<root>
<level value="all" />
<appender-ref ref="ColoredConsoleAppender"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
</configuration>
应用
private static ILog log = LogManager.GetLogger(typeof(Program)); [STAThread] static void Main(string[] args) { log.Info("info"); log.Debug("Debug"); log.Error("Error"); }
https://segmentfault.com/a/1190000012601876