我有一个启动控制台应用程序的Web服务。控制台应用程序包含Log4net。
来自控制台应用的代码:
log4net.Config.XmlConfigurator.Configure();
ILog log = log4net.LogManager.GetLogger(typeof(Program));
我遇到的问题是,当它以network services
运行时,没有创建log4net日志文件。应用程序不会失败它只是继续而不记录。
我需要找到一种方法来检测这个并使应用程序失败,如果没有日志文件我们就无法继续。我需要通知用户他们需要修复权限,以便网络服务可以创建文件。
如何检测到无法创建文件?我能够提出的唯一解决方案是检查文件是否存在于应用程序的顶部,以及是否已创建停止。这对我来说只是一个黑客攻击Log4net应该可以告诉我它无法创建文件。
更新:
我正在测试以下内容。我目前的测试方法是将文件设置为仅在第一次创建文件后才能读取,然后它才能访问它。此时它应该失败,因为它无法写入日志文件。
string logFile = @"D:\TaskDev\Log4NetTesting\Log4NetTesting\bin\Debug\Test\Log4netTesting.Log";
log4net.GlobalContext.Properties["LogFileName"] = logFile;
log4net.Config.XmlConfigurator.Configure();
log4net.NDC.Push("Test Application");
ILog log = log4net.LogManager.GetLogger(typeof(Program));
var m = LogManager.GetAllRepositories().SelectMany(repository => repository.GetAppenders()).OfType<FileAppender>();
try
{
log.Info("hello Log test");
}
catch (Exception ex) {
Console.WriteLine("Log write failed: " + ex.Message);
}
Console.WriteLine("Done");
Console.ReadLine();
的app.config
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<errorHandler type="Log4NetTesting.util.CustomLogErrorHandler" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="%property{LogFileName}" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="250KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
答案 0 :(得分:3)
您是否考虑过为FileAppender设置ErrorHandler?
您可以使用以下代码获取配置文件中设置的FileAppender:
var fileAppenders = LogManager.GetAllRepositories().SelectMany(repository => repository.GetAppenders()).OfType<FileAppender>();
foreach (var fileAppender in fileAppenders) {
fileAppender.ErrorHandler = new MyErrorHandler();
}
MyErrorHandler类有点像这样:
class MyErrorHandler : IErrorHandler {
public void Error(string messages) {
// giver error message and abort
}
public void Error(string messages, Exception exception) {
// giver error message and abort
}
public void Error(string messages, Exception exception, ErrorCode errorCode) {
// giver error message and abort
}
}
我不确定“giver error message and abort”有多么简单,你可以像往常一样在服务中没有GUI访问
此处列出了FileAppender成员:http://logging.apache.org/log4net/release/sdk/html/AllMembers_T_log4net_Appender_FileAppender.htm 和IErrorHandler成员在这里: http://logging.apache.org/log4net/release/sdk/html/AllMembers_T_log4net_Core_IErrorHandler.htm
您可能还想在repository.ConfigurationChanged
答案 1 :(得分:0)
通过Jens的一些提示,我能够实现这一目标。
app.config - 添加了这一行
<errorHandler type="Log4NetTesting.util.CustomLogErrorHandler" />
CustomLogErrorHandler
public class LogException : Exception
{
private readonly ErrorCode errorCode;
/// <summary>Gets the service name which related to this exception.</summary>
public ErrorCode ErrorCode
{
get { return errorCode; }
}
public LogException()
{
}
public LogException(string message)
: base(message)
{
}
public LogException(string message, Exception inner)
: base(message, inner)
{
}
public LogException(string message, Exception inner, ErrorCode errorCode)
: base(message, inner)
{
this.errorCode = errorCode;
}
}
public class CustomLogErrorHandler : IErrorHandler
{
public string message { get;private set; }
public Exception Exception { get; private set; }
public ErrorCode ErrorCode { get; private set; }
#region IErrorHandler Members
public void Error(string message)
{
this.message = message;
}
public void Error(string message, Exception e)
{
this.message = message;
this.Exception = e;
}
public void Error(string message, Exception e, ErrorCode errorCode)
{
this.message = message;
this.Exception = e;
this.ErrorCode = errorCode;
}
#endregion
}
测试
try
{
string logFile = @"D:\TaskDev\Log4NetTesting\Log4NetTesting\bin\Debug\Test\Log4netTesting.Log";
log4net.GlobalContext.Properties["LogFileName"] = logFile;
log4net.Config.XmlConfigurator.Configure();
log4net.NDC.Push("Test Application");
ILog log = log4net.LogManager.GetLogger(typeof(Program));
var fileAppenders = LogManager.GetAllRepositories().SelectMany(repository => repository.GetAppenders()).OfType<FileAppender>();
var customLogErrorHandler = new CustomLogErrorHandler(); // custom error IErrorHandler.
// loop though fileappenders adding the error handler
foreach (var fileAppender in fileAppenders)
{
fileAppender.ErrorHandler = customLogErrorHandler;
}
log.Info("hello Log file");
if (customLogErrorHandler.message != null) // if there is an error throw exception
{
throw new LogException(customLogErrorHandler.message, customLogErrorHandler.Exception, customLogErrorHandler.ErrorCode);
}
}
catch (LogException lex) {
// catch log exception
Console.WriteLine(lex.ErrorCode);
Console.WriteLine(lex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
虽然我明白,默默地失败对某些人来说可能不错。对于我的应用程序,如果我没有日志文件,如果Log4Net无法告诉我它有问题,我们的用户就无法知道存在问题。
答案 2 :(得分:0)
您可以执行以下操作来验证配置是否有效:
if(!LogManager.GetRepository().Configured)
{
XmlConfigurator.Configure();
}
if (!LogManager.GetRepository().Configured)
{
var logMsg = new StringBuilder();
logMsg.AppendLine("Failed to initialize log4net");
foreach(var msg in LogManager.GetRepository().ConfigurationMessages)
{
logMsg.AppendLine(msg.ToString());
}
// ....do something with the message, raise
// an evt to UI, log to OS Event logger, etc....
}