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?

2 Answers 2

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();
        }
    }
}
5
  • If i need it for last 24 hours means what should i do
    – bala3569
    Commented May 31, 2011 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. Commented May 31, 2011 at 10:15
  • Also edited to include 'hours ago'. You shoudl be able to figure it out from there. Commented May 31, 2011 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. Commented May 31, 2011 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. Commented Jun 3, 2011 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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.