(一). WinForm 或者 WPF 中的配置和应用
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandle,log4net"/> </configSections> <log4net> <root> <level value="INFO " /> <!-- 日志等级 OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL --> <appender-ref ref="LogFileAppender" /> </root> <!--<logger name="WebLogger"> <level value="WARN"/> <appender-ref ref="LogFileAppender" /> </logger>--> <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" > <param name="File" value="D:Log/"/> <param name="AppendToFile" value="true" /> <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyy-MM-dd".txt"" /> <!--<file value="./log.txt" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <maxSizeRollBackups value="-1" /> <maximumFileSize value="1MB" /> <staticLogFileName value="false" /> <DatePattern value="yyyy-MM-dd".txt""/>--> <!--<datePattern value="yyyyMMdd-HH:mm:ss" />--> <layout type="log4net.Layout.PatternLayout"> <!--每条日志开始的文字说明--> <param name="Header" value="[Header]\r\n" /> <!--每条日志末尾的文字说明--> <param name="Footer" value="[Footer]\r\n"/> <!--输出格式--> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> <!--<conversionPattern value="%date %-5level - %message%newline" />--> </layout> </appender> </log4net> </configuration>
var path = AppDomain.CurrentDomain.BaseDirectory + @"\log4net_config.xml";//获取配置文件路径 //var path = AppDomain.CurrentDomain.BaseDirectory + @"\log4netConfig.xml"; log4net.Config.XmlConfigurator.Configure(new FileInfo(path)); log4net.ILog log = log4net.LogManager.GetLogger(this.GetType()); if (log.IsWarnEnabled) log.Warn("message");
(二).MVC 4 应用
<configSections> <!--Lo4Net 配置--> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandle,log4net"/> </configSections> <log4net> <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL --> <!-- Set root logger level to ERROR and its appenders --> <root> <level value="ERROR"/> <appender-ref ref="SysAppender"/> </root> <!-- Print only messages of level DEBUG or above in the packages --> <!--<logger name="WebLogger"> <level value="DEBUG"/> </logger>--> <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" > <file value="App_Data/Log/" /> <!-- 一定注意路径 --> <param name="AppendToFile" value="true" /> <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <param name="RollingStyle" value="Date" /> <!--<file value="./log.txt" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <maxSizeRollBackups value="-1" /> <maximumFileSize value="1MB" /> <staticLogFileName value="false" /> <DatePattern value="yyyy-MM-dd".txt""/>--> <!--<datePattern value="yyyyMMdd-HH:mm:ss" />--> <layout type="log4net.Layout.PatternLayout"> <!--每条日志开始的文字说明--> <!--<param name="Header" value="[Header]\r\n" />--> <!--每条日志末尾的文字说明--> <!--<param name="Footer" value="[Footer]\r\n"/>--> <!--输出格式--> <!--<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />--> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> <param name="Header" value=" ----------------------header-------------------------- " /> <param name="Footer" value=" ----------------------footer-------------------------- " /> </layout> </appender> <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net"> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> </appender> </log4net>
public class MvcApplication : SpringMvcApplication { protected void Application_Start() { //log4net 注册,使用路径 log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/Web.config")));
ILog logger = LogManager.GetLogger(this.GetType());
logger.Error(e.ToString());//将异常信息写到磁盘上.
} }
时间: 2024-10-26 11:56:24