log4net写入日志文件示例时一个奇怪的错误(附源码下载)
昨天园子里朋友问起:利用log4net向文件里记录日志不成功!
因为以前邀月曾经在写入SQL时费了一番周折,以为这次应该不会有问题,如法炮制即可。可以参考
log4net写入到SQL server的基本配置(downmoon)
不料试了一下,使用最新版版的log4net1.2.10 ,发现FileAppender老是不成功!
把步骤写下来:
先将下载的log4net.dll添加引用,再在(用的是Console Application)AssemblyInfo.cs中添加:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
并添加独立的lognet.config文件如下:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<!--Log4net Begin by Tony 2009.09.28-->
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="D:\\Log4netDemo.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- setup the root category, add the appenders and set the default level -->
<root>
<level value="DEBUG"/>
<appender-ref ref="LogFileAppender" />
</root>
<!-- specify the level for some specific categories -->
<!-- <logger name="iNotes">
<level value="WARN"/>
<level value="INFO"/>
<level value="DEBUG"/>
<level value="FINE"/>
<appender-ref ref="LogFileAppender"/>
</logger>
<logger name="StellaLogger">
<level value="ALL"/>
<appender-ref ref="LogFileAppender" />
</logger>
-->
</log4net>
<!--Log4net End-->
</configuration>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<!--Log4net Begin by Tony 2009.09.28-->
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="D:\\Log4netDemo.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- setup the root category, add the appenders and set the default level -->
<root>
<level value="DEBUG"/>
<appender-ref ref="LogFileAppender" />
</root>
<!-- specify the level for some specific categories -->
<!-- <logger name="iNotes">
<level value="WARN"/>
<level value="INFO"/>
<level value="DEBUG"/>
<level value="FINE"/>
<appender-ref ref="LogFileAppender"/>
</logger>
<logger name="StellaLogger">
<level value="ALL"/>
<appender-ref ref="LogFileAppender" />
</logger>
-->
</log4net>
<!--Log4net End-->
</configuration>
这是参考官方配置写的,应该不会有问题。不过始终没有写成功!
测试代码如下:
namespace TestLog4NetFile
{
public class Program
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
private static log4net.ILog myLogger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void Main(string[] args)
{
TestlogtoFile();
}
public static void TestlogtoFile()
{
string title = "log4net测试写入File";
for (int i = 1; i < 3; i++)
{
try
{
int lng = int.Parse("testnumber");
}
catch (System.Exception ex)
{
//记录错误日志
myLogger.Error(title + "error:" + ex.Message);
//记录严重错误
myLogger.Fatal(title + "fatal:" + ex.Message);
//记录一般信息
myLogger.Info(title + "info:" + ex.Message);
//记录调试信息
myLogger.Debug(title + "debug:", ex);
//记录警告信息
myLogger.Warn(title + "warn:", ex);
Console.WriteLine("日志记录" + i + "完毕。");
}
}
myLogger.Error(title + "Error:" + System.DateTime.Now);
Console.WriteLine("日志记录{X}完毕。");
Log4netDemo.TestlogtoFile();
Console.WriteLine("当前类调用其他类日志记录完毕。");
try
{
throw new Exception(title);
}
catch (System.Exception)
{
//记录错误日志
log.Error("error");
//记录严重错误
log.Fatal("fatal");
//记录一般信息
log.Info("info");
//记录调试信息
log.Debug("debug");
//记录警告信息
log.Warn("warn");
Console.WriteLine("当前类日志记录完毕。");
Console.ReadKey();
}
Console.ReadKey();
}
public class Log4netDemo
{
public Log4netDemo() { }
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Log4netDemo));
public static void TestlogtoFile()
{
try
{
throw new Exception("log4net测试写入File");
}
catch (System.Exception)
{
//记录错误日志
log.Error("error");
//记录严重错误
log.Fatal("fatal");
//记录一般信息
log.Info("info");
//记录调试信息
log.Debug("debug");
//记录警告信息
log.Warn("warn");
Console.WriteLine("日志记录完毕。");
Console.ReadKey();
}
}
}
}
}
不知道原因,后来看到有人在winform下写入成功,用的是log4net.Appender.RollingFileAppender
试着改了下配置文件,结果调试成功!修改后的配置文件如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="d:\\Log4netDemo" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMddHH".log"" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<!--<param name="conversionPattern" value="%d [%t] 日志级别:%-5p 出错类: %l - 描述:%message%newline" />-->
<param name="ConversionPattern" value="%d [%t] %-5p %l %m %n" />
</layout>
</appender>
<root>
<level value="All" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="d:\\Log4netDemo" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMddHH".log"" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<!--<param name="conversionPattern" value="%d [%t] 日志级别:%-5p 出错类: %l - 描述:%message%newline" />-->
<param name="ConversionPattern" value="%d [%t] %-5p %l %m %n" />
</layout>
</appender>
<root>
<level value="All" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>
调试结果如图:
希望有高手解答一下这是为何?有时间自己再研究一下。 后来试了下,奇怪,以前的配置文件也可以写成功!难道是log4net版本的问题?下载最新源码1.2.10调试后发现成功·用release生成的dll也成功· 真的是不明白。
附源码下载