本文介绍了在获取使用C#,每天服务器重新启动计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用C#来获得在一段时间内的次一台服务器被重启多少?

Is it possible to get the number of times a server gets restarted in a period of time using c#?

我看到这个帖子这是越来越窗口上次关闭去年Windows关机的日期时间事件中使用.NET

I have seen this post which is getting windows last shutdown time Get the date-time of last windows shutdown event using .NET

任何建议?

推荐答案

你有没有考虑从服务器的事件日志读取?

Have you considered reading from the server's event log?

在'USER32'的系统事件源记录停工。

The 'USER32' system event source records shutdowns.

从我读过它似乎,你应该能够读取远程计算机的事件日志编程以及(的)

From what I've read it seem that you should be able to read a remote machine's event log programmatically as well (See How to manage event logs using Visual C# .NET or Visual C# 2005)

修改

下面的控制台应用程序,将有关查询,即使登录了USER32类型的远程计算机上运行。

The following console app will work on querying the even log on a remote machine for the USER32 Type.

所有你需要做的就是插上日期/时间的比较,并添加您的服务器名称。其有点粗糙,并准备好了,但我敢肯定,你可以美化了一点,如果你想

All you have to do is plug in the date/time comparison and add your server name. Its a bit rough and ready, but I'm sure you can beautify it a bit if you want.

using System;
using System.Diagnostics;

namespace ReadEventLog
{
    class Program
    {
        static void Main(string[] args)
        {

            string logType = "System";

            //use this if your are are running the app on the server
            //EventLog ev = new EventLog(logType, System.Environment.MachineName);

            //use this if you are running the app remotely
            EventLog ev = new EventLog(logType, "[youservername]");

            if (ev.Entries.Count <= 0)
                Console.WriteLine("No Event Logs in the Log :" + logType);

            // Loop through the event log records.
            for (int i = ev.Entries.Count - 1; i >= 0; i--)
            {
                EventLogEntry CurrentEntry = ev.Entries[i];

                //use DateTime type to compare on CurrentEntry.TimeGenerated
                DateTime dt = DateTime.Now;
                TimeSpan ts = dt.Subtract( CurrentEntry.TimeGenerated);
                int hours = (ts.Days * 24) + ts.Hours;

                if (CurrentEntry.Source.ToUpper() == "USER32")
                {
                    Console.WriteLine("Time Generated:" + CurrentEntry.TimeGenerated);
                    Console.WriteLine("Hours ago:" + hours);
                    Console.WriteLine("Event ID : " + CurrentEntry.InstanceId);
                    Console.WriteLine("Entry Type : " + CurrentEntry.EntryType.ToString());
                    Console.WriteLine("Message :  " + CurrentEntry.Message + "\n");
                }
            }
            ev.Close();
        }
    }
}

这篇关于在获取使用C#,每天服务器重新启动计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 12:32