log4net应用实例
官方站点:http://logging.apache.org/log4net/
一、关联配置文件
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = false)]
二、web.config配置
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<!--log4net组件-->
<log4net>
<root>
<!--从高到低的七个级别:OFF,FATAL,RROR,WARN,INFO,DEBUG,ALL-->
<level value="ALL" />
</root>
<!--配置记录者-->
<logger name="log1">
<level value="ALL" />
<appender-ref ref="app1" />
</logger>
<logger name="log2">
<level value="ALL" />
<appender-ref ref="app2" />
</logger>
<!--配置记录介质-->
<appender name="app1" type="log4net.Appender.RollingFileAppender, log4net">
<!--日志路径-->
<param name="File" value="D:\log\log1\" />
<!--日期为滚动类型(每天产生一个日志文件)-->
<param name="RollingStyle" value="Date" />
<!--追加方式-->
<param name="AppendToFile" value="true" />
<!--日志文件名-->
<param name="DatePattern" value="yyyy-MM-dd".log"" />
<!--关闭固定文件方式-->
<param name="StaticLogFileName" value="false" />
<!--记录格式-->
<layout type="log4net.Layout.PatternLayout, log4net">
<!--%m:消息内容-->
<!--%n:换行-->
<!--%d:输出时间-->
<!--%p:级别-->
<!--%c:类名-->
<!--%F:文件名-->
<param name="ConversionPattern" value="[%d] %p - %m%n"/>
</layout>
<!--过滤器-->
<filter type="log4net.Filter.LevelRangeFilter, log4net">
<param name="LevelMin" value="ALL" />
<param name="LevelMax" value="OFF" />
</filter>
</appender>
<appender name="app2" type="log4net.Appender.RollingFileAppender, log4net">
<param name="File" value="D:\log\log2\" />
<param name="RollingStyle" value="Date" />
<param name="AppendToFile" value="true" />
<param name="DatePattern" value="yyyy-MM-dd".log"" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout, log4net">
<param name="ConversionPattern" value="[%d] %p - %m%n"/>
</layout>
</appender>
</log4net>
<log4net>
<root>
<!--从高到低的七个级别:OFF,FATAL,RROR,WARN,INFO,DEBUG,ALL-->
<level value="ALL" />
</root>
<!--配置记录者-->
<logger name="log1">
<level value="ALL" />
<appender-ref ref="app1" />
</logger>
<logger name="log2">
<level value="ALL" />
<appender-ref ref="app2" />
</logger>
<!--配置记录介质-->
<appender name="app1" type="log4net.Appender.RollingFileAppender, log4net">
<!--日志路径-->
<param name="File" value="D:\log\log1\" />
<!--日期为滚动类型(每天产生一个日志文件)-->
<param name="RollingStyle" value="Date" />
<!--追加方式-->
<param name="AppendToFile" value="true" />
<!--日志文件名-->
<param name="DatePattern" value="yyyy-MM-dd".log"" />
<!--关闭固定文件方式-->
<param name="StaticLogFileName" value="false" />
<!--记录格式-->
<layout type="log4net.Layout.PatternLayout, log4net">
<!--%m:消息内容-->
<!--%n:换行-->
<!--%d:输出时间-->
<!--%p:级别-->
<!--%c:类名-->
<!--%F:文件名-->
<param name="ConversionPattern" value="[%d] %p - %m%n"/>
</layout>
<!--过滤器-->
<filter type="log4net.Filter.LevelRangeFilter, log4net">
<param name="LevelMin" value="ALL" />
<param name="LevelMax" value="OFF" />
</filter>
</appender>
<appender name="app2" type="log4net.Appender.RollingFileAppender, log4net">
<param name="File" value="D:\log\log2\" />
<param name="RollingStyle" value="Date" />
<param name="AppendToFile" value="true" />
<param name="DatePattern" value="yyyy-MM-dd".log"" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout, log4net">
<param name="ConversionPattern" value="[%d] %p - %m%n"/>
</layout>
</appender>
</log4net>
三、代码
//创建对象
log4net.ILog myLog1 = log4net.LogManager.GetLogger("log1");
log4net.ILog myLog2 = log4net.LogManager.GetLogger("log2");
//日志记录
myLog1.Info("this is info message");
myLog2.Error("this is error message");
log4net.ILog myLog1 = log4net.LogManager.GetLogger("log1");
log4net.ILog myLog2 = log4net.LogManager.GetLogger("log2");
//日志记录
myLog1.Info("this is info message");
myLog2.Error("this is error message");