Reputation: 7574
I want to implement a custom trace listener like follows:
public class TraceListener : System.Diagnostics.TraceListener
{
public override void Write(string message)
{
LogToDatabase(message);
}
public override void WriteLine(string message)
{
LogToDatabase(message);
}
}
Now suppose an error occurs somewhere in the code. In catch block I want to do
Trace.TraceError(ex.ToString())
where ex is caught exception. Now the problem is that in my MyTraceListener the message parameters of Write method and WriteLine method are different. And even more interesting the string generated by ex.ToString() is passed as parameter in WriteLine method but not in Write.
Upvotes: 4
Views: 4059
Reputation: 94625
In fact Trace.TraceError() method invokes two methods: First it will execute Write() to print/write the source of an error along with error code and method WriteLine() to print/write error description.
PS: Before adding an instance of Custom TraceListener, Use Clear() method to remove default instance of trace listener.
Upvotes: 4