Custom implementation of the trace listener. Problems with writing and WriteLine

I want to implement a custom trace listener as shown below:

public class TraceListener : System.Diagnostics.TraceListener
{

    public override void Write(string message)
    {
      LogToDatabase(message);
    }

    public override void WriteLine(string message)
    {
      LogToDatabase(message);
    }
}

Now suppose that the error occurs somewhere in the code. In the catch block, I want to do

Trace.TraceError(ex.ToString()) 

where ex is excluded. Now the problem is that in MyTraceListener the message parameters of the Write and WriteLine methods are different. And even more interesting, the line generated by ex.ToString () is passed as a parameter in the WriteLine method, but not in Write.

+3
source share
1 answer

Trace.TraceError() : Write(), / WriteLine(), / .

PS: Custom TraceListener Clear(), .

+3

Source: https://habr.com/ru/post/1716198/


All Articles