1

I have come across this link http://www.codeproject.com/Articles/447238/Advanced-Tracing#_comments. And the author did quite a nice job of creating a Custom listener that logs to a DataBase. While researching more on tracing/logging I read (from here https://blogs.msdn.microsoft.com/bclteam/2005/09/20/a-tracing-primer-part-ii-b-mike-rousos/) that the trace source calls the following methods when it needs to trace through a listener.

    void TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, int id)
    void TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, int id, string message)
    void TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, int id, string format, params object[] args)
    void TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, int id, object data)
    void TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, int id, params object[] data)
    void TraceTransfer(TraceEventCache eventCache, String source, int id, string message, Guid relatedActivityId)

And nearly all these methods (barring 1 or 2) has been overridden (all of them are public and just the TraceTransfer calls the WriteLine method, since the msdn blog says that these TraceEvent/Data/Transfer methods internally call Write/WriteLine methods) by the author of the custom trace listener (in the very first link from codeproject.com). I want to test this custom listener to see how it behaves. But in real life I don't know how would the trace source call those TraceEvent (with multiple overloads) and TraceTransfer() methods to trace through the custom listener. Though I do know that I can create an instance of trace source as

TraceSource ts = new TraceSource("myApp");
ts.Listeners.Add(new DBTraceListener);

and add custom listener to it (as in the code above) but the question remains in that how would trace source call TraceEvent/TraceTransfer methods (written in DBTraceListener) to trace through the custom listener.

In other words I don't know how to use this custom listener in my application to log traces. Any help will be greatly appreciated.

user2913184
  • 590
  • 10
  • 33

1 Answers1

1

An easy way to add a trace listener is using Trace.Listeners.Add(). For example, to add a TextWriterTraceListener:

var listener = new TextWriterTraceListener("e:\\blah.txt");
Trace.Listeners.Add(listener);
Trace.AutoFlush = true;
Trace.WriteLine("Test!");
  • That's not what I'm looking for. My question is how does trasource call tarceevent/tracetransfer methods of the listener class. – user2913184 Feb 03 '16 at 03:35
  • 1
    Any trace calls you make should go to all attached listeners. Once you have added your `DBTraceListener`, you can try Trace.TraceError(), Trace.TraceWarning(), etc. I think the Trace class calls the TraceEvent, TraceData, and TraceTransfer methods internally. – Andres Rebolledo Feb 03 '16 at 03:47
  • Well that's what I understood when I read the MSDN blog (link given above in my question) but still not sure how a TraceSource instance calls these internal methods on a listener. – user2913184 Feb 03 '16 at 13:58
  • Any more inputs from the experienced users will be highly appreciated. – user2913184 Feb 03 '16 at 14:43
  • 2
    This link on how to use TraceSources may be what you are looking for. https://msdn.microsoft.com/en-us/library/ms228993(v=vs.110).aspx You can also check out the TraceSource source code [here](http://referencesource.microsoft.com/#System/compmod/system/diagnostics/TraceSource.cs,75c4627b6b279433). You can see the TraceEvent methods iterate through each TraceListener, calling the corresponding TraceEvent event. Hope this is closer to what you are looking for. – Andres Rebolledo Feb 03 '16 at 15:30
  • That's pretty much what I was after specially the source code. Thanks a bunch my friend. – user2913184 Feb 03 '16 at 15:43
  • And the next question/confusion that comes to my mind is that do we have to strictly use TraceSwitch with Trace object and SourceSwitch with TraceSource object as I have read here http://stackoverflow.com/questions/3691749/traceswitch-and-sourceswitch-whats-the-difference. Since the codeproject.com code uses TraceSwitch and writes a custom method ShouldLogTrace(TraceEventType eventType) but for SourceSwitch this method is already defined by Microsoft as ShouldTrace(TraceEventType eventType). Will Microsoft's ShouldTrace() method do the same thing as the custom method ShouldLogTrace()? – user2913184 Feb 03 '16 at 15:50
  • Done!!!.. Microsoft's ShouldLog() method does the same job as the custom ShouldLogTrace(). – user2913184 Feb 03 '16 at 18:45