C#在安装后不抛出异常
问题描述:
我们使用Main()
中的try-catch-block来捕获应用程序的所有异常,并将它们写入到lgo文件中。在VS2012中的发布配置中一切正常。抛出异常并将其保存到文件中。C#在安装后不抛出异常
使用WIX创建安装程序并安装应用程序后,直接引发异常(并显示为对话框),但未被Main()中的try-catch-block捕获,因此不再保存在文件中。我如何在Main()
中发现异常?
这是Program.cs中的代码:
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
catch (Exception ex)
{
ExceptionLog.SaveExceptionCloseApp(Program.RuntimeGUID, System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex);
}
}
答
这为我们工作:
@Hans帕桑特:谢谢你的解决方案。
[STAThread]
static void Main()
{
Application.ThreadException += Application_ThreadException;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
ExceptionLog.SaveExceptionCloseApp(Program.RuntimeGUID, System.Reflection.MethodBase.GetCurrentMethod().Name + "()", e.Exception);
}
你是什么意思“保存到文件”?你到底在做什么?为我们提供一些在调试时工作的代码,但不是在安装之后。 –
Program.cs – user2952145
的附加代码是否会发生异常,因为一旦您通过WiX生成的MSI安装了应用程序,安装文件夹不包含所有必需的二进制文件?然后执行会在* Main之前失败*,所以不会被try/catch块捕获。我还建议使用FusLogVw工具来检查是否所有需要的组件都可用。 –