title: A TraceListener, which throws an exception on a failed assertion
body: <p>My problem with <code>DefaultTraceListener</code> (which is the only trace listener, if not overridden in app.config) is that, if <code>AssertUiEnabled</code> is <code>false</code> (i.e. in ASP.NET) it writes a message as <code>OutputDebugString</code> on failed assertions (calls to <code>Trace.Assert(false)</code>), <strong>but continues the execution</strong>.</p>
  
  <p>So I'm using the following sub-class of <code>TraceListener</code>, which instead throws an exception. I activate it using </p>
  
  <pre><code>TraceListenerWhichThrowsExceptionOnFail.InsertAsFirstTraceListener(Trace.Listeners);
  </code></pre>
  
  <p>in <code>Application_Init</code>.</p>
  
  <p>Now, in the <code>Application_Error</code> event I can log the exception (as any exception) with the full stack trace, including the call of <code>Trace.Assert(false)</code>. </p>
  
  <pre><code>public class TraceListenerWhichThrowsExceptionOnFail : TraceListener
  {
      public static void InsertAsFirstTraceListener(TraceListenerCollection traceListeners)
      {
          traceListeners.Insert(0, new TraceListenerWhichThrowsExceptionOnFail());
      }
  
      public override void Fail(string message, string detailMessage)
      {
          message = string.IsNullOrEmpty(detailMessage) ? message : message + ", Detail message: " + detailMessage;
  
          throw new ApplicationException("Trace assertion failed" + 
              (string.IsNullOrEmpty(message) ? "" : ": " + message) + ".");
      }
  
       public override void Write(string message)
       {
          // NOP
       }
  
       public override void WriteLine(string message)
       {
          // NOP
       }
  }
  </code></pre>
  
  <p>Now my question: Does anyone see a problem with this approach?</p>