title: Why my Close function isn't called?
body: <pre><code> 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();
          }
      }
  </code></pre>
  
  <p>I get file size =0</p>
  
  <p>the finilizer <strong>should</strong> have executed because I derive from <code>CriticalFinalizerObject</code></p>
  
  <p><em>I don't want to use Trace.Close() not in the Finalizer.</em></p>
  
  <h2>edit</h2>
  
  <p>after @eric Lippert reply : Ive re-edited the code trying to match it to :<strong>constrained execution region</strong>  ( but still <strong>no</strong> success)</p>
  
  <pre><code>  [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();
          }
      }
  </code></pre>