【发布时间】:2015-06-24 14:46:33
【问题描述】:
我非常仔细地按照 log4net 教程进行操作,但记录器未能保存为 txt 文件,也未能出现在控制台中。我希望日志文件保存在 C:\temp 下。
这是我的 app.config 文件:
<?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="C:\temp\Logs.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="3MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
<root>
<level value="Debug"/>
<appender-ref ref="LogFileAppender"/>
</root>
</log4net>
</configuration>
我也加入了
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
在 AssemblyInfo.cs 中。
我将记录器放在 LoggerView.xaml.cs 类中。 (我正在使用 MVVM 设计模式。)
[Export]
[PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
public partial class LoggerView : Window
{
private LoggerViewModel _viewModel;
protected static readonly ILog log = LogManager.GetLogger(typeof(LoggerView));
[ImportingConstructor]
public LoggerView(LoggerViewModel viewModel)
{
_viewModel = viewModel;
this.DataContext = _viewModel;
InitializeComponent();
}
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
log.Debug("Debug Statement");
log.Info("Info");
log.Error("Error Statement");
log.Warn("Warning Statement");
log.Fatal("Fatal Statement");
}
}
我有什么遗漏或做错了吗?
谢谢。
【问题讨论】:
-
在运行时检查
LogManager.GetRepository().Configured以查看 log4net 是否已获取配置,您还可以查看LogManager.GetRepository().ConfigurationMessages.Cast<LogLog>()是否有任何配置错误。这些可能不包括权限错误。 -
尝试开启log4net内部调试:stackoverflow.com/questions/756125/…
标签: c# wpf logging mvvm log4net