asp.net log4net 使用教程
log4net 线上的教程已经很多,简单地方一句带过。
1.什么是 log4net.
log4net 是log4j在Microsoft .NET平台的一个扩展版。作用:将日志信息输出,毕竟当系统发布后我们不可能在用IDE去调试程序,但是作为一般的程序员都有处理异常信息的能力。
2.安装
安装的方式有两种
(1)引用dll文件;
(2)使用 nuget直接安装;
3.配置文件
配置文件部分,配置明细就不说了(炒鸡明细详解,有兴趣可以看完:http://www.cnblogs.com/QingLang-Raymond/articles/5599884.html),这里重点说的是怎么在web.config 里面把 log4net 的配置项独立出来,一般的系统发布后为了降低耦合,配置文件基本都是单独分开处理。
(1)安装或者引用成功后 新建一个单独的配置文件,命名 log4net.config ;
(2)拷贝 配置文件信息 进log4net.config,这样就达到了独立配置文件的目的;
4.使用
将以下的引用拷贝到你需要使用的文件下面,我这里对log4net的使用 用的是单独的一个类文件 log4help.cs ,所以拷贝到此文件的using里面即可,这里需要注意下ConfigFile = @"log4net.config" ,黄色部分是文件存放路径,如果你是放在文件夹里面请在前面写上ConfigFile = @"xxx/log4net.config".
[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"log4net.config", Watch = true)]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using log4net; [assembly: log4net.Config.XmlConfigurator(ConfigFile = @"log4net.config", Watch = true)] namespace Common { public class LogHelper { private ILog logger; public LogHelper(ILog log) { this.logger = log; } public void Info(object message) { this.logger.Info(message); } public void Info(object message, Exception e) { this.logger.Info(message, e); } public void Debug(object message) { this.logger.Debug(message); } public void Debug(object message, Exception e) { this.logger.Debug(message, e); } public void Warming(object message) { this.logger.Warn(message); } public void Warming(object message, Exception e) { this.logger.Warn(message, e); } public void Error(object message) { this.logger.Error(message); } public void Error(object message, Exception e) { this.logger.Error(message, e); } public void Fatal(object message) { this.logger.Fatal(message); } public void Fatal(object message, Exception e) { this.logger.Fatal(message, e); } } public class LogFactory { static LogFactory() { } public static LogHelper GetLogger(Type type) { return new LogHelper(LogManager.GetLogger(type)); } public static LogHelper GetLogger(string str) { return new LogHelper(LogManager.GetLogger(str)); } } }
5.配置文件信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <!--日志记录组建配置--> <!--log4net 写log--> <log4net> <root> <level value="all" /> <appender-ref ref="INFOAppender" /> <appender-ref ref="DEBUGAppender" /> <appender-ref ref="WARNAppender" /> <appender-ref ref="ERRORAppender" /> <appender-ref ref="FATALAppender" /> <appender-ref ref="ColoredConsole" /> <appender-ref ref="TraceAppender" /> </root> <appender name="INFOAppender" type="log4net.Appender.RollingFileAppender"> <file value="logs\\INFO\\" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" /> <staticLogFileName value="false"/> <maxSizeRollBackups value="100" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="INFO"/> <param name="LevelMax" value="INFO"/> </filter> </appender> <appender name="DEBUGAppender" type="log4net.Appender.RollingFileAppender"> <file value="logs\\DEBUG\\" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" /> <staticLogFileName value="false"/> <maxSizeRollBackups value="100" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="DEBUG"/> <param name="LevelMax" value="DEBUG"/> </filter> </appender> <appender name="WARNAppender" type="log4net.Appender.RollingFileAppender"> <file value="logs\\WARN\\" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" /> <staticLogFileName value="false"/> <maxSizeRollBackups value="100" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="WARN"/> <param name="LevelMax" value="WARN"/> </filter> </appender> <appender name="ERRORAppender" type="log4net.Appender.RollingFileAppender"> <file value="logs\\ERROR\\" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" /> <staticLogFileName value="false"/> <maxSizeRollBackups value="100" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="ERROR"/> <param name="LevelMax" value="ERROR"/> </filter> </appender> <appender name="FATALAppender" type="log4net.Appender.RollingFileAppender"> <file value="logs\\FATAL\\" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" /> <staticLogFileName value="false"/> <maxSizeRollBackups value="100" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="FATAL"/> <param name="LevelMax" value="FATAL"/> </filter> </appender> <!--调试模式,输出日志到控制台--> <appender name="ColoredConsole" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR"/> <foreColor value="Red, HighIntensity"/> <!--<backColor value="Green" />--> </mapping> <mapping> <level value="DEBUG"/> <foreColor value="Yellow, HighIntensity"/> <backColor value="Green"/> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="TraceAppender" type="log4net.Appender.TraceAppender"> <layout type="log4net.Layout.PatternLayout"> <param name="conversionPattern" value="%newline*******************************************%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline*******************************************%newline" /> </layout> </appender> </log4net> </configuration>
====Bloger QQ:820412456===
=====转载请注明出处========