473,406 Members | 2,220 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,406 software developers and data experts.

Trace Listeners in a web app

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.Curre nt.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.Curre nt.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\TraceListenerEx ample\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.Curre nt.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.




Nov 18 '05 #1
1 1799
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/
----------------------------------
Making waves on the Web
"Martin" <ma****@hotmail.com> wrote in message
news:Ou**************@TK2MSFTNGP12.phx.gbl...
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.Curre nt.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.Curre nt.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\TraceListenerEx ample\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.Curre nt.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.



Nov 18 '05 #2

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

Similar topics

8
by: Geopsaros | last post by:
Hi! I have created a custom trace Listener class, called "DBTraceListener" it works fine when i add it manually in code : (eg. Trace.listeners.add(new DBTraceListener("myDBListener",...
2
by: Ken | last post by:
I would like to start using EventLogTraceListener, and am running into a couple of significant limitations: 1) I have found that there is no way to write EventLog entries with different...
2
by: Wilfried Hoermann | last post by:
Presumably a trivial question... I want to write trace information from a web service to a log file using the Trace Class. Is this possible without closing and opening the Trace in every single...
2
by: S. Han | last post by:
I use Trace.Assert() through out my app and it's such a great way to debug a program. My question is is there any way to redirect the assertion pop-up to the command line output in my console...
3
by: Jens Alenius | last post by:
I've been looking at the Trace and TraceSwitch Classes i C# and I thrying to make it act more like java log4j. What I want is to add two different tracelisteners (like EventLogTraceListener and...
5
by: guy | last post by:
Where do I get trace output? Trace.Write("Hello World");
1
by: Patrick | last post by:
When Tracing in ASP.NET, the IIS process (on IIs5.1) is locking on the Trace file, and I can't read the trace file without restarting the IIS: Even the following does NOT work (how could I fix...
3
by: Absolon | last post by:
Hi, Can anyone tell the advantages/dis-advantages of adding a listener to the listeners collection? In MSDN it says that listeners not in the collection do not recieve all trace output. What...
0
by: msnews.microsoft.com | last post by:
Hi, I am tring to use Trace Listener in asp.net 1.1 application as mentoned here. But i am getting the following error, Configuration Error Description: An error occurred during the processing...
6
by: Zytan | last post by:
According to http://www.15seconds.com/issue/020910.htm I am doing this in the c'tor of a 'logfile' class: objStream = new System.IO.FileStream(logFilename, System.IO.FileMode.OpenOrCreate);...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.