PC Review


Reply
Thread Tools Rate Thread

Coding techniques book sample error?

 
 
RD
Guest
Posts: n/a
 
      5th Dec 2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best Codeplex sample for showing best coding practices? John Dalberg Microsoft ASP .NET 3 16th Nov 2006 12:07 PM
any sample coding for capture I.P. address ? ken Microsoft Dot NET 1 23rd Jul 2006 09:30 PM
Outlook Express Address Book Backup Techniques parvardigar@yahoo.com Microsoft Outlook Contacts 1 15th Jul 2006 08:03 PM
Getting sample code to work from a 'Wrox' book... RAB Microsoft ASP .NET 1 21st May 2006 01:17 PM
sample form coding pls... =?Utf-8?B?YW50aG9ueSBwLg==?= Microsoft Access Form Coding 4 15th Oct 2004 12:57 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:36 AM.