我非常仔细地遵循了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");
}
}
我有什么遗漏或做错了吗?
谢谢。
答案 0 :(得分:0)
验证log4net是否具有写入该文件夹的正确权限 关于登录到控制台,您需要添加appender以及更多详细信息,请参阅此帖子How to configure log4net to print to console in debug mode
这是log4net正常工作的配置文件的一部分:
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="SmtpAppender" />
<appender-ref ref="AdoNetAppender" />
</root>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\EntitlementPermissionManagerService.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
答案 1 :(得分:0)
尝试在AssemblyInfo.cs中指定配置文件,我的是这样的:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
将XML文件log4net.config添加到项目中,使用此文件开头:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="=========================================
%nLEVEL:........%-5level
%nTIMESTAMP:....%date
%nMessage:
%n%m
%nUsername: %username
%n=========================================
%n" />
</layout>
</appender>
<root>
<level value="all" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="SmtpAppender" />
</root>
</log4net>
</configuration>
然后把它放在你的应用程序中(我的只是一个简单的控制台应用程序,只记录“测试”。
class Program
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
log.Error("Test");
Console.ReadLine();
}
}
答案 2 :(得分:0)
您可以通过设置log4net诊断来获取有关log4net的更多详细信息。请查看此链接How to track down log4net problems