class Program : CriticalFinalizerObject
{
static void Main(string[] args)
{
Program p = new Program();
TextWriterTraceListener listener = new TextWriterTraceListener(@"C:\trace.txt");
Trace.Listeners.Clear(); // Remove default trace listener
Trace.Listeners.Add(listener);
Trace.WriteLine("First Trace"); // Generate some trace messages
Trace.WriteLine("Perhaps last Trace.");
}
~Program()
{
Trace.Close();
}
}
I get file size =0
the finilizer should have executed because I derive from CriticalFinalizerObject
I don't want to use Trace.Close() not in the Finalizer.
edit
after @eric Lippert reply : Ive re-edited the code trying to match it to :constrained execution region ( but still no success)
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
class Program : CriticalFinalizerObject
{
static void Main(string[] args)
{
RuntimeHelpers.PrepareConstrainedRegions();
try
{
}
catch (Exception e)
{
}
finally
{
Program p = new Program();
TextWriterTraceListener listener = new TextWriterTraceListener(@"C:\trace1.txt");
Trace.Listeners.Clear();
Trace.Listeners.Add(listener);
Trace.WriteLine("First Trace");
Trace.WriteLine("Perhaps last Trace.");
}
}
~Program()
{
Trace.Flush();
}
}
Trace.Flush()
in your main method. If I am understanding the docs correctly,Trace.Close
may not call flush on the underlying listener. – LinnealinneanTrace.Close
, the file is not saved! Seems like an issue ofTrace.Close
getting executed in a finiliser thread. – Lipkin