log4net的使用
1.下载log4net.dll
2.将组件添加引用到项目中
3.项目里新建一个配置文件Log4Net.config,删除自动生成的代码,Copy下列代码到该文件.
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<span style="color: rgb(102, 102, 204);"><appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<log4net>
<!-- A1 is set to be a ConsoleAppender -->
<appender name="A1" type="log4net.Appender.FileAppender">
<file value="logfile.txt" />
<appendToFile value="false" />
<!-- A1 uses PatternLayout -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="A1" />
</root>
</log4net>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections></span>
</configuration>
4.在程序类里面加入(注意是在namespace上面加)
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
5.并在执行程序里面加
log.Debug("hello");
运行后,在bin目录里面会生成logfile.txt
也可以自定义一个静态类
namespace Attendance
{
statuc class Log4net.ILog staticlog=log4net.LogManger.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static log4net.ILog log
{
get
{
return staticlog;
}
set
{
}
}
}
在方法中调用 staticlog.Warn(“”); staticlog.Error(“”); staticlog.Info(“”);
Staticlog.Debug(“”); staticlog.Off(“”); staticlog.Fatal(“”);
- <!-- 日志级别:
- 级别 允许的方法 Boolean属性 优先级别
- OFF Highest
- FATAL void Fatal(...); bool IsFatalEnabled;
- RROR void Error(...); bool IsErrorEnabled;
- WARN void Warn(...); bool IsWarnEnabled;
- INFO void Info(...); bool IsInfoEnabled;
- DEBUG void Debug(...); bool IsDebugEnabled;
- ALL Lowest
10. -->
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。