通过.NET桌面应用程序发送错误日志

发布于 2024-08-28 06:35:46 字数 153 浏览 8 评论 0原文

最近,我们的客户遇到了意想不到的崩溃。我们已经在他们的本地计算机上记录了错误。是否有一种机制可以让他们在应用程序崩溃或发生意外行为时以某种方式“发送错误日志”?

换句话说,我如何知道应用程序冻结、挂起或崩溃,以便我可以发送某些内容,并覆盖正常的“未响应”Windows 消息?

Lately our customers are experiencing unexpected crashes. We are already logging the errors on their local machines. Is there a mechanism to enable them to "send error log" somehow when the application crashes or when unexpected behavior takes place?

In other word how do I know that the application freezed or hung or crashed so I can send something, and override the normal "not responding" windows message?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒解孤独 2024-09-04 06:35:47

我认为如果它挂起,您无能为力,但如果它崩溃了,您可能可以使用 UnhandledExceptionEventHandler

您可以通过检查应用程序启动时是否正确关闭并向日志写入“关闭”消息来处理挂起,如果没有,您可以询问用户是否要将日志发送到你。

I don't think there's much you can do if it's hung but if it crashes you might be able to catch some of those crashes with the UnhandledExceptionEventHandler.

You could possibly handle the hangs by checking when the application starts up if it previously shut down correctly and wrote a "shutting down" message to the log, and if it didn't you could ask the user if they want to send the log to you.

弱骨蛰伏 2024-09-04 06:35:47

过去,我很幸运地使用如下代码将异常记录到 Web 服务(只要允许客户端注销到互联网)。这是为了记录您尚未捕获的任何内容。如果您在发布模式下编译应用程序,但还包含 pdb 文件,您将获得带有行号的堆栈跟踪。

您还应该记录程序集的版本,以了解哪个版本的应用程序给您带来错误。

public void RegisterHandlers()
{
    Application.ThreadException += new ThreadExceptionEventHandler(ThreadExceptionFunction);
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionFunction);
}

private void UnhandledExceptionFunction(object sender, UnhandledExceptionEventArgs args)
{
    Exception e = (Exception)args.ExceptionObject;
    ExceptionLogger(e.StackTrace);
}

private void ThreadExceptionFunction(object sender, ThreadExceptionEventArgs args)
{
    ExceptionLogger(args.Exception.StackTrace);
}

private void ExceptionLogger(string trace)
{
    // log the message to a webservice
}

In the past I've had luck with logging exceptions to a webservice (as long as the client are allowed to log out to the internet) with code like the one below. This is for logging anything you're not already catching. If you compile your application in release mode but also include the pdb files, you will get a stacktrace with line numbers.

You should also log the version of your assembly to know what version of the application is giving you errors.

public void RegisterHandlers()
{
    Application.ThreadException += new ThreadExceptionEventHandler(ThreadExceptionFunction);
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionFunction);
}

private void UnhandledExceptionFunction(object sender, UnhandledExceptionEventArgs args)
{
    Exception e = (Exception)args.ExceptionObject;
    ExceptionLogger(e.StackTrace);
}

private void ThreadExceptionFunction(object sender, ThreadExceptionEventArgs args)
{
    ExceptionLogger(args.Exception.StackTrace);
}

private void ExceptionLogger(string trace)
{
    // log the message to a webservice
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文