/// <summary>
///EventLogClass 的摘要说明
/// </summary>
public class EventLogClass
{
public EventLogClass()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 创建日志
/// </summary>
/// <param name="sourcename">源名称</param>
/// <param name="logname">日志名</param>
/// <returns>是否成功</returns>
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;
}
}
/// <summary>
/// 日志源是否存在
/// </summary>
/// <param name="sourcename">源名称</param>
/// <returns>是否存在</returns>
public static bool ExistsSourceEvent(string sourcename)
{
if (System.Diagnostics.EventLog.SourceExists(sourcename))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 删除日志源
/// </summary>
/// <param name="sourcename">源名称</param>
/// <returns>是否成功</returns>
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;
}
}
/// <summary>
/// 写入日志
/// </summary>
/// <param name="sourcename">源名称</param>
/// <param name="logname">日志名</param>
/// <param name="logmes">日志信息</param>
/// <param name="logtype">日志类型:1-错误事件,2-失败审核事件,3-信息事件,4-成功审核事件,5-警告事件</param>
/// <param name="eventID">事件ID</param>
/// <param name="category">类别</param>
/// <returns>是否成功</returns>
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;
}
}
/// <summary>
/// 创建事件实例
/// </summary>
/// <param name="sourcename">源名称</param>
/// <param name="logname">日志名</param>
/// <returns>返回事件实例</returns>
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;
}
/*
/// <summary>
/// 读取日志
/// </summary>
/// <param name="sourcename">源名称</param>
/// <param name="logname">日志名</param>
/// <returns>是否成功</returns>
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;
}
}*/
/// <summary>
/// 删除日志
/// </summary>
/// <param name="sourcename">源名称</param>
/// <param name="logname">日志名</param>
/// <returns>是否成功</returns>
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
{
/// <summary>
/// 事件源名称
/// </summary>
private string eventSourceName;
EventLogEntryType eventLogType;
public LogWirter()
{
eventSourceName = "test";
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, EventLogEntryType.Error);
}
}
}