/// <summary> /// A simple helper method to create a listener with an event pre-registered /// for receiving trace data. /// </summary> /// <param name="lineAction">Simplified action to perform when trace data /// is received.</param> /// <returns>The newly-created listener, NOT yet registered.</returns> public static EventTraceListener Create(Action <string> lineAction) { EventTraceListener TL = new EventTraceListener(); TL.MessageWritten += (traceSender, tracee) => lineAction.Invoke(tracee.Data); return(TL); }
/// <summary> /// A wrapper for the Main function of a console application. It /// will execute the passed-in code with proper message trapping, display /// trace and message information to the console, and return the correct /// return value for Main, based on whether a fatal error occurred. /// </summary> /// <param name="mainProcess">The code to actually run (in essence, /// the body of the entire console application).</param> /// <returns>The value Main() should return for the process: 0 if /// all is well, and 1 if an error occurred.</returns> public static int ExecuteWrapped(Action mainProcess) { EventTraceListener DebugListener = EventTraceListener.CreateFormatted(L => Console.WriteLine(L)); try { DebugListener.Register(); try { mainProcess.Invoke(); } catch (Exception ex) { Trace.WriteLine(string.Empty); Trace.WriteLine(EXCEPTION_DEBUG_LINE); Trace.WriteLine(ex.ExceptionDump()); return(1); } return(0); } finally { DebugListener.Unregister(); } }