日期:2014-05-17 浏览次数:20957 次
C#中异常的记录也有一个模板,就是log4net。多的就不说了直接看怎么用的吧。
1、引用log4net.dll。
2、实现log4net的方法。
public class LogImplement { private ILog logger; public LogImplement(ILog log) { this.logger = log; } 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 Info(object message) { this.logger.Info(message); } public void Info(object message, Exception e) { this.logger.Info(message, e); } } public class LogFactory { static LogFactory() { FileInfo configFile = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + @"Log.config"); log4net.Config.XmlConfigurator.Configure(configFile); } public static LogImplement GetLogger(Type type) { return new LogImplement(LogManager.GetLogger(type)); } public static LogImplement GetLogger(string str) { return new LogImplement(LogManager.GetLogger(str)); } }
3、配置文件
<configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-2.0"/> </configSections> <log4net> <root> <appender-ref ref="ErrorLogFileAppender"/> <appender-ref ref="WarningFileAppender"/> <appender-ref ref="DebugLogFileAppender"/> </root> <!--记录warning--> <appender name="WarningFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log\Warn.log"/> <appendToFile value="true"/> <maxSizeRollBackups value="10"/> <maximumFileSize value="1MB"/> <rollingStyle value="Size"/> <staticLogFileName value="true"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%date] [%-5level] [%logger] - %message%newline"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="WARN"/> <param name="LevelMax" value="WARN"/> </filter> </appender> <!--记录错误--> <appender name="ErrorLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log\Error.log"/> <appendToFile value="true"/> <maxSizeRollBackups value="10"/> <maximumFileSize value="1MB"/> <rollingStyle value="Size"/> <staticLogFileName value="true"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%date] [%-5level] [%logger] - %message%newline"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="ERROR"/> <param name="LevelMax" value="ERROR&