exceptionless / Exceptionless.Net

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

Add Exceptionless.Windows plugin to capture additional helpful information #269

Closed elachlan closed 2 years ago

elachlan commented 2 years ago

Hi,

I wrote a plugin to capture the processes handle count, user object count, and GDI object count.

To reduce maintenance and improve portability. I think this information should be made available via a plugin in Exceptionless. Similar to the web/webapi plugins.

Would you accept a pull request for a plugin added to Exceptionless.Windows?

This is what I currently use, but I imagine there is a nicer approach using dependency injection with IEnvironmentInfoCollector so that we can extend EnvironmentInfo?

Public Class SysInfoPlugin
    Implements IEventPlugin
    Public Sub Run(context As EventPluginContext) Implements IEventPlugin.Run
        If context.Event.Type = [Event].KnownTypes.Error Then
            Dim info As Data.EnvironmentInfo = Nothing
            If context.Event.Data.TryGetValue([Event].KnownDataKeys.EnvironmentInfo, info) Then
                info.Data.Add("Handle Count", Process.GetCurrentProcess.HandleCount)
                info.Data.Add("User Object Count", GetGuiResourcesUserCount())
                info.Data.Add("GDI Object Count", GetGuiResourcesGDIObjectCount())
            End If
        End If
    End Sub

    <DllImport("User32")>
    Public Shared Function GetGuiResources(hProcess As IntPtr, uiFlags As Integer) As Integer
    End Function

    Public Shared Function GetGuiResourcesUserCount() As Integer
        Return GetGuiResources(Process.GetCurrentProcess().Handle, 1)
    End Function
    Public Shared Function GetGuiResourcesGDIObjectCount() As Integer
        Return GetGuiResources(Process.GetCurrentProcess().Handle, 0)
    End Function
End Class

Cheers, Lachlan

niemyjski commented 2 years ago

Hello,

Thanks for reaching out. How are you using this information? We've had a commercial WPF app for decades and never needed it or requests by other users so I'm trying to figure out the use cases. I don't think we would support adding Gui resources as it would cause issues with the extensions working cross platform on .NET 6+ due to the dll import.

Regardless, I'd recommend getting the current process once in your plugin and then passing it to GuiResources so you don't keep getting it over and over as I think it might be expensive.

I also think a plugin to extend the environment info is the best approach currently, but you could inject your own just take a look at the source.

elachlan commented 2 years ago

Hi Blake,

This is for Winforms not WPF, which isn't cross platform and currently Microsoft does not plan to make it so. Additionally, WPF is less likely to run into the exceptions I speak of due to its design, it doesn't use nearly as many window handles are winforms.

Having used Exceptionless for a number of years with two winforms apps, I have occasionally run into "out of memory" or "unable to create window handle" errors. These are quite rare but when they do occur the stack traces are pretty much useless. So the metadata is what becomes useful. Usually I have to contact the user and ask what they were doing (we have the user dialog disabled).

I wasn't able to quite work out how to extend the environment info via the plugin, do you have an example somewhere?

Regards, Lachlan

elachlan commented 2 years ago

@niemyjski I have put in a PR at #270. I'd love some feedback there.