C#EventLogクラスアプリケーション
7978 ワード
///
///EventLogClass
///
public class EventLogClass
{
public EventLogClass()
{
//
//TODO:
//
}
///
///
///
///
///
///
public static bool CreateEvent(string sourcename, string logname)
{
try
{
if (DeleteSourceEvent(sourcename))
{
// Logs and Sources are created as a pair.
System.Diagnostics.EventLog.CreateEventSource(sourcename, logname);
System.Diagnostics.EventLog eventLog1 = setEvent(sourcename, logname);
return true;
}
else
{
return false;
}
}
catch (Exception)
{
return false;
throw;
}
}
///
///
///
///
///
public static bool ExistsSourceEvent(string sourcename)
{
if (System.Diagnostics.EventLog.SourceExists(sourcename))
{
return true;
}
else
{
return false;
}
}
///
///
///
///
///
public static bool DeleteSourceEvent(string sourcename)
{
try
{
if (System.Diagnostics.EventLog.SourceExists(sourcename))
{
System.Diagnostics.EventLog.DeleteEventSource(sourcename);
}
return true;
}
catch (Exception)
{
return false;
throw;
}
}
///
///
///
///
///
///
/// :1- ,2- ,3- ,4- ,5-
/// ID
///
///
public static bool WriteEvent(string sourcename, string logname, string logmes, int logtype, int eventID, short category)
{
if (!ExistsSourceEvent(sourcename))
{
CreateEvent(sourcename, logname);
}
System.Diagnostics.EventLog eventLog1 = setEvent(sourcename, logname);
if (System.Diagnostics.EventLog.Exists(logname))
{
try
{
switch (logtype)
{
case 1:
eventLog1.WriteEntry(logmes, System.Diagnostics.EventLogEntryType.Error, eventID, category);//
break;
case 2:
eventLog1.WriteEntry(logmes, System.Diagnostics.EventLogEntryType.FailureAudit,eventID, category);//
break;
case 3:
eventLog1.WriteEntry(logmes, System.Diagnostics.EventLogEntryType.Information, eventID, category);//
break;
case 4:
eventLog1.WriteEntry(logmes, System.Diagnostics.EventLogEntryType.SuccessAudit, eventID, category);//
break;
case 5:
eventLog1.WriteEntry(logmes, System.Diagnostics.EventLogEntryType.Warning, eventID, category);//
break;
default:
eventLog1.WriteEntry(logmes);
break;
}
return true;
}
catch (Exception)
{
return false;
throw;
}
}
else
{
return false;
}
}
///
///
///
///
///
///
public static System.Diagnostics.EventLog setEvent(string sourcename, string logname)
{
System.Diagnostics.EventLog eventLog1 = new System.Diagnostics.EventLog();
eventLog1.Log = logname;
eventLog1.Source = sourcename;
eventLog1.MachineName = ".";
return eventLog1;
}
/*
///
///
///
///
///
///
private static bool MessageEvent(string sourcename, string logname)
{
System.Diagnostics.EventLog eventLog1 = setEvent(sourcename, logname);
if (eventLog1.Entries.Count > 0)
{
try
{
foreach (System.Diagnostics.EventLogEntry entry
in eventLog1.Entries)
{
MessageBox.Show(entry.Message);
}
return true;
}
catch (Exception)
{
return false;
throw;
}
}
else
{
return false;
}
}*/
///
///
///
///
///
///
public static bool ClearEvent(string sourcename, string logname)
{
System.Diagnostics.EventLog eventLog1 = setEvent(sourcename, logname);
if (System.Diagnostics.EventLog.Exists(logname))
{
try
{
eventLog1.Clear();
return true;
}
catch (Exception)
{
return false;
throw;
}
}
else
{
return false;
}
}
}
単純な方法
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace Log
{
class LogWirter
{
///
///
///
private string eventSourceName;
EventLogEntryType eventLogType;
public LogWirter()
{
eventSourceName = "test";
eventLogType = EventLogEntryType.Error;
}
///
///
///
public string EventSourceName
{
set { eventSourceName = value; }
}
///
///
///
public EventLogEntryType EventLogType
{
set { eventLogType = value; }
}
///
///
///
///
public void LogEvent(string message)
{
if (!EventLog.SourceExists(eventSourceName))
{
EventLog.CreateEventSource(eventSourceName, "Application");
}
EventLog.WriteEntry(eventSourceName, message, EventLogEntryType.Error);
}
}
}