Search code examples
vb.netloggingdllclass-libraryexception-logging

How to log unhandled exceptions in a vb.net class library project?


I'm creating an add-in for Solidworks EPDM (example from API help). This is a class library (.dll) project which is added to EPDM and allows some custom functions to be added to the program.

I want to add logging for unhandled errors so that when an exception is caused by my add-in (as opposed to by the Solidworks EPDM program itself) I can be notified of it and try to fix it.

I'm quite new to all of this (and by all of this I mean VB.NET as a language, programming anything other than macros in VBA, structured exception handling, error logging, etc) and I'm trying to follow MSDN How To: Log Exceptions in Visual Basic but the instructions for logging unhanlded exceptions don't seem applicable to class library projects.

Specifically, I don't know how to get past step 3:

To log an unhandled exception
1. Have a project selected in Solution Explorer. On the Project menu, choose Properties.
2. Click the Application tab.
3. Click the View Application Events button to open the Code Editor.
This opens the ApplicationEvents.vb file.

The View Application Events button is greyed out for class library projects.

enter image description here

So, is there another way to add logging for unhandled exceptions in class library projects? Or, another way to access the ApplicationEvents.vb file for class library objects? I've tried searching for either, and have yet to find a solution that would allow me to log unhandled exceptions.


Solution

  • This is a very basic example but wrap your code with Try/Catch in the only two interface methods (host "callbacks") that IEdmAddIn5 defines and which your add-in class must implement.

    Public Sub GetAddInInfo(ByRef poInfo As EdmAddInInfo, ByVal poVault As IEdmVault5, ByVal poCmdMgr As IEdmCmdMgr5) Implements IEdmAddIn5.GetAddInInfo
        Try
           ...
        Catch ex As Exception
           ' Handle exception...
        End Try
    End Sub
    
    Public Sub OnCmd(ByRef poCmd As EdmCmd, ByRef ppoData As System.Array) Implements IEdmAddIn5.OnCmd
        Try
           ...
        Catch ex As Exception
           ' Handle exception...
        End Try
    End Sub
    

    I would ordinarily agree with @Hans Passant about re-throwing the exception but I generally have found that to be problematic with an EPDM add-ins as it can cause the COM host to crash.