C#寫入windows日誌
來源:互聯網
上載者:User
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace GPSFileMonitor
{
public class LogWirter
{
/**//// <summary>
/// 事件來源名稱
/// </summary>
private string eventSourceName;
EventLogEntryType eventLogType;
public LogWirter()
{
eventSourceName = "GPSServer";
eventLogType = EventLogEntryType.Error;
}
/**//// <summary>
/// 訊息事件來源名稱
/// </summary>
public string EventSourceName
{
set { eventSourceName = value; }
}
/**//// <summary>
/// 訊息事件類型
/// </summary>
public EventLogEntryType EventLogType
{
set { eventLogType = value; }
}
/**//// <summary>
/// 寫入系統日誌
/// </summary>
/// <param name="message">事件內容</param>
public void LogEvent(string message)
{
if (!EventLog.SourceExists(eventSourceName))
{
EventLog.CreateEventSource(eventSourceName, "Application");
}
EventLog.WriteEntry(eventSourceName, message, eventLogType);
}
public void LogEvent(string message,EventLogEntryType eventLogType)
{
if (!EventLog.SourceExists(eventSourceName))
{
EventLog.CreateEventSource(eventSourceName, "Application");
}
EventLog.WriteEntry(eventSourceName, message,eventLogType);
}
}
}