环境:log4net.dll程序一个,它的记录方式一般是存储于文本文件、SqlLite数据库、控制台控制显示、Windows事件日志
以App.Config文件为主进行配置,根节点是<configuration></configuration>
在该根节点中添加:
< configSections >
< section name = " log4net " type = " System.Configuration.IgnoreSectionHandler " />
</ configSections >
和
< log4net >
<!--一.定义输出到文件中-->
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--显示指定文件存放位置-->
<!--<file value="D:\log4netfile.txt" />--><!--日志文件名开头-->
<param name="File" value="MonitorConfigLog" /><!--默认存放根目录-->
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10"/><!-- 切割最多文件数 -->
<param name="MaximumFileSize" value="10MB"/><!-- 每个文件的大小 -->
<param name="StaticLogFileName" value="false"/>
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd".log"" /><!--文件名后缀(带日期)-->
<layout type="log4net.Layout.PatternLayout">
<footer value="by Xluo" /> <!--每条日志末尾的文字说明-->
<conversionPattern value="*记录时间【%date】 级别【%-5level】 出错位置【%logger 行号:%line】 错误描述:%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 = " LogFileAppender " />
<!-- 控制台控制显示日志 -->
< appender - ref ref = " ConsoleAppender " />
<!-- Windows事件日志 -->
<!--< appender - ref ref = " EventLogAppender " />-->
<!-- 如果不启用相应的日志记录,可以通过这种方式注释掉 -->
<!--< appender - ref ref = " AdoNetAppender_Access " />-->
</ root >
</ log4net >
比如,我想在项目在UI层,添加几句示例代码,需注意以下几点:
1.在命名空间上方,需注明属性:
[assembly: log4net.Config.XmlConfigurator(Watch = true )]
namespace MonitorConfig.UI
2.在后台(.cs)文件中,实例化接口:
// 创建日志记录组件实例
ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
3.记录代码书写方式:
private void Form1_Load( object sender, EventArgs e)
{// 记录日志
log.Error( " 有可能是数据库连接异常 " , new Exception( " 绑定前,发生一个异常-1 " ));
log.Info( " 有可能是参数不正确 " , new Exception( " 绑定前,发生一个异常-2 " ));
}
4.日志查看:
AppConfig文件中,是配置在D盘的,文件名为 log4netfile.txt
参考来源:
http://blog.csdn.net/zhoufoxcn/archive/2008/03/26/2220533.aspx
http://blog.csdn.net/zhoufoxcn/archive/2010/11/23/6029021.aspx