首页 > 编程 > .NET > 正文

.Net配置log4net

2024-07-10 12:59:14
字体:
来源:转载
供稿:网友

使用log4net-1.2.0-beta8
在项目的assemblyinfo.cs文件中设置

[assembly: log4net.config.domconfiguratorattribute(watch=true)]
添加并编辑项目配置文件(默认为app.config),内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configsections>
<section name="log4net" type="log4net.config.log4netconfigurationsectionhandler, log4net"/>
</configsections>
<log4net>
<appender name="stdout" type="log4net.appender.consoleappender" >
<layout type="log4net.layout.patternlayout">
<!--conversionpattern value="[%d %l] %n%m%n" /-->
<param name="conversionpattern" value="[%d %l] %m%n" />
</layout>
</appender>
<appender name="file" type="log4net.appender.fileappender">
<param name="file" value="log.txt" />
<param name="appendtofile" value="true" />
<layout type="log4net.layout.patternlayout">
<param name="conversionpattern" value="[%d %l] %m%n" />
</layout>
</appender>


<root>
<level value="all" />
<appender-ref ref="stdout" />
<appender-ref ref="file" />
</root>

<logger name="*">
<level value="all" />
<appender-ref ref="stdout" />
<appender-ref ref="file" />
</logger>
</log4net>
</configuration>



程序代码中使用如下语句
...
using log4net;
using log4net.config;
...
private static ilog log = logmanager.getlogger(typeof(testclass));
...
log.info("this is a test!");

ok!收工


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表