Here's how:
One, app. config configuration
<?xml version="1.0"encoding="Utf-8"?><configuration> <configSections> <section name="log4net"Type="System.Configuration.IgnoreSectionHandler"/> </configSections> <startup> <supportedruntime version="v4.0"sku=". netframework,version=v4.5"/> </startup> <appSettings> <!--Compressed file decompression path--<add value="D:\FileCompare"key="Filecompare"/> </appSettings> <log4net> <appender name="Rollinglogfileappender"Type="log4net. Appender.rollingfileappender"> <param name="File"Value="Errorlogs\errorlog.txt"/> <param name="Appendtofile"Value="true"/> <param name="maxsizerollbackups"Value=" -"/> <param name="maximumFileSize"Value="8096KB"/> <param name="Rollingstyle"Value="Size"/> <param name="Staticlogfilename"Value="true"/> <layout type="log4net. Layout.patternlayout"> <!--<param name="Header"Value="[header]\r\n"/> <param name="Footer"Value="[footer]\r\n"/>--> <param name="Conversionpattern"Value="%d%-5p%c%m%n"/> </layout> </appender> <root> <level value=" All"/> <appender-ref ref="Rollinglogfileappender"/> </root> </log4net></configuration>
Second, call in Form.cs: Initialize log
Private void Btnselect_click (object sender, EventArgs e) { = Logmanager.getlogger (" Error");}
Three, Assmbely.cs configuration
?
| 1 2 3 4 |
//[assembly: log4net.Config.XmlConfigurator(Watch = true, ConfigFile = "Log4Net.config")] // 日志组件配置 //[assembly: log4net.Config.DOMConfigurator(ConfigFile = "Log4Net.config", Watch = true)][assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "config", Watch = true)] |
Don't forget to introduce Log4net.dll.
If the "app. Config hint error" configuration system failed to initialize "
Solution:
"If the configuration file contains a configsections element, the configsections element must be the first child element of the configuration element. "So if there is a connectionstrings or other node in front of it, you must put the connectionstrings behind it."