通过代码或者配置文件 对log4net进行配置
1.通过代码进行配置
1.1代码
public class Logger { public static void Setup() { Hierarchy hierarchy = (Hierarchy) LogManager.GetRepository(); PatternLayout patternLayout = new PatternLayout(); patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"; patternLayout.ActivateOptions(); RollingFileAppender roller = new RollingFileAppender(); roller.AppendToFile = false; roller.File = @"Logs\EventLog.txt"; roller.Layout = patternLayout; roller.MaxSizeRollBackups = 5; roller.MaximumFileSize = "1GB"; roller.RollingStyle = RollingFileAppender.RollingMode.Size; roller.StaticLogFileName = true; roller.ActivateOptions(); hierarchy.Root.AddAppender(roller); MemoryAppender memory = new MemoryAppender(); memory.ActivateOptions(); hierarchy.Root.AddAppender(memory); hierarchy.Root.Level = Level.Info; hierarchy.Configured = true; } }
1.2代码调用
1.2.1 在程序启动的时候,调用 Logger.Setup();
1.2.2 输出日志
private static void Application_ThreadException(object sender,ThreadExceptionEventArgs e) { var logger = LogManager.GetLogger(sender.GetType()); logger.Error("Error", e.Exception); MessageBox.Show(e.Exception.Message); }
2.通过配置文件进行配置
2.1配置文件内容
<configSections> <section name ="log4net" type ="System.Configuration.IgnoreSectionHandler"/> </configSections> <log4net> <appender name="errorAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="ERROR" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <File value="Logs\err.log" /> <PreserveLogFileNameExtension value="true" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <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" /> <File value="Logs\info.log" /> <PreserveLogFileNameExtension value="true" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <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"> <levelToMatch value="DEBUG" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <File value="Logs\debug.log" /> <PreserveLogFileNameExtension value="true" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <appender name="perfAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="INFO" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <File value="Logs\perf.log" /> <PreserveLogFileNameExtension value="true" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %logger - %message%newline" /> </layout> </appender> <root> <level value="ALL" /> <appender-ref ref="errorAppender" /> <appender-ref ref="infoAppender" /> <appender-ref ref="debugAppender" /> </root> <logger name="Performance" additivity="false"> <level value="ALL" /> <appender-ref ref="perfAppender" /> </logger> </log4net>
2.2代码调用
http://www.cnblogs.com/chucklu/p/4359582.html
需要注意
XmlConfigurator.Configure();这个不带参数的用法,需要研究一下