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にしておく