runtime icon indicating copy to clipboard operation
runtime copied to clipboard

WindowsService: Event log - Error events are reported as info events falsely (System.ServiceProcess.ServiceController)

Open eschgi opened this issue 6 years ago • 4 comments

Description

We use the System.ServiceProcess.ServiceBase to run our applications as windows service. During development we found out that error events are reported as info events falsely.

The bug can be found/fixed inside the WriteLogEntry function. The fix should be easy.

private void WriteLogEntry(string message, bool error = false)
{
    // EventLog failures shouldn't affect the service operation
    try
    {
        if (AutoLog)
        {
+            EventLogEntryType eventLogEntryType = error ? EventLogEntryType.Error : EventLogEntryType.Information;
+            EventLog.WriteEntry(message, eventLogEntryType);
-            EventLog.WriteEntry(message);
        }
    }
    catch
    {
        // Do nothing.  Not having the event log is bad, but not starting the service as a result is worse.
    }
}

Configuration

Which version of .NET is the code running on?

  • .NET Core 3.1

What OS and version, and what distro if applicable?

  • Windows 10

What is the architecture (x64, x86, ARM, ARM64)?

  • x64

Regression?

Did this work in a previous build or release of .NET Core, or from .NET Framework?

Yes it works with .NET Framework. I also had a look at the reference source code and there that bug does not exist.

To log the error events the following function is used inside the reference source code. The event log entry type is passed there as argument.

  • Code copied from: https://github.com/microsoft/referencesource/blob/master/System.ServiceProcess/ServiceBase.cs
private void WriteEventLogEntry (string message, EventLogEntryType errorType) 
{
    //EventLog failures shouldn't affect the service operation
    try 
    {
        if (AutoLog)
            this.EventLog.WriteEntry (message, errorType);
    }
    #region Stuff not to catch
    catch (StackOverflowException) 
    {
        throw;
    }
    catch (OutOfMemoryException) 
    {
        throw;
    }
    catch (ThreadAbortException) 
    {
        throw;
    }
    #endregion
    catch  
    {
        // Do nothing.  Not having the event log is bad, but not starting the service as a result is worse.
    }
}

Thanks in the meantime.

Kind regards, Stefan

eschgi avatar Jun 03 '20 17:06 eschgi

I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

Dotnet-GitSync-Bot avatar Jun 03 '20 17:06 Dotnet-GitSync-Bot

I looked at the old code (which for some reason is not on referencesource) and I agree. Do you want to offer a PR? Not sure whether there are tests for this method or not, but if there are we should make sure they check it.

danmoseley avatar Jun 03 '20 21:06 danmoseley

Apologies for somehow missing this. It's a duplicate of https://github.com/dotnet/runtime/pull/39766. Arbitrarily taking the other one since CI just ran on it. Sorry about that and thanks for the fix.

danmoseley avatar Jul 23 '20 19:07 danmoseley

Fixed by #39766 (and #37513!)

danmoseley avatar Jul 23 '20 19:07 danmoseley