kikki's tech note

技術ブログです。UnityやSpine、MS、Javaなど技術色々について解説しています。

C#でWindowsイベントに登録するLoggingライブラリを作ってみた

本章では、C#WPFASP.NET MVCで活用できる、ログ書き込みライブラリについて共有します。

ロギングの手法

C#でのログ出力用ライブラリは、以下の通り、いくつかあります。

  • log4net
  • NLog
  • EventLog
  • EventSource

今回は、その中でも比較的簡単に扱える、「EventLog」を採用します。

ライブラリ本体

以下に、ライブラリを示します。

using System;
using System.Diagnostics.Tracing;

namespace LogLibrary
{
    public static class EventLogLogger
    {
        private static readonly string _SOURCE_NAME = "【ソース名】";
        private static string _MESSAGE_FORMAT = "【ログのフォーマット】";
        private static string _ANONYMOUS = "anonymous";
        private static EventLog _log;
        private static EventLogLogger _eventLogInstance;

        public static EventLogLogger EventLogInstance
        {
            get
            {
                if (_eventLogInstance == null)
                {
                    if (!EventLog.SourceExists(_SOURCE_NAME))
                    {
                        EventLog.CreateEventSource(_SOURCE_NAME, "");
                    }
                    _eventLogInstance = new EventLogLogger();
                }
                return _eventLogInstance;
            }
        }

        public EventLogLogger()
        {
            _log = new EventLog();
            _log.Source = _SOURCE_NAME;
        }

        public void Write(LogLevel level, string message)
        {
            switch (level)
            {
                case LogLevel.Fatal:
                    this.Fatal(message, userId);
                    break;
                case LogLevel.Error:
                    this.Error(message, userId);
                    break;
                case LogLevel.Warn:
                    this.Warn(message, userId);
                    break;
                case LogLevel.Info:
                    this.Info(message, userId);
                    break;
                case LogLevel.Debug:
                    this.Debug(message, userId);
                    break;
                case LogLevel.Trace:
                    this.Trace(message, userId);
                    break;
                default:
                    break;
            }
        }
        
        private void Fatal(string message)
        {
            _log.WriteEntry(String.Format("Fatal: " + _MESSAGE_FORMAT, DateTime.Now, message)
                , EventLogEntryType.Error, 1);
        }
        
        private void Error(string message)
        {
            _log.WriteEntry(String.Format("Error: " + _MESSAGE_FORMAT, DateTime.Now, message)
                , EventLogEntryType.Error, 2);
        }
        
        private void Warn(string message)
        {
            _log.WriteEntry(String.Format("Warn: " + _MESSAGE_FORMAT, DateTime.Now, message)
                , EventLogEntryType.Warning, 3);
        }
        
        private void Info(string message)
        {
            _log.WriteEntry(String.Format("Info: " + _MESSAGE_FORMAT, DateTime.Now, message)
                , EventLogEntryType.Information, 4);
        }
        
        private void Debug(string message)
        {
            _log.WriteEntry(String.Format("Debug: " + _MESSAGE_FORMAT, DateTime.Now, message)
                , EventLogEntryType.Information, 5);
        }
        
        private void Trace(string message)
        {
            _log.WriteEntry(String.Format("Trace: " + _MESSAGE_FORMAT, DateTime.Now, message)
                , EventLogEntryType.Information, 6);
        }
    }

    public enum LogLevel
    {
        Fatal,
        Error,
        Warn,
        Info,
        Debug,
        Trace,
    }
}

ログの種類(エラーやデバッグ情報など)に応じて、イベントIDを割り振り、ログをWindowsのイベント情報として登録します。

利用例

利用方法について簡単に説明します。

    [TestClass]
    public class LG01LoggingTest
    {
        [TestMethod]
        public void Write_Info_Test()
        {
            try
            {
                EventLogLogger.EventLogInstance.Write(LogLevel.Info, "TEST_MESSAGE");
            }
            catch
            {
                Assert.Fail();
            }
        }
    }

筆休め

現在ロギングの手法も、「log4net」一択ではなく、様々な手法が生み出されています。色々な可能性を試し、要件にあった手法を利用できればと考えます。

以上、「C#Windowsイベントに登録するLoggingライブラリを作ってみた」でした。


※無断転載禁止 Copyright (C) kikkisnrdec All Rights Reserved.