C# EventLog类应用

/// 
///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);
         }
     }
 }


 

 

 

 

你可能感兴趣的:(Asp.net)