I have a windows service installed, it is running.
I have a write to event log entry, when the system is shutdown, however the code doesn't write to event log on system shut down or restart.
protected override void OnShutdown()
{
WriteToEventLog("Services shut down.");
}
private void WriteToEventLog(string logMessage)
{
var mySource = "MySourceLog";
// Create the source and log, if it does not already exist.
if (!EventLog.SourceExists(mySource))
{
EventLog.CreateEventSource(mySource, mySource);
}
// Create an EventLog instance and assign its source.
using (var eventLog = new EventLog())
{
eventLog.Source = mySource;
// Write an entry to the event log.
eventLog.WriteEntry(logMessage, EventLogEntryType.Warning, 1002);
}
}
I want to write an event into event log whenever my windows service crashes and possibly send an email as an alert saying the service crashed. How can i detect that service has been crashed and write it to event log?