C#中使用Log4记录日志
Log4Net配置还是挺简单的,具体:
1、新建Web或者Webform项目,使用Nuget下载log4net
2、新建Config文件夹,将log4net配置文件放入此文件夹中(当修改web.config的时候会重启网站,所以把log4net单独出来比较好,也好管理),log4net配置:
<?xml version="1.0" encoding="utf-8" ?> <log4net> <appender name="fatalAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="FATAL" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <param name="File" value="Logs\\"/> <param name="AppendToFile" value="true"/> <param name="MaxSizeRollBackups" value="-1"/> <!--超过1024KB则自动新增文件--> <param name="MaximumFileSize" value="1024KB"/> <param name="RollingStyle" value="Composite"/> <param name="StaticLogFileName" value="false"/> <!--"不能少--> <param name="DatePattern" value=""fatal_"yyyyMMdd".txt""/> <param name="Encoding" value="utf-8"/> <!--为空默认情况下会独占日志文件,不能被File.Open,通过"log4net.Appender.FileAppender+MinimalLock"使用最小锁定模型以允许多个进程可以写入同一个文件--> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <appender name="errorAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="ERROR" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <param name="File" value="Logs\\"/> <param name="AppendToFile" value="true"/> <param name="MaxSizeRollBackups" value="-1"/> <!--超过1024KB则自动新增文件--> <param name="MaximumFileSize" value="1024KB"/> <param name="RollingStyle" value="Composite"/> <param name="StaticLogFileName" value="false"/> <!--"不能少--> <param name="DatePattern" value=""error_"yyyyMMdd".txt""/> <param name="Encoding" value="utf-8"/> <!--为空默认情况下会独占日志文件,不能被File.Open,通过"log4net.Appender.FileAppender+MinimalLock"使用最小锁定模型以允许多个进程可以写入同一个文件--> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <appender name="warnAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="WARN" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <param name="File" value="Logs\\"/> <param name="AppendToFile" value="true"/> <param name="MaxSizeRollBackups" value="-1"/> <!--超过1024KB则自动新增文件--> <param name="MaximumFileSize" value="1024KB"/> <param name="RollingStyle" value="Composite"/> <param name="StaticLogFileName" value="false"/> <!--"不能少--> <param name="DatePattern" value=""warn_"yyyyMMdd".txt""/> <param name="Encoding" value="utf-8"/> <!--为空默认情况下会独占日志文件,不能被File.Open,通过"log4net.Appender.FileAppender+MinimalLock"使用最小锁定模型以允许多个进程可以写入同一个文件--> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <appender name="infoAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="INFO" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <param name="File" value="Logs\\"/> <param name="AppendToFile" value="true"/> <param name="MaxSizeRollBackups" value="-1"/> <!--超过1024KB则自动新增文件--> <param name="MaximumFileSize" value="1024KB"/> <param name="RollingStyle" value="Composite"/> <param name="StaticLogFileName" value="false"/> <!--"不能少--> <param name="DatePattern" value=""info_"yyyyMMdd".txt""/> <param name="Encoding" value="utf-8"/> <!--为空默认情况下会独占日志文件,不能被File.Open,通过"log4net.Appender.FileAppender+MinimalLock"使用最小锁定模型以允许多个进程可以写入同一个文件--> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <appender name="debugAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <!--只有级别是DEBUG的日志才能输出--> <levelToMatch value="DEBUG" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <param name="File" value="Logs\\"/> <param name="AppendToFile" value="true"/> <param name="MaxSizeRollBackups" value="-1"/> <!--超过1024KB则自动新增文件--> <param name="MaximumFileSize" value="1024KB"/> <param name="RollingStyle" value="Composite"/> <param name="StaticLogFileName" value="false"/> <!--"不能少--> <param name="DatePattern" value=""debug_"yyyyMMdd".txt""/> <param name="Encoding" value="utf-8"/> <!--为空默认情况下会独占日志文件,不能被File.Open,通过"log4net.Appender.FileAppender+MinimalLock"使用最小锁定模型以允许多个进程可以写入同一个文件--> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <root> <level value="ALL" /> <appender-ref ref="fatalAppender" /> <appender-ref ref="errorAppender" /> <appender-ref ref="warnAppender" /> <appender-ref ref="infoAppender" /> <appender-ref ref="debugAppender" /> </root> </log4net>
3、右键点击第2步骤的log4net文件属性,改成如图所示
在app.config或者web.config中的configuration节点下加入:
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections>
4、在Properties下的AssemblyInfo.cs文件中加入
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Config/log4net.config", Watch = true)]
我写了一个log4帮助类方便使用,代码:
1 public static class Log4Helper 2 { 3 public static void Fatal(Type type, object message, Exception exception = null) 4 { 5 ILog log = LogManager.GetLogger(type); 6 if (exception == null) 7 log.Fatal(message); 8 else 9 log.Fatal(message, exception); 10 } 11 12 public static void Error(Type type, object message, Exception exception = null) 13 { 14 ILog log = LogManager.GetLogger(type); 15 if (exception == null) 16 log.Error(message); 17 else 18 log.Error(message, exception); 19 } 20 21 public static void Warn(Type type, object message, Exception exception = null) 22 { 23 ILog log = LogManager.GetLogger(type); 24 if (exception == null) 25 log.Warn(message); 26 else 27 log.Warn(message, exception); 28 } 29 30 public static void Info(Type type, object message, Exception exception = null) 31 { 32 ILog log = LogManager.GetLogger(type); 33 if (exception == null) 34 log.Info(message); 35 else 36 log.Info(message, exception); 37 } 38 39 public static void Debug(Type type, object message, Exception exception = null) 40 { 41 ILog log = LogManager.GetLogger(type); 42 if (exception == null) 43 log.Debug(message); 44 else 45 log.Debug(message, exception); 46 } 47 }
5、调用:
1 Log4Helper.Fatal(this.GetType(), "Fatal"); 2 Log4Helper.Error(this.GetType(), "Error", new Exception("error")); 3 Log4Helper.Warn(this.GetType(), "Warn"); 4 Log4Helper.Info(this.GetType(), "Info"); 5 Log4Helper.Debug(this.GetType(), "Debug");
在根目录会有一个Log文件夹,文件夹内容:
最后说下Log4Net跨项目使用:
如果我们将log4net帮助类放在另一个公共的类库项目中,又需要在另一个引用的项目中使用log4helper类,
这时候仅需要在该项目里的Properties下的AssemblyInfo.cs文件中加入这句:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Config/log4net.config", Watch = true)]