Just "Trace" it

It’s been quite a while by looking into this simple .NET feature which helps a lot in finding out issues in Live applications. The other day we struggled in implementing Trace functionality for our ongoing application and surprisingly we were unable to find significant/straightforward help.
Thought it would be good if I can draft it in simple terms which may be useful for all (at least for my later reference 🙂 ).
When we are talking about Trace, we should know about 3 main elements.
1) Sources : Trace source is the component which actually generates all the trace data, Define the required listeners for this source to track the trace information. An “application” can have more than one trace source, typically it will be defined per module of the project. For example, you can define one source for your helpers module which logs only Error and another source for your service project where you can log both Errors & warnings as well if you want.
This is how a typical trace source config looks like. ( I’ll discuss about the Switches mentioned in the below block further in the post, don’t worry about it for now).

 




































2)Listeners : Trace listener listens for data on a particular source and dumps into the required location as defined by the listener type. There are some default providers MS provides and more detailed description of these listener types is available at https://msdn.microsoft.com/en-us/library/4y5y10s7(v=vs.110).aspx.
Typical trace listener looks like..

 
















*There is a concept called “Shared Listener”, as the name suggests, this listener can be shared with multiple sources if required. To use shared listeners, the config should be

 

































<add type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"



name="sharedlistner1"/>









3)Switch : Trace switches are basically On/Off configuration for Trace enable/disable. In addition to simple On/Off, you can define Trace only errors or only warnigs etc. One thing to remember over here is trace swithc follow heirarchy ie. If Trace switch is for “Information”, it will log Error+Warning+Information, in the same way, if it is for “Warning”, it will log Error+Warning.
The configuration of switches would look like..

 
















So, finally the whole configuration would look like..

 






















































<add type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"



name="sharedlistner1"



BaseFileName="test"



Location="Custom"



CustomLocation="C:\temp\"



LogFileCreationSchedule="Daily"



traceOutputOptions="DateTime">

















Some of the points to look at in the configuration a) Look at the 2nd Line, Trace Autoflush set to True, this is mandatory to flush the data to file.
b) In the listeners, though we have added only 1 listener (ie shared listener), .NET by default adds one another listener (you can observe it in debug mode of the application), you can remove the default listener by adding like
c) Check the additional attributes in the shared listener. Though you see a warning message for all the additional attributes, don’t worry about them, compile the code it should work.
d)To define a custom path for your trace file, the location should be “Custom” and then mention the CustomLocation attribute. Make sure the folder exists already if you are using CustomLocation.
e) You can use the predefined locations like LocalUserAppdata etc in the Location tag.
Finally, to use the trace logging in the source

 


TraceSource log = new TraceSource("Source1");



log.TraceData(TraceEventType.Error, 1, "test error message");



log.TraceData(TraceEventType.Information, 2, "test information message");





** This has been provided as a simple example. In real world you would like to create a nice wrapper for all your log/trace mechanism and provide simple, easy to use helper methods to log the messages.
Hope this helps, Happy coding !!

One thought on “Just "Trace" it

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s