Recently I went thru’ a article that talks about Conditional Debugging in C#. Eager to try it out, I was researching it. I was really surprised to see the out comes. So I thought of sharing all those things with you. As the above has link already talked about conditional debugging, lets start with tracing.
In C#, there are loads of ways to trace the execution. All of them can be acheived using the object Trace. You can trace the execution and the errors that are thrown by the application in to a,
Console
Here what ever you trace will be written to the Console (command prompt), if you run the application from a command prompt. To accomplish this we need to do the following,
- Create an object of the ConsoleTraceListener
ConsoleTraceListener aConsoleTraceListener = new ConsoleTraceListener(); //Creating an object for tracing
- In the place where you wanna trace something, add the following statement
aConsoleTraceListener.WriteLine("<the message you wish to trace on executing the current method>"); //Will write a trace line in to the console
This will work only if you are running a console application or a window application from the console (You can also change the properties of a window application to console application and then run it so that it automatically runs from the console even if you press F5 from Visual Studio).
EventViewer
There is a utility called EventViewer that comes with windows. Try typing eventvwr in Run applications. You will get a dialog that has loads of events and their logs. You can create an object of EventLogTraceListener to log the events out there. Lets see how we do it.
- Create an object of the EventLogTraceListener
EventLogTraceListener aEventLogTraceListener = new EventLogTraceListener("Foo"); //Creating an object of the trace viewer with the source name as Foo
- In the place where you wanna trace something, add the following statementa
EventLogTraceListener.WriteLine("<the message you wish to trace on executing the current method>"); //Will write a trace line in to the event viewer
Additional options
The above methods allows you to trace some messages. In addition if you need to trace the ThreadID, ProcessID, TimeStamp or even CallStack, you can use the TraceEventCache. This class allows one to get the above mentioned properties of a event. Try adding the following snippet to your code.
TraceEventCache aTraceEventCache = new TraceEventCahce(); //New trace cache object that will hold additional info about the event
aConsoleTraceListener.TraceOutputOptions = TraceOptions.TimeStamp | TraceOptions.ProcessId; //Will add these details to the log
The TraceEvent method will log additional details like TimeStamp, ProcessID, etc., to the message that is being logged. These are some of the methods to trace the flow or execution of a program. These are some of the easy ways to find the bugs(if any) in our application even if it has been released to the customers irrespective of the nature of the build (debug or release).
aConsoleTraceListener.TraceEvent(aTraceEventCache, “Foo”, TraceEventtype.Information, 0, “Method __name___ has been executed”); //Will log the trace message in to the console window
Update
To log to a text stream you can use the TextWriterTraceListener object. You can also use the static class EventLog to trace messages to the EventViewer.
Tags: C#, Editors, General Programming, Tips 'n' Tricks, Visual Studio

Just wanna make one important note. The TraceEventCache.CallStack won’t work properly. Take a look at this,
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=97335
If it works for you, tell us how you did that.