Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

Using System.Diagnostics.TraceSwitch in ASP.NET 2.0

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Moe Sisko - 28 Mar 2008 01:14 GMT
According to the documentation, instance members of the TraceSwitch class
are not guaranteed to be thread safe.

Consider the following code :
==
in class "A":
       public static TraceSwitch BlahSwitch = new TraceSwitch("blah",
"blah");

in class "B" :

       private void DoSomething()
       {
           if (A.BlahSwitch.TraceError)
               System.Diagnostics.Trace.WriteLine("Error occured");
==

In doing some testing, I saw cases where BlahSwitch.TraceError sometimes had
the incorrect value. (To reproduce this, I created mulitple threads in a
winform app, and accessed BlahSwitch.TraceError from different threads at
the same time).

So, I'm wondering if using static instances of TraceSwitch is a good idea in
ASP.NET ?
Otherwise, creating a local instance of the TraceSwitch everytime it is
needed seems very inefficient.
John Saunders [MVP] - 28 Mar 2008 13:05 GMT
...
> So, I'm wondering if using static instances of TraceSwitch is a good idea
> in ASP.NET ?
> Otherwise, creating a local instance of the TraceSwitch everytime it is
> needed seems very inefficient.

Any time you use a static instance of anything in ASP.NET, you need to be
cautious. It can be accessed from multiple requests at the same time, which
means multiple threads at the same time. As the documentation said, the
instance methods of those classes are not thread-safe, so your code is not
thread-safe.

I recommend you not concern yourself with the performance impact of
instantiating a TraceSwitch, until you find that there _is_ a performance
impact. For all you know, .NET could be caching the important parts of the
TraceSwitch, causing little or no performance impact to you.
Signature

--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.