首先将log4net.dll下载来,添加到项目引用中:
在assembly文件最后面加(其实没关系的):
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config",ConfigFileExtension = "config",Watch = true)]
单独的log4net.config文件,要加的项目里添加新建项,配置文件log4net.config,右击该文件,属性=》如果较新则复制,内容如下
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> </layout> </appender> <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> <param name="File" value="my_log-file.txt"/> <param name="AppendToFile" value="true"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%d [%t] %-5p %c [%x] <%X{auth}>%n - %m%n" /> </layout> </appender> <root> <level value="ALL" /> <appender-ref ref="LogFileAppender" /> </root> </log4net>
这样算是配置完毕了
开始使用log4net吧
示例如下:
log4net.ILog log = log4net.LogManager.GetLogger("MyLogger");//MyLogger是实例名字 if (log.IsDebugEnabled)//是否启用了Debug { log.Debug("hello,log4net"); }
文章参考:http://www.cnblogs.com/kissazi2/p/3392094.html