C#Windows服务-如何处理日志记录异常? [英] c# windows-services - How do I handle logging exceptions?

查看:662
本文介绍了C#Windows服务-如何处理日志记录异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建Windows服务。当发生异常时,我会适当地处理它并创建一个日志。我正在使用装饰器模式,因为人们可以通过多种方式查看这些日志。我有一个电子邮件记录器,一个文件记录器和一个Windows事件记录器,所有这些都继承自实现ILogger的LoggingDecorator。因此,没有记录器知道任何其他记录器。

I am creating a Windows service. When an exception occurrs, I handle it appropriately and create a log. I am using the decorator pattern, as there are many different ways people will be looking at these logs. I have an email logger, a file logger, and a windows event logger, all which inherit from LoggingDecorator, which implements ILogger. So, no logger knows about any other logger.

我的问题是:我应该如何处理记录异常?

My question is: How should I handle logging exceptions?

如果写入文件失败或发送电子邮件失败,我该怎么办?我想与其他记录器一起记录初始日志内容,但是如何处理记录异常?

If writing to a file fails, or sending an email fails, what should I do? I want to log the initial log content with the other loggers, but what do I do with the logging exception? Doesn't it also depend on the order of the loggers in the constructor?

现在,我只是用空catch(Exception)语句包装try / catch块。 ,这感觉很脏,并且使FxCop向我吼叫。但是,这是否是取决于 时刻?

Right now, I'm just wrapping try/catch blocks with empty catch(Exception) statements, which just feels dirty and makes FxCop yell at me. However, is this one of those "it depends" moments?

[Flags]
public enum LoggingCategories
{
    None = 0,
    ServiceEvents = 1, 
    ProcessingInformation = 2,
    ProcessingErrors = 4,
    UnexpectedErrors = 8
}

public interface ILogger
{
    void LogMessage(LoggingCategories category, string message);
}

public abstract class LoggerDecorator : ILogger
{
    private ILogger _decoratedLogger;
    private LoggingCategories _categories;

    protected LoggerDecorator(ILogger logger, LoggingCategories categories)
    {
        this._decoratedLogger = logger;
        this._categories = categories;
    }

    protected bool ShouldLogCategory(LoggingCategories category)
    {
        return ((this._categories & category) == category);
    }

    public virtual void LogMessage(LoggingCategories category, string message)
    {
        _decoratedLogger.LogMessage(category, message);
    }

}

public class ControlLogger : ILogger
{
    public ControlLogger()
    {
    }

    public void LogMessage(LoggingCategories category, string message)
    {
        Console.WriteLine(LoggingHelper.ConstructLog(category, message));
    }
}

(WindowsEventLogger中的可疑代码)

(questionable code in WindowsEventLogger)

try
{
    this._eventLog.WriteEntry(log, type);
}
catch (Exception)
{
    //Even if this logging fails, we do not want to halt any further logging/processing.
}

(服务构造函数中的代码)

(code in service constructor)

ILogger controlLogger = new ControlLogger();
ILogger windowsEventLogger = new WindowsEventLogger(controlLogger, windowsEventLogCategories, windowsEventLogSource);
ILogger emailLogger = new EmailLogger(windowsEventLogger, emailCategories, emailSubject, emailAddresses);
ILogger fileLogger = new FileLogger(emailLogger, fileCategories, logDirectory, logFileNamePrefix, logFileExtension);

this._logger = fileLogger;


推荐答案

这不是答案,但很好奇为什么选择不使用现有的System.Diagnostics.Trace方法。您也许可以在上面实现某种类型的日志类型分类?

Its not an answer, but curious why did you choose to not utilize the existing System.Diagnostics.Trace methods. You could implement some type of categorization of log types on top it it perhaps?

这篇关于C#Windows服务-如何处理日志记录异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆