之前在网上找了很多方法,可就是不行.不出现logfile.txt
http://stackoverflow.com/questions/17106682/why-is-log4net-not-recognized-in-configuration-file
---------------------------------------------------------------------------------------------------------------------------
本文用的是在app.config里配置
首先下载 log4net根据 .net framework 框架版本使用log4net-1.2.13\bin\net里的dll文件
复制进项目里,然后引用dll文件
如果你的项目已经正确引用文件,但依然提示未using,问题可能是.net版本的问题,进入项目属性里修改项目框架版本号,log4net支持的框架版本可以去官网找
比如它不支持net framework4 client profile ,且该版本里没有app.config,只有一个app.manifest
成功引用了库,且选择了受支持.net版本后
接着,在app.config里加入<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<log4net>
<!-- A1 is set to be a ConsoleAppender -->
<appender name="A1" type="log4net.Appender.FileAppender">
<file value="logfile.txt" />
<appendToFile value="false" />
<!-- A1 uses PatternLayout -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="A1" />
</root>
</log4net>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
</configuration>
接着在程序类里加入
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
并在执行程序里加入
log.Debug("hello");
运行,在bin目录里会出现logfile.txt
当然,你也可以自定义一个静态类,这样
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace TestWindow
{
static class Log4net
{
private static log4net.ILog staticlog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static log4net.ILog log
{
get {
return staticlog;
}
set {
;
}
}
}
}
方便使用.