C# EventLog类应用

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


 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值