C#环境下log4net使用方法
1. 下载log4net开发包
2. 新建一个Windows应用程序,在工程中添加log4net.dll引用
3. 在工程中添加应用程序配置文件,并命名为log4net.config,内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Register a section handler for the log4net section -->
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<!-- This section contains the log4net configuration settings -->
<log4net>
<!-- Define some output appenders -->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="log//log-iQSLive.txt"/>
<param name="appendToFile" value="true"/>
<param name="datePattern" value="yyyy-MM-dd"/>
<param name="rollingStyle" value="Date"/>
<param name="MaxSizeRollBackups" value="60"/>
<layout type="log4net.Layout.PatternLayout">
<param name="conversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%t] [%c]-[%-5p] %m%n"/>
</layout>
</appender>
<root>
<level value="Debug"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>s
</configuration>
4.在工程的AssemblyInfo.cs文件中添加如下语句:
[assembly: log4net.Config.XmlConfigurator(Watch = true, ConfigFile = "log4net.config")]
5. 在From1类中添加:
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
6. 添加一个按钮,并在响应事件中添加如下代码:
log.Info("按钮被点击!");
7. 将log4net.config文件拷贝到Debug目录下,运行程序点击按钮即可在Debug目录下看到log文件夹下的日志文件