Environment.FailFast 在 c# 应用程序中
Environment.FailFast in c# application
我想了解 c# 应用程序中的 Environment.FailFast
规则。所以,我编写了这段代码:
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";
}
}
据我所知,消息被写入 Windows application event log
并且应用程序终止。
当我 运行 应用程序并将 Exit
作为字符串时:
- 调试器指示错误:
- 我试图找到事件日志文件,如 msdn 中所示,但我没有找到
我不明白为什么应用程序没有抛出异常就没有关闭?对于第二点,如何在我的电脑中找到日志文件?
我已经用以下代码重构了您的问题:
static void Main(string[] args)
{
try
{
Console.WriteLine("Foo");
Environment.FailFast("WOHO!");
}
finally { }
}
当运行这个在调试器下时,我也没有看到任何异常注册。 运行 这个 没有调试器 (Ctrl + F5) 使异常正确地出现在事件查看器中(请参阅@aevitas 回答或阅读 this 以了解发生这种情况的原因) :
Environment.FailFast(string)
立即退出应用程序,不允许对象上有任何 catch
语句或终结器 运行。
只有当应用程序的状态处于永远无法恢复的状态时,才应使用此方法,退出应用程序是确保情况比以往更糟的唯一方法崩溃不会发生。在您的示例中,使用带有退出代码的 Environment.Exit
更合适,因为它看起来是一种优雅的退出,而不是强制退出损坏状态。
一般情况下,建议在附加调试器时不要使用 FailFast
- 这是 documented in the System.Diagnostics.Assert
class:
// 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.
这意味着您在 Visual Studio 中看到的异常是 CLR 中的错误,可以安全地忽略。
他们的解决方法如下:
if (Debugger.IsAttached)
Environment.Exit(COR_E_FAILFAST);
else
Environment.FailFast(message);
这意味着当 运行 附加调试器时,您不会写入事件日志消息,而在发行版中您会写入。
我想了解 c# 应用程序中的 Environment.FailFast
规则。所以,我编写了这段代码:
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";
}
}
据我所知,消息被写入 Windows application event log
并且应用程序终止。
当我 运行 应用程序并将 Exit
作为字符串时:
- 调试器指示错误:
- 我试图找到事件日志文件,如 msdn 中所示,但我没有找到
我不明白为什么应用程序没有抛出异常就没有关闭?对于第二点,如何在我的电脑中找到日志文件?
我已经用以下代码重构了您的问题:
static void Main(string[] args)
{
try
{
Console.WriteLine("Foo");
Environment.FailFast("WOHO!");
}
finally { }
}
当运行这个在调试器下时,我也没有看到任何异常注册。 运行 这个 没有调试器 (Ctrl + F5) 使异常正确地出现在事件查看器中(请参阅@aevitas 回答或阅读 this 以了解发生这种情况的原因) :
Environment.FailFast(string)
立即退出应用程序,不允许对象上有任何 catch
语句或终结器 运行。
只有当应用程序的状态处于永远无法恢复的状态时,才应使用此方法,退出应用程序是确保情况比以往更糟的唯一方法崩溃不会发生。在您的示例中,使用带有退出代码的 Environment.Exit
更合适,因为它看起来是一种优雅的退出,而不是强制退出损坏状态。
一般情况下,建议在附加调试器时不要使用 FailFast
- 这是 documented in the System.Diagnostics.Assert
class:
// 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.
这意味着您在 Visual Studio 中看到的异常是 CLR 中的错误,可以安全地忽略。
他们的解决方法如下:
if (Debugger.IsAttached)
Environment.Exit(COR_E_FAILFAST);
else
Environment.FailFast(message);
这意味着当 运行 附加调试器时,您不会写入事件日志消息,而在发行版中您会写入。