I have written a custom trace listener extending TextWriterTraceListener.
Now, how do I call the Dispose() on the listener? I add it through the app.config of my project.
Tried adding the call Dispose(false) in the finalizer, but it not being called. Weirdly, it is being called in VS 2010 but not when I run the application, but I know that GC collection is not guaranteed. ( basically this is being used in nunit tests to log System.Net.Socket calls from the tests and I need to do some post processing after all the tests are run and write that to the log as well. I added this part in Dispose() )
App.config snippet:
<sources>
<source name="System.Net.Sockets" tracemode="protocolonly" maxdatasize="10240">
<listeners>
<add name="CustomTraceListener" type="Tests.Custom.MyTest.CustomTraceListener, Tests.Custom.MyTest" initializeData="custom.log" />
</listeners>
</source>
</sources>
Some code snippets:
~CustomTraceListener()
{
Dispose(false);
}
protected override void Dispose (bool disposing)
{
base.Write(customProcessor.PostProcess());
base.Flush();
base.Dispose(disposing);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
I had to write a SetUpFixture and in the TearDown, used some reflection to get type “System.Diagnostics.TraceSource” from the assembly, got a private static field which was a list of all trace sources and called Dispose() on my custom listener.
Call Dispose() on all listeners on TraceSource