统计Windows系统日志

服务器要求定期巡检,并记录日志发生的次数。

代码如下:

  static void Main(string[] args)
    {
            Console.WriteLine("请输入检索的日志月份(如2023-01):");
            var month =  Console.ReadLine();
            new EventWatch(month).OutputSystemLogs();
            Console.ReadLine();
    }

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; 

namespace SystemEventClassLibrary
{
    public class EventWatch
    {
        Stopwatch sw = new Stopwatch();

        readonly DateTime dateFrom = new DateTime();
        readonly   DateTime dateTo = new DateTime();

        Dictionary<long, int> EventResult = new Dictionary<long, int>();//{ { 4624,0}, { 4625, 0 }, { 4719, 0 }, { 4698, 0 }, { 1102, 0 }, };
 
        public EventWatch(string month )
        {
      

            var watchEventID = System.Configuration.ConfigurationManager.AppSettings["EventID"];

            var _eventList = watchEventID.Split(new char[] { ',' });
            foreach ( var _eventID in _eventList ) {

                EventResult.Add( long.Parse( _eventID), 0);
            }

            var fromDay = System.DateTime.Now.ToString("yyyy-MM-01");
            if (!string.IsNullOrEmpty(month))
            {
                if (month.Length == 7)
                {
                    fromDay = string.Format("{0}-01", month);
                }
                else if (month.Length == 6) {
                    fromDay = string.Format("{0}-{1}-01", month.Substring(0,4), month.Substring(4,2));
                }
            }

            dateFrom = DateTime.Parse(fromDay);

            dateTo = dateFrom.AddMonths(1);

        }

        public   void OutputSystemLogs()
        {
            try
            {
                sw.Start();

                var logName = System.Configuration.ConfigurationManager.AppSettings["EventName"];//

                System.Console.WriteLine(string.Format("开始检索{0}日志,{1}~{2},事件ID({3})",
                    logName,
                    dateFrom, dateTo,
                    System.Configuration.ConfigurationManager.AppSettings["EventID"]));

                System.Diagnostics.EventLog eventLogSecurity = new System.Diagnostics.EventLog();

                if (!string.IsNullOrEmpty(logName))
                {
                    eventLogSecurity.Log = logName;
                }


                System.Diagnostics.EventLogEntryCollection collection = eventLogSecurity.Entries;


                int Count = collection.Count;

                if (collection != null && collection.Count > 0)
                {

                    foreach (System.Diagnostics.EventLogEntry entry in collection)
                    {
                        if (DateTime.Compare(entry.TimeGenerated, dateFrom) >= 0 &&
                            DateTime.Compare(entry.TimeGenerated, dateTo) < 0
                            ) 
                        { 
                            if (EventResult.Keys.Count(t => t == entry.InstanceId) > 0)
                            {
                                EventResult[entry.InstanceId]++;
                            }
                        }
                        else
                        {
                            //break;
                        }
                    }
                    
                    OutPut();
                }

            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);

            }
            finally {

                sw.Stop(); 

                System.Console.WriteLine( string.Format("运行结束,耗时:{0} 毫秒", sw.ElapsedMilliseconds.ToString()));

            }

        }

        private   void OutPut()
        {
            string content = string.Format("系统日志巡检周期:{0}至{1}",  dateFrom, dateTo);
            for (int i = 0; i < EventResult.Count; i++)
            {
                var key = EventResult.ElementAt(i).Key;

                var curLing = string.Format("事件ID:{0} 数量:{1}", key, EventResult == null ? 0 : EventResult[key]);
                content += Environment.NewLine + curLing;
                System.Console.WriteLine(curLing);
            }

            string fileName = System.IO.Path.Combine(Environment.CurrentDirectory, System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + "log.txt");
            System.IO.File.WriteAllText(fileName, content);
        }
    }
}
 

运行画面如下:

exe下载地址:https://download.csdn.net/download/moonshineidolon/87932664

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值