LoginSignup
0
2

More than 3 years have passed since last update.

イベントログにログ出力する共通クラス

Last updated at Posted at 2019-03-08
LogHelper.cs

using System;
using System.Diagnostics;

namespace LogSample.Service
{
    public class LogHelper
    {
        private static readonly string _SOURCE_NAME = "Application";
        private static string _MESSAGE_FORMAT = "OccurrTime:{0};Message:{1}";
        private static EventLog _log;
        private static LogHelper _eventLogInstance;

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

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

        public void WriteError(string message)
        {
            _log.WriteEntry(String.Format("Error: " + _MESSAGE_FORMAT, DateTime.Now, message)
                , EventLogEntryType.Error);//, 2);
        }

        public void WriteWarn(string message)
        {
            _log.WriteEntry(String.Format("Warn: " + _MESSAGE_FORMAT, DateTime.Now, message)
                , EventLogEntryType.Warning);//, 3);
        }

        public void WriteInfo(string message)
        {
            _log.WriteEntry(String.Format("Info: " + _MESSAGE_FORMAT, DateTime.Now, message)
                , EventLogEntryType.Information);//, 4);
        }

    }
}

権限の問題が出てきそうなので現段階ではソース名はApplicationにしておく

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2