我在最新版本的 log4net 1.2.12 中遇到了一个奇怪的问题, 我的日志文件创建成功没有问题,无论如何,总是在 Exe 文件的同一目录中创建一个(空)文件。
这是我的 xml 配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="DEBUG"/>
<acceptOnMatch value="false"/>
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5.5level] %logger - %message%newline"/>
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<threshold value="ALL"/>
<File type="log4net.Util.PatternString" value="%property{LogFileName}"/>
<param name="appendToFile" value="false"/>
<param name="maximumFileSize" value="20000KB"/>
<param name="maxSizeRollBackups" value="200"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5.5level] %logger - %message%newline"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFile"/>
</root>
</log4net>
</configuration>
这就是我从我的主要调用它的方式:
class Program
{
/// <summary>
/// The logger
/// </summary>
private static ILog logger = LogManager.GetLogger(typeof(Program));
/// <summary>
/// The CMD line args
/// </summary>
public static Arguments CmdLineArgs = null;
/// <summary>
/// Main.
/// </summary>
/// <param name="args">The arguments.</param>
/// <returns></returns>
static int Main(string[] args)
{
try
{
CmdLineArgs = new Arguments();
Parser CmdLineParser = new Parser();
if (CmdLineParser.ParseArguments(args, CmdLineArgs))
{
log4net.GlobalContext.Properties["LogFileName"] = CmdLineArgs.LogFile;
log4net.Config.XmlConfigurator.Configure();
CmdLineArgs.PrintArguments(args);
CmdLineArgs.Check();
CmdLineArgs.PrintArguments();
}
else
{
CmdLineArgs.PrintArguments(args);
Console.WriteLine(CmdLineArgs.GetUsage());
Console.WriteLine(string.Format("Return Code: {0}", 2));
return 2;
}
logger.Info("Execution Terminated");
logger.Info(string.Format("Return Code: {0}", 0));
return 0;
}
catch (Exception ex)
{
if (logger.IsInfoEnabled)
{
logger.Fatal("Fatal Error:", ex);
}
else
{
Console.WriteLine(ex.ToString());
Console.WriteLine(string.Format("Return Code: {0}", 8));
}
logger.Info(string.Format("Return Code: {0}", 8));
return 8;
}
}
}
这完全适用于 log4net 1.2.11 和以前的版本,但使用 log4net 1.2.12 创建一个(空)文件。
我正在使用 VS2012 和 .net 4.0 框架。