Trace Listeners in a web app

M

Martin

Hi,

is it a wise thing to do to place trace listeners (that write to a text
file) into an asp.net application.

I wish to do this for testing so that a user may vist as many different
pages as they like and the trace information for that session will be kept
in a sigle place.

I have implemented a logger and added a trace switch to my web.config file

<system.diagnostics>
<switches>
<add name="mySwitch" value="4" />
</switches>
</system.diagnostics>


however the following code in global asax throwws an exception of
"NullRefereneceException"
however it DOES actually write the file

Public Class Global
Inherits System.Web.HttpApplication

Private Shared mySwitch As New TraceSwitch("mySwitch", "Entire
Application")
Dim traceFile =
System.IO.File.Create(System.Web.HttpContext.Current.Server.MapPath("TraceOu
tputHTML1.htm"))

Dim Logger As New Logger(traceFile)

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
'Dim traceFile =
System.IO.File.Create(System.Web.HttpContext.Current.Server.MapPath("TraceOu
tputHTML1.htm"))
'Dim Logger As New Logger(traceFile)
Logger.WriteError("Writing an error")
Logger.WriteWarning("Writing a warning")
Logger.WriteInfo("Writing some info")
Logger.WriteVerbose("Writing verbose information")
If mySwitch.TraceWarning() Then
Logger.WriteInfo("Conditional write")
End If
Logger.Flush()
Logger.Close()
End Sub

The follwoing code DOES NOT write to the file BUT also does not throw an
error -- despite the code executing, because I have walked through it with
the debugger.

Public Class Global
Inherits System.Web.HttpApplication

Private Shared mySwitch As New TraceSwitch("mySwitch", "Entire
Application")
Dim traceFile =
System.IO.File.Create("D:\Projects\TraceListenerExample\TracingWeb2\TraceOut
putHTML1.htm")

Dim Logger As New Logger(traceFile)

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
'Dim traceFile =
System.IO.File.Create(System.Web.HttpContext.Current.Server.MapPath("TraceOu
tputHTML1.htm"))
'Dim Logger As New Logger(traceFile)
Logger.WriteError("Writing an error")
Logger.WriteWarning("Writing a warning")
Logger.WriteInfo("Writing some info")
Logger.WriteVerbose("Writing verbose information")
If mySwitch.TraceWarning() Then
Logger.WriteInfo("Conditional write")
End If
Logger.Flush()
Logger.Close()
End Sub



Notice that the only difference is the use of server.mappath when creating
the file.

The logger definayly works because it is being used successfully in a web
forms app.


any help is appreciated.

cheers

martin.
 
R

Rick Strahl [MVP]

Hi Martin,

It may have severe performance implications because access to the file needs
to be serialized in some way in order to write to it. Won't be a problem in
low volume situations though.

I suspect the error you get has to do with the file being in used. Further
you have the logger as a static/shared component which further will cause
problems for you when multiple simultaneous requests access this component.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top