exceptionless / Exceptionless.Net

Exceptionless clients for the .NET platform
https://exceptionless.com
Other
551 stars 142 forks source link

Exception.HResult is not being mapped to error.code property #273

Closed niemyjski closed 2 years ago

niemyjski commented 2 years ago

HResult doesn't seem to be picked up properly via reflection in the ToError extensions. This could be due to it being a public property and the reflection is checking that it's private: https://docs.microsoft.com/en-us/dotnet/api/system.exception.hresult?msclkid=5283e541cf9b11ec938e45f7252349bc&view=net-6.0

Starting with the .NET Framework 4.5, the HResult property's setter is protected, whereas its getter is public. In previous versions of the .NET Framework, both getter and setter are protected.

I'm thinking that since the library now targets .NET 4.5 and above, we can change our implementation to just check public.

    public class MyException : Exception {
        public TestOutOfMemoryException(string message) : base(message) {
            HResult = unchecked((int)0x8007000E);
        }
    }

    [Fact]
    private static void TrackException() {
        throw new TestOutOfMemoryException("Test");
        throw new OutOfMemoryException("Test");
    }
niemyjski commented 2 years ago

@elachlan Any chance you could take a quick look into this (If not, that is fine but would be nice to get into the next release)? I think we just need to throw an OutOfMemory exception in a test fact and then update the reflection flags here: https://github.com/exceptionless/Exceptionless.Net/blob/master/src/Exceptionless/Extensions/ToErrorModelExtensions.cs#L57

elachlan commented 2 years ago

Looking now.