■ サンプル
EventLogger.cs
using System.Diagnostics;
namespace SampleForm
{
public static class EventLogger
{
private const string SourceName = "HelloWorld";
// イベントID ★以下「使用上の注意」も参照★
private const int DefaultEventId = 11000;
private const int WarningEventId = 11001;
private const int ErrorEventId = 11002;
static EventLogger()
{
if (!EventLog.SourceExists(SourceName))
{
EventLog.CreateEventSource(SourceName, string.Empty);
}
}
public static void WriteInfo(string content)
{
EventLogger.WriteLog(content, EventLogEntryType.Information);
}
public static void WriteWarning(string content)
{
EventLogger.WriteLog(content, EventLogEntryType.Warning);
}
public static void WriteError(string content)
{
EventLogger.WriteLog(content, EventLogEntryType.Error);
}
private static void WriteLog(string content, EventLogEntryType logType)
{
int eventId;
switch (logType)
{
case EventLogEntryType.Warning:
eventId = WarningEventId;
break;
case EventLogEntryType.Error:
eventId = ErrorEventId;
break;
case EventLogEntryType.Information:
default:
eventId = ErrorEventId;
break;
}
EventLog.WriteEntry(SourceName, content, logType, eventId);
}
}
}