c#windows-services-ロギング例外を処理するにはどうすればよいですか?
-
19-08-2019 - |
質問
Windowsサービスを作成しています。例外が発生した場合、適切に処理してログを作成します。これらのログを見るにはさまざまな方法があるため、装飾パターンを使用しています。 。メールロガー、ファイルロガー、およびWindowsイベントロガーがあります。これらはすべて、ILoggerを実装するLoggingDecoratorから継承しています。したがって、他のロガーを知っているロガーはありません。
私の質問は、ロギング例外をどのように処理すればよいですか?
ファイルへの書き込みに失敗した場合、またはメールの送信に失敗した場合、どうすればよいですか?他のロガーで最初のログコンテンツを記録したいのですが、ロギング例外はどうすればよいですか?コンストラクター内のロガーの順序にも依存しませんか?
今、try / catchブロックを空のcatch(Exception)ステートメントでラップしているだけです。ただし、これは <!> quot;それは<!> quot; の瞬間に依存しますか?
[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の疑わしいコード)
try
{
this._eventLog.WriteEntry(log, type);
}
catch (Exception)
{
//Even if this logging fails, we do not want to halt any further logging/processing.
}
(サービスコンストラクターのコード)
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メソッドを利用しないことにしたのでしょうか。おそらくその上にログタイプの分類のいくつかのタイプを実装できますか?
他のヒント
ロガーが失敗した場合、実際のWindowsイベントログに記録しないのはなぜですか?
信頼性の高い信頼できる信頼できるマシンで、個別のpingサービスを作成します。プライマリサービスが失敗した場合、pingも失敗し、コントロールサービスは警告をメールで送信します。
所属していません StackOverflow