3

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

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

Any suggestion?

Community
  • 1
  • 1
bala3569
  • 10,832
  • 28
  • 102
  • 146

2 Answers2

3

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

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)

EDIT

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();
        }
    }
}
James Wiseman
  • 29,946
  • 17
  • 95
  • 158
  • If i need it for last 24 hours means what should i do – bala3569 May 31 '11 at 10:08
  • Edited question. You need to plug in your server name if you are running remotely, or use `EventLog ev = new EventLog(logType, System.Environment.MachineName);` if locally. – James Wiseman May 31 '11 at 10:15
  • Also edited to include 'hours ago'. You shoudl be able to figure it out from there. – James Wiseman May 31 '11 at 10:25
  • @bala3569: Seriously? You're looping round each item, and you're not sure how to get the count? How about incrementing a counter, or replacing the loop with a LINQ expression and getting the length? Have a think yourself. Part of the learning experience is trying something yourself and making mistakes. I knew nothing of this before I read the question. – James Wiseman May 31 '11 at 11:02
  • 1
    I don't mean to be unhelpful, but you have the time of the event, and you can easily get the current time. It shoudl be very trivial to work it out from this. Programming is about solving problems youself, as well as getting help from others. While I'm happy to help with a solution, I'm not really wanting to completely write your code for you so you so you can paste it verbatim into your program. – James Wiseman Jun 03 '11 at 14:10
0

You could create a windows service and log the startup event. That way you will know how many times the service has started (and been shut down).

Mharlin
  • 1,697
  • 16
  • 24