The following code snippet is taken from book Coding techniques for
Microsoft Viusal Basic .Net. Its part of a sample error handler.
In the public Sub New below after comment Write information to our file,
These lines of code do not write to the text file. The file is created and
opened OK but when this code is invoked and executed, nothing gets written
to the file, it remains empty. It looks like the TextWriterTraceListener is
not working and the trace stream does not get written to the text file.
Can anyone tell me how to fix this code? I'm turning around in circles.
Any help would be greatly appreciated,
Bob
Imports System.IO
Imports System.Diagnostics
Public Class ErrorTrace
Private m_errorFile As StreamWriter
Private m_fileListener As TextWriterTraceListener
Private m_sAppName As String = ""
Private m_tsTraceSwitch As TraceSwitch
Private m_elEventLog As EventLog
Private m_isEventWriter As Boolean
Public Sub New(ByVal sApplication As String, _
ByVal addEventWriter As Boolean)
Dim sEnv As String = _
Environment.GetEnvironmentVariable("demoSwitch")
m_tsTraceSwitch = New TraceSwitch("demoSwitch", _
"demoSwitch")
m_tsTraceSwitch.Level = CInt(sEnv)
If m_tsTraceSwitch.Level = TraceLevel.Off Then Exit Sub
m_errorFile = File.AppendText("C:\ErrorFile.txt")
m_fileListener = New TextWriterTraceListener(m_errorFile)
Trace.Listeners.Add(m_fileListener)
m_sAppName = sApplication
m_isEventWriter = addEventWriter 'used when disposing
'-- Do we want to log info to the event log also?
If (m_isEventWriter = True) Then
m_elEventLog = New EventLog("Application", ".", _
sApplication)
m_elEventLog.WriteEntry(Date.Now & " Application: " & _
m_sAppName & " started", EventLogEntryType.Information)
End If
'-Write information to our file - THIS SHOULD WRITE TO THE TEXT FILE BUT IT
DOES NOT
Trace.WriteLine(Date.Now & " Application: " & _
m_sAppName & " started")
Trace.WriteLine("Application Version: " & _
Environment.Version.ToString)
Trace.WriteLine("Current Directory: " & _
Environment.CommandLine)
Trace.WriteLine("Machine Name: " & _
Environment.MachineName)
Trace.WriteLine("Operating System: " & _
Environment.OSVersion.ToString)
Trace.WriteLine("User: " & Environment.UserName.ToString)
Trace.WriteLine( _
"------------------------------------------")
End Sub
Public Sub writeError(ByVal eException As System.Exception)
Trace.WriteLineIf(m_tsTraceSwitch.TraceInfo, _
"Error: " & eException.Message)
Trace.WriteLineIf(m_tsTraceSwitch.TraceVerbose, _
"Stack Trace: ")
Trace.WriteLineIf(m_tsTraceSwitch.TraceVerbose, _
eException.StackTrace)
Trace.WriteLineIf(m_tsTraceSwitch.TraceVerbose, _
"------------------------------------")
'Write any error to the event log
If (m_isEventWriter = True) Then
m_elEventLog.WriteEntry(eException.Message, _
EventLogEntryType.Error)
End If
End Sub
Public Sub writeText(ByVal sMessageToWrite As String)
Trace.WriteLineIf(m_tsTraceSwitch.TraceInfo, _
sMessageToWrite)
End Sub
Public Sub writeEventLogInformation(ByVal sMessage As String)
m_elEventLog.WriteEntry(sMessage, _
EventLogEntryType.Information)
End Sub
Public Sub writeEventLogWarning(ByVal sMessage As String)
m_elEventLog.WriteEntry(sMessage, _
EventLogEntryType.Warning)
End Sub
Public Sub dispose()
If m_tsTraceSwitch.Level = _
System.Diagnostics.TraceLevel.Off Then
Exit Sub
End If
Trace.WriteLine(Date.Now & " Application: " & _
m_sAppName & " ended")
If (m_isEventWriter = True) Then
m_elEventLog.WriteEntry(Date.Now & " Application: " & _
m_sAppName & " ended.", _
EventLogEntryType.Information)
End If
m_errorFile.Flush()
m_errorFile.Close()
End Sub
End Class