Featured

    Featured Posts

  • How to use Shortcut keys in USD
  • Create Timer behavior with action calls

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).
 


<system.diagnostics>  


 
<sources>  


 
<source name="Source1" switchName="MySwitch" switchType="System.Diagnostics.SourceSwitch" >  


 
<listeners>  


 
</listeners>  


 
</source>  


 
</sources>  


 
</system.diagnostics> 


 


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..
 


<listeners>  


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


 
</listeners> 


 


*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
 


<sources> 


<source name="Source1" switchName="MySwitch" switchType="System.Diagnostics.SourceSwitch" >  


 
<listeners>  


 
<add name="sharedlistner1"/>  


 
</listeners>  


 
</source>  


 
</sources>  


 
<sharedListeners>  


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


 
name="sharedlistner1"/>  


 
</sharedListeners> 


 


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..
 


<switches>  


 
<add name="MySwitch" value="Information"/>  


 
</switches> 


 


So, finally the whole configuration would look like..
 


<system.diagnostics>  


 
<trace autoflush="true"/>  


 
<sources>  


 
<source name="Source1" switchName="MySwitch" switchType="System.Diagnostics.SourceSwitch" >  


 
<listeners>  


 
<add name="sharedlistner1"/>  


 
</listeners>  


 
</source>  


 
</sources>  


 
<switches>  


 
<add name="MySwitch" value="Information"/>  


 
</switches>  


 
<sharedListeners>  


 
<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">  


 
</add>  


 
</sharedListeners>  


 
</system.diagnostics> 


 


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 !!
author

Naga Srikanth Alluri

A passionate developer, learner and a budding blogger :)

Get Email Updates to your Inbox!

1 comments:

Short and sweet. Thanks for the quick info on traces.

Reply

Post a Comment

Hey, dont forget to leave your name if your giving comment as anonymous user :)

www.CodeNirvana.in

Translate

Total Pageviews

Copyright © CRM Ramblings | Designed By Code Nirvana