Note that there are some explanatory texts on larger screens.

plurals
  1. POc# windows-services - How do I handle logging exceptions?
    text
    copied!<p>I am creating a Windows service. When an exception occurrs, I handle it appropriately and create a log. I am using the <a href="http://en.wikipedia.org/wiki/Decorator_pattern" rel="nofollow noreferrer">decorator pattern</a>, 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.</p> <p>My question is: How should I handle logging exceptions? </p> <p>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?</p> <p>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 <a href="https://stackoverflow.com/questions/21938/is-it-really-that-bad-to-catch-a-general-exception">"it depends"</a> moments?</p> <pre><code>[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 &amp; 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)); } } </code></pre> <p>(questionable code in WindowsEventLogger)</p> <pre><code>try { this._eventLog.WriteEntry(log, type); } catch (Exception) { //Even if this logging fails, we do not want to halt any further logging/processing. } </code></pre> <p>(code in service constructor)</p> <pre><code>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; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload