Link to home
Create AccountLog in
Avatar of GIANTOCR
GIANTOCR

asked on

Logging Exceptions Visual Basic

I made a Windows application using Visual Basic 2005. I would like to write Unhandled Exceptions to the application log. Below is the code I am using to do this. I have also included the code from the app.config file.

I have deployed the application on another computer and encountered some exceptions that caused the application to shutdown. I am trying to find the application log, but am unable to find it. How can I locate the application log on the computer?

Thanks.
Partial Friend Class MyApplication
 
        Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            'Exception logging code copied from "How to: Log Exceptions in Visual Basic "
 
            My.Application.Log.WriteException(e.Exception, _
                TraceEventType.Critical, _
                "Application shut down at " & _
                My.Computer.Clock.GmtTime.ToString)
            MessageBox.Show("An unexpected error has occurred. The application will shut down.")
            'It is the default that the application closes after this event has occured
        End Sub
    End Class
 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="BoilerReadings.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <connectionStrings>
        <add name="BoilerReadings.My.MySettings.FacOpsDataConnectionString"
            connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\FacOpsData.mdb"
            providerName="System.Data.OleDb" />
    </connectionStrings>
    <system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    <!-- Uncomment the below section to write to the Application Event Log -->
                    <!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLog"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
                 initializeData="FileLogWriter"/>
            <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
            <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>
    <applicationSettings>
        <BoilerReadings.My.MySettings>
            <setting name="oledbConnectionString" serializeAs="String">
                <value>Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\FacOpsData.mdb</value>
            </setting>
        </BoilerReadings.My.MySettings>
    </applicationSettings>
</configuration>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Note that your exception handler will not catch all exceptions. For instance, if you have a divide by zero error, it is handled by the GUI as a ThreadException, and its default behavior is to not handle it. To catch this type of exceptions, you have to register a handler for the thread exceptions:




Application.ThreadException += _
                 new System.Threading.ThreadExceptionEventHandler( _
                 AddressOf OnGuiUnhandledException)
 
Public Shared Sub OnGuiUnhandledException(Object sender, _
   System.Threading.ThreadExceptionEventArgs e)
{
    ' do your logging or throw new exception:
    Throw New InvalidOperationException("Invalid operation.")
}

Open in new window

Avatar of GIANTOCR
GIANTOCR

ASKER

Thanks
Thanks. I had read that MSDN article several times but until now had missed the community content at the end which stated

"This documentation fails to point out that the FileLogTraceListener (which is enabled by default in Visual Studio apps), defaults to using LocalUserApplicationDirectory for its location.
So for example, if you create an app with an executable named MyApp.exe, you will find the FileLogTraceListener log file named MyApp.log in C:\Documents and Settings\<your user name>\Application Data\<company name>\MyApp\<version>
If you use any of the My.Application.Log methods to write to the log, the output will go to that log file as well as any other listeners that you enable."

By making my hidden files visible in Windows Explorer I was able to find the log file. Also thanks for the tip on GUI exceptions. I will change my program to handle those as well.

You're welcome :)

-- Abel --
> By making my hidden files visible in Windows Explorer I was able to find the log file.

apologies, since that's often the first thing I turn on on any system I work on, I always forget to mention that system / hidden files are not visible for everybody.