473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Coding techniques book sample error?

RD
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
Nov 21 '05 #1
0 786

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Nick Coghlan | last post by:
Thought some folks here might find this one interesting. No great revelations, just a fairly sensible piece on writing readable code :) The whole article:...
55
by: Jonas Smithson | last post by:
I've seen a few attractive multi-column sites whose geometry is based on pure CSS-P, but they're what you might call "code afficionado" sites, where the subject matter of the site is "coding...
4
by: dotNetDave | last post by:
About three weeks ago I released the first .NET coding standards book titled "VSDN Tips & Tricks .NET Coding Standards". Here is what the famous author/ speaker Deborah Kurata says about it: ...
15
by: christopher diggins | last post by:
I have written an article on how to do Aspect Oriented Programming in vanilla C++ (i.e. without language extensions or other tools such as AspectC++). The article is available at...
17
by: emerth | last post by:
Hello all: I have read references to optimizing C code to exploit the CPU cache of <insert your favourite CPU>. Can anyone point me towards some info on the 'net that gets into techniques for...
2
by: Newbie \(C#,Asp.net\) | last post by:
Hi I have two simple questions that I make sure that I have underestood them 1) what is the main difference between inline coding and code behind besides that code behind is part of DLL and is...
6
by: Maitre Bart | last post by:
One said: "The more time you'll spend on designing your app, the less you'll spend time debugging it." I agree with that, but chances are, you'll still need to debug it anyway! And the larger the...
2
by: =?Utf-8?B?TWljaGFlbA==?= | last post by:
Hi, I have been developing using C# in ASP.Net for about a year now. And have been a programmer for about 10 years. I have learned many different things in .NET, but still when I look at sample...
8
by: wangdaixing | last post by:
I am reading "Programming .NET Components" 2nd Edition by Juval Lowy, O'Reilly. In Appendix E, there is a chapter "Coding Practices" which I agree and practice mostly. However, there are a few...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.