gpt4 book ai didi

c# - 发布 .NET 程序崩溃

转载 作者:行者123 更新时间:2023-12-03 17:45:36 25 4
gpt4 key购买 nike

我有一个已完成并在客户机器上运行的 .NET 应用程序。我收到报告说它偶尔会因 Windows 错误框而崩溃

xxxProgram has stop working,一个问题导致程序停止正常工作。如果有可用的解决方案,Windows 将关闭该程序并通知您。

我正在尝试跟踪此问题的根源。我认为这是某处未处理的异常,但我很难找到位置。我正在寻找有关跟踪源的帮助的输入。尽管该程序是 .NET,但我使用了一些非托管资源,例如 OPC 和我编写的一个小包装器,但我觉得我很好地涵盖了编码。该应用程序几乎 24/7 运行,目前它可能每 4-10 天崩溃一次。

我正在考虑的一件事是订阅 AppDomain.UnhandledExceptionEventHandler并将堆栈跟踪记录到文件中。这是可能的吗?人们将如何去做?

谢谢

最佳答案

我会创建一个日志系统。即使它只是从页面中获取基本错误的尝试。我之前使用下面的代码在服务器上存储日志文件。

public void logError(string errorString)
{
try
{
// Takes unknown errors and logs them to the log file
string user = HttpContext.Current.User.Identity.Name.ToString();
string FileLine = user + ", " + DateTime.Now.ToString() + ", " + errorString;
string filename = HttpContext.Current.Server.MapPath("../Log/error_log.txt");

using (System.IO.StreamWriter file = new System.IO.StreamWriter(filename, true))
{
file.WriteLine(FileLine);
file.WriteLine("---------------------------------------");
}
}
catch (Exception ex)
{
// send email, or another notification
}

}

您可以称其为:
try
{
//function code
}
catch (Exception ex)
{
logError(ex.ToString());
}

关于c# - 发布 .NET 程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36942564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com