这个东西实在是让人感动的想哭囊…………-_-。。。。。
Log4Net.config文件
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/> </configSections> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <!--站点日志配置部分--> <log4net> <root> <!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF--> <!--比如定义级别为INFO,则INFO级别向下的级别,比如DEBUG日志将不会被记录--> <!--如果没有定义LEVEL的值,则缺省为DEBUG--> <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="admin\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="admin\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="admin\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="admin\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="admin\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="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>
LogHelper.cs
using log4net; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)] namespace DBUtility { public class LogHelper { private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); public static void Debug(object ex) { Log.Debug(ex); } public static void Warn(object ex) { Log.Warn(ex); } public static void Error(object ex) { Log.Error(ex); } public static void Info(object ex) { Log.Info(ex); } public static void Debug(object message, Exception ex) { Log.Debug(message, ex); } public static void Warn(object message, Exception ex) { Log.Warn(message, ex); } public static void Error(object message, Exception ex) { Log.Error(message, ex); } public static void Info(object message, Exception ex) { Log.Info(message, ex); } } }