添加log4net.dll引用
在項目根目錄下新建log4net.XML
log4net.XML
<?XML version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="INFO" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p %d %c %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
</layout>
</appender>
</log4net>
在AssemblyInfo.cs中添加
[assembly: log4net.Config.DOMConfigurator(ConfigFile = "log4net.XML", Watch = true)]
在程序中聲明
private static readonly ILog logger = LogManager.GetLogger(typeof(Form1));
logger.Info("開始備份數據.");