1.添加log4net.dll引用
2.在项目根目录下新建log4net.xml
log4net.xml
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="c:\ehailog.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p %d %c %m%n"/>
</layout>
</appender>
</log4net>
3.在AssemblyInfo.cs中添加
[assembly: log4net.Config.DOMConfigurator(ConfigFile = "log4net.xml", Watch = true)]
4.在程序中声明
private static readonly ILog logger = LogManager.GetLogger(typeof(类));
logger.Info("开始备份数据.");
日志级别由高往低依次是:
None
Fatal
ERROR
WARN
DEBUG
INFO
ALL