码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-05-15 18:28:29      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:private   new   rate   blog   type   each   empty   来源   env   

 

 

测试

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事件日志)

标签:private   new   rate   blog   type   each   empty   来源   env   

原文地址:http://www.cnblogs.com/xmai/p/6857379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!