首页 > 代码库 > 深入解密.NET(Windows事件日志)

深入解密.NET(Windows事件日志)

 

 

测试

using System;
using System.Diagnostics;

namespace WindowsConsoleApp
{
    //测试
    public class EnventLogHelper
    {
        private EventLog log;

        public EnventLogHelper()
        {
            log = new EventLog();//默认写应用程序日志
        }
        public EnventLogHelper(string name)
        {
            log = new EventLog(name);//指定写入的分类,用户自定义则新建分组。系统保留//"Application"应用程序, "Security"安全, "System"系统
            //或者可以用 log.Log = "Security";指定
        }



        public void WriteToApp()
        {
            try
            {

                log.Source = "我的应用程序";//日志来源
                log.WriteEntry("处理信息1", EventLogEntryType.Information);//日志类型
                log.WriteEntry("处理信息2", EventLogEntryType.Information);
                throw new System.IO.FileNotFoundException("readme.txt文件未找到");
            }
            catch (System.IO.FileNotFoundException exception)
            {
                log.WriteEntry(exception.Message, EventLogEntryType.Error);

            }
        }

        public void ReadLog()
        {
            EventLogEntryCollection eventLogEntryCollection = log.Entries;//获取日志collection
            foreach (EventLogEntry entry in eventLogEntryCollection)
            {
                
                string info = string.Empty;

                info += "【类型】:" + entry.EntryType.ToString() + ";";
                info += "【日期】" + entry.TimeGenerated.ToLongDateString() + ";";
                info += "【时间】" + entry.TimeGenerated.ToLongTimeString() + ";";

                info += "【计算机】" + entry.MachineName + "【来源】" + entry.Source + "【详细信息】" + entry.Message + "【】";
                //
                Console.WriteLine(info);

            }
        }


    }
}

 

 

 

 

 

 

 

 

 

 

资源:

https://referencesource.microsoft.com/#System/services/monitoring/system/diagnosticts/EventLog.cs

https://msdn.microsoft.com/zh-cn/library/system.diagnostics.eventlog(v=vs.110).aspx

 

深入解密.NET(Windows事件日志)