Rediscovering the BCL - tracing

by Matt 17. July 2007 03:15

Back in the mists of .net 1.x, if you wanted to put any kind of logging (or - if you're posh - telemetry) in your application, you generally avoided the Base Class Libraries, and for good reason. What was there was basic, and not massively flexible.

You had System.Diagnostics.Trace and System.Diagnostics.Debug. They would write out to any and all TraceListeners defined in config (of which there could be Default, EventLog and TextWriter). There was no great difference between the two - the output wasn't categorised as trace or debug, and they both went to the same set of listeners.

Categories were a bit rubbish - they simply got prepended to the text that was output. No filtering and no routing of categorised output to a specific listener. The way to do conditional output was to check with a named Switch before calling Write. The output level of the Switch (or rather its derived classes TraceSwitch and BooleanSwitch) would get set in config. This lead to code like:


which leads to the WriteIf family of methods:


(One kinda nifty feature was that each of the Write methods on these classes are marked with the ConditionalAttribute, meaning Trace.Write would only work if you'd compiled with the TRACE constant defined, and Debug.Write requires the DEBUG constant. The nifty bit is that if you don't have the constants defined, the call to the method is optimised away. It's like surrounding the call to Debug.Write in #ifdef's. But it wouldn't optimise away the "if" when using switches. Hence WriteIf.)

All in, not too terrible, but not very practical, either. The switch idea is ok, but you decide in code (not config) if it's going to be all or nothing (BooleanSwitch) or based on trace level (TraceSwitch). It's a bit restricting that you can only output text, and any extra information you want, you have to get yourself. You're mainly limited by outputting everything to all listeners.

The normal reaction to this was to reach for a 3rd party solution, such as log4net, or Enterprise Library. Both of these provide categorisation, routing to different listeners and finer control of what gets output.

It's what we did at work. We used Enterprise Library (and Enterprise Instrumentation Framework before that, I believe) and it hurt, so we wrote a wrapper API for it, and that also hurt, so we wrote a new wrapper for it, and then migrated to EntLib 2, which changed their API, and we wrote a new wrapper for that.

And now I'm looking at WCF, and especially the Service Trace Viewer, and I need to understand how that works, which means a dive back into .net 2's tracing features.

Guess what? They've pretty much fixed it.

You've still got Trace and Debug, and they still function basically the same. But you don't use those in new code. You use TraceSource instead. You create a named instance of one of these, and call TraceEvent, TraceInformation or TraceData on it. Each TraceSource can have its own set of TraceListeners or use any from a pool of shared listeners. Giving it a name effectively gives you categories (a common naming convention is to use your namespace), and lets you define routing in config. A TraceSource also has a SourceSwitch to provide blanket filtering (by trace level) for all events from a TraceSource. Each listener can have a TraceFilter, which also filters events - useful for shared listeners where the TraceSource isn't configured the same. And a TraceListener can now automatically output a bunch of useful information along with the text, including callstack, timestamp and process and thread id. Arbitrary data can be logged with TraceData.

There are even a few more listeners: Default, EventLog, TextWriter, Console, DelimitedList, WebPage (nice!) and the most interesting one, XmlWriterTraceListener.

(And there's a StopWatch, too.)

Indeed it looks like the only reason to go with a 3rd party solution now is for more out of the box listeners. Log4net ships with a ton of them. Enterprise Library (which was rearchitected to use .net 2's logging) adds a few more (MSMQ, Wmi, email and so on). But if you don't want to take the dependency, it's not hard to write a custom TraceListener (or just use standard .net 2 APIs and the EntLib listeners).

But the killer app for .net 2 logging? That's where XmlWriterTraceListener, WCF's Service Trace Viewer, the CorrelationManager's ActivityId's and End-to-End Tracing come in. The next post will have pretty pictures...

PS. A few more useful links:

  1. MSDN's tracing docs (part 1)
  2. More info on .net 1.x's poor logging
  3. Tracing in + playing nice with System.Diagnostics.Trace
  4. BCL Team Blog - A Tracing Primer - Part I
  5. BCL Team Blog - A Tracing Primer - Part II (A)
  6. BCL Team Blog - A Tracing Primer - Part II (B)
  7. BCL Team Blog - A Tracing Primer - Part II (C)


Comments (40) -

6/5/2008 9:13:53 AM #

RE: Rediscovering the BCL - tracing

What an awesome post - you just explained my entire plight, and how to fix it. THANK YOU!!!


best hybrid cars
best hybrid cars
7/22/2011 4:55:16 AM #

Fantastic task I like your type! Would really like to right here your feedback on my website! I am also seeking for someone to help you me make websites!


Marianne Babers
Marianne Babers United States
1/6/2016 3:11:43 PM #

Printing out an essay or paper, reading the first line and realizing there aren't any mistakes.<br />AWESOME!


Selma Houzah
Selma Houzah United States
1/15/2016 5:00:15 AM #

Hey, congratulations Neil. This site really is something special and it's been a pleasure to share it with my readership too. Looking forward to future awesome things, and I'll send any suggestions that are sufficiently awesome.


Natividad Sluder
Natividad Sluder United States
1/15/2016 5:15:07 AM #

AWESOME!Can't wait for that, will be something to want to accomplish day by day, to do something AWESOME!


Johnathon Paulson
Johnathon Paulson United States
1/15/2016 6:07:39 AM #

Congratulations, Neil! I love this site and I know I'll love the book. Christmas, birthdays, Father's Day 2010  check!


Charlie Mallat
Charlie Mallat United States
1/15/2016 6:43:12 AM #

I love the dolphin saving you one.


Marylynn Okoro
Marylynn Okoro United States
1/15/2016 7:11:30 AM #

I love the dolphin saving you one.


Selma Houzah
Selma Houzah United States
1/15/2016 8:20:37 AM #

I can't wait to read this masterpiece. Ordering off the menu at fast food restaurants and old dangerous playground equipment have to be in the book!


Dion Newcomer
Dion Newcomer United States
1/15/2016 8:29:03 AM #

AWESOME!Can't wait for that, will be something to want to accomplish day by day, to do something AWESOME!


Gilda Shibuya
Gilda Shibuya United States
8/25/2016 4:28:41 AM #

That's a nice post. hopefully you will also make a dedicated post for smart home design which features home automation.


Genevie Chetram
Genevie Chetram United States
9/27/2018 6:45:53 PM #

Excellent post. I was checking continuously this weblog and I am inspired! Very helpful info specially the ultimate phase Smile I take care of such info a lot. I used to be seeking this particular information for a long time. Thank you and best of luck.


Jettie Gurtner
Jettie Gurtner United States
9/28/2018 3:39:12 AM #

Hello there, just became aware of your blog through Google, and found that it's truly informative. I?m gonna watch out for brussels. I will be grateful if you continue this in future. Lots of people will be benefited from your writing. Cheers!


Barney Minnehan
Barney Minnehan United States
9/28/2018 6:07:49 AM #

Today, I went to the beachfront with my kids. I found a sea shell and gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!


Jordan Polski
Jordan Polski United States
9/28/2018 9:42:33 PM #

I sight myself approach to your blog increasingly oftentimes to the point where my visits are just about every day immediately!


Daren Horvitz
Daren Horvitz United States
9/29/2018 3:40:38 AM #

I always visit your blog everyday to read new topics.;”.,;


Loni Hargis
Loni Hargis United States
9/29/2018 1:06:36 PM #

Wonderful work! This is the type of info that should be shared around the net. Shame on Google for not positioning this post higher! Come on over and visit my website . Thanks =)


Abraham Gaznes
Abraham Gaznes United States
9/29/2018 5:03:38 PM #

Thank you, I have recently been searching for info about this subject for a long time and yours is the greatest I've found out so far. However, what about the bottom line? Are you sure about the source?


Mignon Vuturo
Mignon Vuturo United States
9/30/2018 2:14:35 AM #

my hard drive is getting full already because i always download free online movies on the internet~


Sean Ruot
Sean Ruot United States
9/30/2018 2:43:06 AM #

Excellent post however , I was wanting to know if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit more. Bless you!


Horacio Besanson
Horacio Besanson United States
10/9/2018 12:37:59 AM #

Heya are using Wordpress for your site platform? I'm new to the blog world but I'm trying to get started and create my own. Do you need any coding knowledge to make your own blog? Any help would be greatly appreciated!


Haywood Cuppett
Haywood Cuppett United States
10/9/2018 7:22:03 AM #

I do agree with all of the ideas you have presented in your post. They are really convincing and will certainly work. Still, the posts are too short for novices. Could you please extend them a little from next time? Thanks for the post.


Lieselotte Micks
Lieselotte Micks United States
10/9/2018 8:35:40 PM #

Definitely, what a magnificent site and informative posts, I definitely will bookmark your website.Have an awsome day!


Theresia Mesenbring
Theresia Mesenbring United States
10/9/2018 11:27:50 PM #

Excellent post. I was checking continuously this blog and I am impressed! Very helpful info specifically the last part Smile I care for such information a lot. I was seeking this particular information for a long time. Thank you and best of luck.


完美 United States
10/10/2018 4:40:07 AM #



Ngan Howey
Ngan Howey United States
10/10/2018 6:07:40 AM #

Great post. I was checking continuously this blog and I'm inspired! Extremely useful info particularly the final part Smile I deal with such info a lot. I was looking for this particular info for a very lengthy time. Thanks and good luck.


HIFU 無創超聲波4D拉皮
HIFU 無創超聲波4D拉皮 United States
10/10/2018 6:08:12 AM #

「 生蛇 」( 醫學名稱是 『帶狀疱疹』Shingles ) 是由水痘病毒引起的疾病。水痘癒合後,病毒在神經細胞潛伏多年,日後隨時重新激活後導致的帶狀皮疹,就是「 生蛇 」。因此,凡感染過水痘的人,都有機會「 生蛇 」。病發時,身體某一部位會感到內疼或劇痛,通常是臉或背部及腰部的一側;這時由於病毒從脊髓處沿其中華一條神經擴散到身體以及去到連接的皮膚表面,形成紅疹及小水疱。帶狀疱疹疫苗Zostavax康栢苗 (蛇針)能助減低「 生蛇 」的機會,有效預防: 「 生蛇 」( 帶狀疱疹 ) 成效高達 70% 「 生蛇 」的後遺神經痛:即使紅疹痊癒,神經痛還可以持續數月或數年。年紀越大,持續有後遺   神經痛風險越高 「 生蛇 」引起的急性及長期痛症 不適用人士: 曾對疫苗成分包括明膠( Gelatin ) 及紐奧黴 ( Neomycin ) 有過敏性休克反應的人士 有原發及繼發性免疫系統問題的病患者,或正服用抑鬱免疫系統藥物(如高劑量膽固醇)的病人 患有肺結核,正發病及未經治療人士 孕婦


Soledad Bruse
Soledad Bruse United States
10/11/2018 9:14:10 AM #

Great goods from you, man. I've understand your stuff previous to and you're just extremely wonderful. I actually like what you have acquired here, really like what you're stating and the way in which you say it. You make it entertaining and you still take care of to keep it smart. I can't wait to read far more from you. This is actually a terrific site.


Dee Schneiderman
Dee Schneiderman United States
10/11/2018 1:46:04 PM #

Wow! This can be one particular of the most useful blogs We have ever arrive across on this subject. Basically Magnificent. I'm also an expert in this topic therefore I can understand your hard work.


Ramonita Pereida
Ramonita Pereida United States
10/11/2018 6:14:06 PM #

I’m impressed, I must say. Actually not often do I encounter a weblog that’s each educative and entertaining, and let me inform you, you've gotten hit the nail on the head. Your concept is excellent; the difficulty is something that not enough individuals are speaking intelligently about. I am very pleased that I stumbled across this in my seek for one thing regarding this.


Moon Marchman
Moon Marchman United States
10/12/2018 10:44:14 AM #

Howdy! I know this is somewhat off topic but I was wondering which blog platform are you using for this site? I'm getting fed up of Wordpress because I've had problems with hackers and I'm looking at options for another platform. I would be awesome if you could point me in the direction of a good platform.


醫學美容 浴鹽
醫學美容 浴鹽 United States
10/14/2018 2:28:21 AM #

Annie`s Way 【調理面膜系列】綠茶調理隱形面膜 Green Tea Relaxing Invisible Silk Mask的商品介紹   Annie`s Way,調理面膜系列,綠茶調理隱形面膜 Green Tea Relaxing Invisible Silk Mask       


Michael Hudmon
Michael Hudmon United States
10/14/2018 3:13:29 AM #

Hello, Neat post. There’s a problem with your website in internet explorer, would check this… IE nonetheless is the marketplace leader and a huge portion of people will omit your great writing due to this problem.


Schaebens 雪本詩 商品一覽   找品牌 以UrCosme指數高至低排序 第1頁 Schaebens 雪本詩 化妝品商品一覽,提供 Schaebens 雪本詩的產品列表,以UrCosme指數高至低排列,共15件,Schaebens 雪本詩的搜尋結果之第1頁        


Daniel Stride
Daniel Stride United States
10/15/2018 1:26:51 AM #

of course like your website but you have to check the spelling on several of your posts. Many of them are rife with spelling problems and I find it very troublesome to tell the truth nevertheless I’ll definitely come back again.


連雙下巴 United States
10/17/2018 4:54:12 AM #

維持肌膚大量水分,造就柔嫩有彈性、晶瑩煥白的肌膚。 中性肌膚使用


Barrett Heppding
Barrett Heppding United States
10/18/2018 10:15:38 PM #

I was curious if you ever thought of changing the structure of your website? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or two pictures. Maybe you could space it out better?


Natural Life 【天然護膚系列】掃瑕淨白精華液的商品介紹   Natural Life,天然護膚系列,掃瑕淨白精華液       


Edmund Vergo
Edmund Vergo United States
10/19/2018 6:31:27 PM #

Hello, i think that i saw you visited my site thus i came to “return the favor”.I am trying to find things to enhance my web site!I suppose its ok to use a few of your ideas!!


Brady Lupiani
Brady Lupiani United States
10/20/2018 1:51:31 PM #

You should take part in a contest for probably the greatest blogs on the web. I will suggest this site!


Add comment

  • Comment
  • Preview


Month List


Comment RSS