因公司同事写的log4net记录日志不是很到位,从日志看不出错误真正发生源,这里特写一个小demo显示log4net的用法。
log4net 帮助类,Log4netHelper
public class Log4netHelper { static Log4netHelper() { string log4netConfigSource = System.Configuration.ConfigurationManager.AppSettings["log4netconfig"]; string filename = ""; if (!string.IsNullOrEmpty(log4netConfigSource)) { filename = HttpRuntime.AppDomainAppPath + log4netConfigSource; log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(filename)); } else { throw new Exception("应用系统日志配置文件配置错误!"); } } /// <summary> /// 输出日志到Log4Net /// </summary> /// <param name="t"></param> /// <param name="ex"></param> public static void WriteErrorLog(Type t, Exception ex) { log4net.ILog log = log4net.LogManager.GetLogger(t); log.Error("Error", ex); } /// <summary> /// 输出日志到Log4Net /// </summary> /// <param name="t"></param> /// <param name="msg"></param> public static void WriteInfoLog(Type t, string msg) { log4net.ILog log = log4net.LogManager.GetLogger(t); log.Info(msg); } }
log4net 配置:
<?xml version="1.0" encoding="utf-8" ?> <log4net> <!--定义输出到文件中--> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <!--定义文件存放位置--> <param name="File" value="Logs\" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyy-MM-dd.'log'" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout"> <!--每条日志末尾的文字说明--> <!--<footer value="此条记录结束" />--> <!--输出格式--> <!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info--> <param name="ConversionPattern" value="%newline记录时间:[%date] 线程ID:[%thread] 日志级别:[%-5level] 出错类:【%logger property:(%property{NDC})] 错误描述:[%message]%newline" /> </layout> </appender> <!--定义输出到控制台命令行中--> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--定义输出到windows事件中--> <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--定义输出到数据库中,这里举例输出到Access数据库中,数据库为C盘的log4net.mdb--> <appender name="AdoNetAppender_Access" type="log4net.Appender.AdoNetAppender"> <connectionString value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:log4net.mdb" /> <commandText value="INSERT INTO LogDetails ([LogDate],[Thread],[Level],[Logger],[Message]) VALUES (@logDate, @thread, @logLevel, @logger,@message)" /> <!--定义各个参数--> <parameter> <parameterName value="@logDate" /> <dbType value="String" /> <size value="240" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date" /> </layout> </parameter> <parameter> <parameterName value="@thread" /> <dbType value="String" /> <size value="240" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%thread" /> </layout> </parameter> <parameter> <parameterName value="@logLevel" /> <dbType value="String" /> <size value="240" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%level" /> </layout> </parameter> <parameter> <parameterName value="@logger" /> <dbType value="String" /> <size value="240" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%logger" /> </layout> </parameter> <parameter> <parameterName value="@message" /> <dbType value="String" /> <size value="240" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message" /> </layout> </parameter> </appender> <!--定义日志的输出媒介,下面定义日志以四种方式输出。也可以下面的按照一种类型或其他类型输出。--> <root> <!--文件形式记录日志--> <appender-ref ref="RollingLogFileAppender" /> <!--控制台控制显示日志--> <!--<appender-ref ref="ConsoleAppender" /> --> <!--Windows事件日志--> <!--<appender-ref ref="EventLogAppender" />--> <!-- 如果不启用相应的日志记录,可以通过这种方式注释掉 <appender-ref ref="AdoNetAppender_Access" /> --> </root> </log4net>
测试结果:
源码下载: 点击这里。