Write error events for service errors (#39766)
authorNext Turn <45985406+NextTurn@users.noreply.github.com>
Thu, 23 Jul 2020 19:10:43 +0000 (03:10 +0800)
committerGitHub <noreply@github.com>
Thu, 23 Jul 2020 19:10:43 +0000 (12:10 -0700)
* Write error events for service errors

* Explicitly specify EventLogEntryType.Error

src/libraries/System.ServiceProcess.ServiceController/src/System/ServiceProcess/ServiceBase.cs

index 00a392a..f4ef7e7 100644 (file)
@@ -412,7 +412,7 @@ namespace System.ServiceProcess
                 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.
@@ -434,7 +434,7 @@ namespace System.ServiceProcess
             }
             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.
@@ -455,7 +455,7 @@ namespace System.ServiceProcess
                 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.
@@ -482,7 +482,7 @@ namespace System.ServiceProcess
             }
             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.
@@ -498,7 +498,7 @@ namespace System.ServiceProcess
             }
             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.
@@ -530,7 +530,7 @@ namespace System.ServiceProcess
                 {
                     _status.currentState = previousState;
                     SetServiceStatus(_statusHandle, pStatus);
-                    WriteLogEntry(SR.Format(SR.StopFailed, e), true);
+                    WriteLogEntry(SR.Format(SR.StopFailed, e), EventLogEntryType.Error);
                     throw;
                 }
             }
@@ -556,7 +556,7 @@ namespace System.ServiceProcess
             }
             catch (Exception e)
             {
-                WriteLogEntry(SR.Format(SR.ShutdownFailed, e), true);
+                WriteLogEntry(SR.Format(SR.ShutdownFailed, e), EventLogEntryType.Error);
                 throw;
             }
         }
@@ -641,7 +641,7 @@ namespace System.ServiceProcess
                     service.Dispose();
                     if (!res)
                     {
-                        service.WriteLogEntry(SR.Format(SR.StartFailed, errorMessage), true);
+                        service.WriteLogEntry(SR.Format(SR.StartFailed, errorMessage), EventLogEntryType.Error);
                     }
                 }
             }
@@ -847,7 +847,7 @@ namespace System.ServiceProcess
             }
             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
@@ -900,7 +900,7 @@ namespace System.ServiceProcess
                 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;
@@ -943,21 +943,21 @@ namespace System.ServiceProcess
                 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