Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions from EventRecordWrittenEventArgs are missing the original win32 error code #67702

Closed
wokis opened this issue Apr 7, 2022 · 4 comments · Fixed by #70629
Closed
Labels
area-System.Diagnostics.EventLog help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@wokis
Copy link

wokis commented Apr 7, 2022

Description

One can retrieve a HResult error code from System.Diagnostics.Eventing.Reader.EventRecordWrittenEventArgs.EventException but it has no connection to the underlying win32 error code from the EvtNext function used (https://github.com/microsoft/referencesource/blob/master/System.Core/System/Diagnostics/Eventing/Reader/EventLogWatcher.cs#L228).

For example subscribing to an non existing log channel gives the error The handle is invalid. but the HResult code is -2146233088 (0x80131500). The win32 API error code for ERROR_INVALID_HANDLE is 6 (0x6).

Reproduction Steps

The MRE below subscribes to a non existing channel yielding an exception in the Watcher_EventRecordWritten handler.

private static void Test1()
{
    var query = new EventLogQuery(null, PathType.LogName, "<QueryList><Query Id=\"0\"><Select Path=\"non-existing-channel\">*</Select></Query></QueryList>");

    var watcher = new EventLogWatcher(query);
    watcher.EventRecordWritten += Watcher_EventRecordWritten;
    watcher.Enabled = true;
    EventLog.WriteEntry("dummy source", "Dummy test message");
    Thread.Sleep(1000 * 60);
    watcher.Enabled = false;
    Console.WriteLine("Test 1 completed");
}

private static void Watcher_EventRecordWritten(object sender, EventRecordWrittenEventArgs e)
{
    if (e.EventRecord == null) {
        Console.WriteLine(e.EventException.Message);
        Console.WriteLine(e.EventException.HResult);
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(string.Format("HRESULT:0x{0:X8}", e.EventException.HResult));
        Console.ForegroundColor = ConsoleColor.White;
    } else
    {
        Console.WriteLine(e.EventRecord.FormatDescription());
        e.EventRecord.Dispose();
    }
}

Expected behavior

The HResult from a EventRecordWrittenEventArgs.EventException should match the win32 error code returned from the EvtNext function (https://github.com/microsoft/referencesource/blob/5697c29004a34d80acdaf5742d7e699022c64ecd/System.Core/System/Diagnostics/Eventing/Reader/EventLogWatcher.cs#L228).

Actual behavior

The HResult from a EventRecordWrittenEventArgs.EventException does not match the actual error message.
All EventRecordWrittenEventArgs.EventException seem to return the same HResult and the EventException is of the base type Exception so there is no way to different between different errors.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6
Windows 10 21H2 (OS Build 19043.1586)
x64

Other information

No response

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Apr 7, 2022
@ghost
Copy link

ghost commented Apr 7, 2022

Tagging subscribers to this area: @tarekgh, @tommcdon, @pjanotti
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

One can retrieve a HResult error code from System.Diagnostics.Eventing.Reader.EventRecordWrittenEventArgs.EventException but it has no connection to the underlying win32 error code from the EvtNext function used (https://github.com/microsoft/referencesource/blob/master/System.Core/System/Diagnostics/Eventing/Reader/EventLogWatcher.cs#L228).

For example subscribing to an non existing log channel gives the error The handle is invalid. but the HResult code is -2146233088 (0x80131500). The win32 API error code for ERROR_INVALID_HANDLE is 6 (0x6).

Reproduction Steps

The MRE below subscribes to a non existing channel yielding an exception in the Watcher_EventRecordWritten handler.

private static void Test1()
{
    var query = new EventLogQuery(null, PathType.LogName, "<QueryList><Query Id=\"0\"><Select Path=\"non-existing-channel\">*</Select></Query></QueryList>");

    var watcher = new EventLogWatcher(query);
    watcher.EventRecordWritten += Watcher_EventRecordWritten;
    watcher.Enabled = true;
    EventLog.WriteEntry("dummy source", "Dummy test message");
    Thread.Sleep(1000 * 60);
    watcher.Enabled = false;
    Console.WriteLine("Test 1 completed");
}

private static void Watcher_EventRecordWritten(object sender, EventRecordWrittenEventArgs e)
{
    if (e.EventRecord == null) {
        Console.WriteLine(e.EventException.Message);
        Console.WriteLine(e.EventException.HResult);
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(string.Format("HRESULT:0x{0:X8}", e.EventException.HResult));
        Console.ForegroundColor = ConsoleColor.White;
    } else
    {
        Console.WriteLine(e.EventRecord.FormatDescription());
        e.EventRecord.Dispose();
    }
}

Expected behavior

The HResult from a EventRecordWrittenEventArgs.EventException should match the win32 error code returned from the EvtNext function (https://github.com/microsoft/referencesource/blob/5697c29004a34d80acdaf5742d7e699022c64ecd/System.Core/System/Diagnostics/Eventing/Reader/EventLogWatcher.cs#L228).

Actual behavior

The HResult from a EventRecordWrittenEventArgs.EventException does not match the actual error message.
All EventRecordWrittenEventArgs.EventException seem to return the same HResult and the EventException is of the base type Exception so there is no way to different between different errors.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6
Windows 10 21H2 (OS Build 19043.1586)
x64

Other information

No response

Author: wokis
Assignees: -
Labels:

area-System.Diagnostics.Tracing, untriaged

Milestone: -

@wokis
Copy link
Author

wokis commented Apr 7, 2022

I see that the EventLogException (EventLogException.cs#L23) actually converts the errorCode to exceptions but as error 6 isn't among those converted the error code is lost since there is no method from which to retrieve the error code.

@ghost
Copy link

ghost commented Apr 7, 2022

Tagging subscribers to this area: @dotnet/area-system-diagnostics-eventlog
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

One can retrieve a HResult error code from System.Diagnostics.Eventing.Reader.EventRecordWrittenEventArgs.EventException but it has no connection to the underlying win32 error code from the EvtNext function used (https://github.com/microsoft/referencesource/blob/master/System.Core/System/Diagnostics/Eventing/Reader/EventLogWatcher.cs#L228).

For example subscribing to an non existing log channel gives the error The handle is invalid. but the HResult code is -2146233088 (0x80131500). The win32 API error code for ERROR_INVALID_HANDLE is 6 (0x6).

Reproduction Steps

The MRE below subscribes to a non existing channel yielding an exception in the Watcher_EventRecordWritten handler.

private static void Test1()
{
    var query = new EventLogQuery(null, PathType.LogName, "<QueryList><Query Id=\"0\"><Select Path=\"non-existing-channel\">*</Select></Query></QueryList>");

    var watcher = new EventLogWatcher(query);
    watcher.EventRecordWritten += Watcher_EventRecordWritten;
    watcher.Enabled = true;
    EventLog.WriteEntry("dummy source", "Dummy test message");
    Thread.Sleep(1000 * 60);
    watcher.Enabled = false;
    Console.WriteLine("Test 1 completed");
}

private static void Watcher_EventRecordWritten(object sender, EventRecordWrittenEventArgs e)
{
    if (e.EventRecord == null) {
        Console.WriteLine(e.EventException.Message);
        Console.WriteLine(e.EventException.HResult);
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(string.Format("HRESULT:0x{0:X8}", e.EventException.HResult));
        Console.ForegroundColor = ConsoleColor.White;
    } else
    {
        Console.WriteLine(e.EventRecord.FormatDescription());
        e.EventRecord.Dispose();
    }
}

Expected behavior

The HResult from a EventRecordWrittenEventArgs.EventException should match the win32 error code returned from the EvtNext function (https://github.com/microsoft/referencesource/blob/5697c29004a34d80acdaf5742d7e699022c64ecd/System.Core/System/Diagnostics/Eventing/Reader/EventLogWatcher.cs#L228).

Actual behavior

The HResult from a EventRecordWrittenEventArgs.EventException does not match the actual error message.
All EventRecordWrittenEventArgs.EventException seem to return the same HResult and the EventException is of the base type Exception so there is no way to different between different errors.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6
Windows 10 21H2 (OS Build 19043.1586)
x64

Other information

No response

Author: wokis
Assignees: -
Labels:

area-System.Diagnostics.Tracing, untriaged, area-System.Diagnostics.EventLog

Milestone: -

@carlossanlop carlossanlop added this to the 7.0.0 milestone Jun 1, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jun 1, 2022
@carlossanlop carlossanlop added the help wanted [up-for-grabs] Good issue for external contributors label Jun 1, 2022
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jun 12, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jun 19, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jul 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Diagnostics.EventLog help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants