Environment.FailFast in C # Application

I would like to understand a rule Environment.FailFast

in a C # application. So, I made this code:

  public static void Main()
   {
       string strInput = GetString();
       Console.WriteLine(strInput);
       Console.ReadKey();
   }

   private static string GetString()
   {
       Console.WriteLine("Get string");
       string s = Console.ReadLine();
       try
       {
           if (s == "Exit")
           {
                Environment.FailFast("Erreur fatale");
                return s;
           }
           else
           {
               return s;
           }
       }
       catch (Exception)
       {
           return "catch";
       }
       finally
       {
           s += "finally";
       }
   }

      

As I found out, the message is written to Windows application event log

and the application exits.

When I ran the application and put Exit

in the line:

  • The debugger points out the error:

enter image description here

  • I am trying to find the event log file as mentioned in msdn , but I cannot find it enter image description here

I don't understand why the application didn't close without throwing an exception? For the second point: How do I find the log file on my PC?

+3


source to share


2 answers


Environment.FailFast(string)

exits the application immediately, not allowing any statements catch

or finalizers to run on the object.

This method should only be used if your application state is at a point where it can never be restored , and exiting the application is the only way to ensure that something is much worse than crashing. In your example, use Environment.Exit

with an exit code is more appropriate, since it looks like a graceful exit rather than one enforced corrupt state.

In general, it is recommended not to use it FailFast

when connecting a debugger - this is documented in the classSystem.Diagnostics.Assert

:

// However, in CLR v4, Environment.FailFast when a debugger is attached gives you an MDA 
// saying you've hit a bug in the runtime or unsafe managed code, and this is most likely caused 
// by heap corruption or a stack imbalance from COM Interop or P/Invoke.  That extremely
// misleading error isn't right, and we can temporarily work around this by using Environment.Exit 
// if a debugger is attached.  The right fix is to plumb FailFast correctly through our native
// Watson code, adding in a TypeOfReportedError for fatal managed errors.  We may want a contract-
// specific code path as well, using COR_E_CODECONTRACTFAILED.

      



This means that the exception you see in Visual Studio is due to a bug in the CLR and can be safely ignored.

Their work around:

if (Debugger.IsAttached) 
    Environment.Exit(COR_E_FAILFAST);
else
    Environment.FailFast(message); 

      

This means that while working with the attached debugger, you will not receive the "Event Log" message recorded while in release you are doing.

+5


source


I have reverse engineered your problem with the following piece of code:

static void Main(string[] args)
{
    try
    {
        Console.WriteLine("Foo");
        Environment.FailFast("WOHO!");
    }
    finally { }
}

      



When running this under the debugger, I didn't see any logged exceptions. Doing this without a debugger (Ctrl + F5) throws the exception in the event viewer correctly (see @aevitas answer or read this for why this happens):

Event Viewer with the exception message

+4


source







All Articles