I am trying to implement a class for logging that utilizes System.Diagnostics.Trace
. I want to control when I want to write/flush data to file (listeners) so I want to disable AutoFlush = False
but since I am using static class, I wonder how I am supposed to flush all remaining data such that I will not lose any pending logs to be written to file when the app (holding the Trace class) is closed instantly.
Please advise.
Below is a snippet of my class.
namespace App
{
public class Trace
{
private Trace()
{
// prevent construction
}
Timer timer;
private timer_Elapsed()
{
// Flush all pending lines to log
System.Diagnostics.Trace.Flush()
}
static public void Initialize()
{
// Init
System.Diagnostics.Trace.Listeners.Add( Listener );
System.Diagnostics.Trace.AutoFlush = false;
}
static void Log(string str)
{
System.Diagnostics.Trace.WriteLine("str");
}
}
}