Skip to content

Commit

Permalink
Write error events for service errors (#39766)
Browse files Browse the repository at this point in the history
* Write error events for service errors

* Explicitly specify EventLogEntryType.Error
  • Loading branch information
nxtn authored Jul 23, 2020
1 parent 73f0750 commit 0bed2de
Showing 1 changed file with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ private unsafe void DeferredContinue()
catch (Exception e)
{
_status.currentState = ServiceControlStatus.STATE_PAUSED;
WriteLogEntry(SR.Format(SR.ContinueFailed, e), true);
WriteLogEntry(SR.Format(SR.ContinueFailed, e), EventLogEntryType.Error);

// We re-throw the exception so that the advapi32 code can report
// ERROR_EXCEPTION_IN_SERVICE as it would for native services.
Expand All @@ -434,7 +434,7 @@ private void DeferredCustomCommand(int command)
}
catch (Exception e)
{
WriteLogEntry(SR.Format(SR.CommandFailed, e), true);
WriteLogEntry(SR.Format(SR.CommandFailed, e), EventLogEntryType.Error);

// We should re-throw the exception so that the advapi32 code can report
// ERROR_EXCEPTION_IN_SERVICE as it would for native services.
Expand All @@ -455,7 +455,7 @@ private unsafe void DeferredPause()
catch (Exception e)
{
_status.currentState = ServiceControlStatus.STATE_RUNNING;
WriteLogEntry(SR.Format(SR.PauseFailed, e), true);
WriteLogEntry(SR.Format(SR.PauseFailed, e), EventLogEntryType.Error);

// We re-throw the exception so that the advapi32 code can report
// ERROR_EXCEPTION_IN_SERVICE as it would for native services.
Expand All @@ -482,7 +482,7 @@ private void DeferredPowerEvent(int eventType, IntPtr eventData)
}
catch (Exception e)
{
WriteLogEntry(SR.Format(SR.PowerEventFailed, e), true);
WriteLogEntry(SR.Format(SR.PowerEventFailed, e), EventLogEntryType.Error);

// We rethrow the exception so that advapi32 code can report
// ERROR_EXCEPTION_IN_SERVICE as it would for native services.
Expand All @@ -498,7 +498,7 @@ private void DeferredSessionChange(int eventType, int sessionId)
}
catch (Exception e)
{
WriteLogEntry(SR.Format(SR.SessionChangeFailed, e), true);
WriteLogEntry(SR.Format(SR.SessionChangeFailed, e), EventLogEntryType.Error);

// We rethrow the exception so that advapi32 code can report
// ERROR_EXCEPTION_IN_SERVICE as it would for native services.
Expand Down Expand Up @@ -530,7 +530,7 @@ private unsafe void DeferredStop()
{
_status.currentState = previousState;
SetServiceStatus(_statusHandle, pStatus);
WriteLogEntry(SR.Format(SR.StopFailed, e), true);
WriteLogEntry(SR.Format(SR.StopFailed, e), EventLogEntryType.Error);
throw;
}
}
Expand All @@ -556,7 +556,7 @@ private unsafe void DeferredShutdown()
}
catch (Exception e)
{
WriteLogEntry(SR.Format(SR.ShutdownFailed, e), true);
WriteLogEntry(SR.Format(SR.ShutdownFailed, e), EventLogEntryType.Error);
throw;
}
}
Expand Down Expand Up @@ -641,7 +641,7 @@ public static unsafe void Run(ServiceBase[] services)
service.Dispose();
if (!res)
{
service.WriteLogEntry(SR.Format(SR.StartFailed, errorMessage), true);
service.WriteLogEntry(SR.Format(SR.StartFailed, errorMessage), EventLogEntryType.Error);
}
}
}
Expand Down Expand Up @@ -847,7 +847,7 @@ private void ServiceQueuedMainCallback(object state)
}
catch (Exception e)
{
WriteLogEntry(SR.Format(SR.StartFailed, e), true);
WriteLogEntry(SR.Format(SR.StartFailed, e), EventLogEntryType.Error);
_status.currentState = ServiceControlStatus.STATE_STOPPED;

// We capture the exception so that it can be propagated
Expand Down Expand Up @@ -900,7 +900,7 @@ public unsafe void ServiceMainCallback(int argCount, IntPtr argPointer)
if (_statusHandle == (IntPtr)0)
{
string errorMessage = new Win32Exception().Message;
WriteLogEntry(SR.Format(SR.StartFailed, errorMessage), true);
WriteLogEntry(SR.Format(SR.StartFailed, errorMessage), EventLogEntryType.Error);
}

_status.controlsAccepted = _acceptedCommands;
Expand Down Expand Up @@ -943,21 +943,21 @@ public unsafe void ServiceMainCallback(int argCount, IntPtr argPointer)
statusOK = SetServiceStatus(_statusHandle, pStatus);
if (!statusOK)
{
WriteLogEntry(SR.Format(SR.StartFailed, new Win32Exception().Message), true);
WriteLogEntry(SR.Format(SR.StartFailed, new Win32Exception().Message), EventLogEntryType.Error);
_status.currentState = ServiceControlStatus.STATE_STOPPED;
SetServiceStatus(_statusHandle, pStatus);
}
}
}

private void WriteLogEntry(string message, bool error = false)
private void WriteLogEntry(string message, EventLogEntryType type = EventLogEntryType.Information)
{
// EventLog failures shouldn't affect the service operation
try
{
if (AutoLog)
{
EventLog.WriteEntry(message);
EventLog.WriteEntry(message, type);
}
}
catch
Expand Down

0 comments on commit 0bed2de

Please sign in to comment.