File Logging...

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Okay,

In the process of fixing other issues, i came across this whole logging thing, and thought "Woohoo, i don't have to write a debug-log." So i created two things:

ApplicationEvents.vb
VB.NET:
    Private Const Except As String = _
        "[Exception - {0}]" & vbCrLf & _
        "   Source: {1}" & vbCrLf & _
        "   Message: {2}" & vbCrLf & _
        "   Data: {3}" & vbCrLf & _
        "   Stack: {4}" & vbCrLf & _
        "[Exception - {0} End]"


    Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
      Dim sData As String
      Dim sLog As String
      sData = e.Exception.Data.Count.ToString
      If e.Exception.Data.Count > 0 Then
        For i = 0 To e.Exception.Data.Count - 1
          sData += vbCrLf & "     " & e.Exception.Data.Item(i).ToString()
        Next
      End If
      sLog = String.Format(Except, _
                e.Exception.GetType().Name, _
                e.Exception.Source, _
                e.Exception.Message, _
                sData, _
                e.Exception.StackTrace)
      Me.Log.WriteEntry(sLog, TraceEventType.Suspend)
      e.ExitApplication = CBool( _
          MsgBox("An Unhandled Exception Occured." & vbCrLf & _
              sLog & vbCrLf & "Exit Application?", _
              MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton1 Or MsgBoxStyle.Exclamation, _
              "UniversalTool - Unhandled Exception") = MsgBoxResult.Yes)
    End Sub
  End Class

app.config
VB.NET:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                  <add name="FileLogListener" />
                  <!-- Uncomment to connect the event log. -->
                  <!-- <add name="EventLog" /> -->
                  <!-- Uncomment to connect the event log. -->
                  <!-- <add name="Delimited" /> -->
                  <!-- Uncomment to connect the XML log. -->
                  <!-- <add name="XmlWriter" /> -->
                  <!-- Uncomment to connect the console log. -->
                  <!-- <add name="Console" /> -->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLogListener"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" 
                 initializeData="FileLogListenerWriter"
                 location="ExecutableDirectory"/>
          <!-- 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>
</configuration>

now supposedly, whenever an unhandled exception is thrown, I get a message box listing the information - this happens. But it is also supposed to write to a Log File in the "Executable" directory, but this doesn't happen. ANy ideas why? Do i need to tell the Log class to "Open a Log" or something?

Thanks
Jaeden "Sifo Dyas" al'Raec Ruiner
 
Back
Top