前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一个.NET开发的将WinForm崩溃报告发送到指定邮箱的库

一个.NET开发的将WinForm崩溃报告发送到指定邮箱的库

作者头像
郑子铭
发布2023-12-19 18:21:32
1040
发布2023-12-19 18:21:32
举报

今天给大家推荐一个.NET开发的,可以将winform应用的崩溃报告发送到指定邮箱的库CrashReporter.NET,其中包含完整的异常报告(如堆栈跟踪、异常类型、消息、源、.NET CLR 版本、OS 版本和应用程序版本)、堆栈跟踪和屏幕截图。

1

使用代码

先安装nuget包:

代码语言:javascript
复制
PM> Install-Package CrashReporter.NET.Official

2、在桌面应用程序中的Program.cs 文件中订阅 Application.ThreadException 和 AppDomain.CurrentDomain.UnhandledException,并如下所示。

代码语言:javascript
复制
static class Program
{
    private static ReportCrash _reportCrash;

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.ThreadException += (sender, args) => SendReport(args.Exception);
        AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
            {
                SendReport((Exception)args.ExceptionObject);
            };
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        _reportCrash = new ReportCrash("Email where you want to receive crash reports")
        {
            Silent = true,
            ShowScreenshotTab = true,
            IncludeScreenshot = false,
            #region Optional Configuration
            WebProxy = new WebProxy("Web proxy address, if needed"),
            AnalyzeWithDoctorDump = true,
            DoctorDumpSettings = new DoctorDumpSettings
            {
                ApplicationID = new Guid("Application ID you received from DrDump.com"),
                OpenReportInBrowser = true
            }
            #endregion
        };
        _reportCrash.RetryFailedReports();
        Application.Run(new FormMain());
    }

    public static void SendReport(Exception exception, string developerMessage = "")
    {
        _reportCrash.DeveloperMessage = developerMessage;
        _reportCrash.Silent = false;
        _reportCrash.Send(exception);
    }

    public static void SendReportSilently(Exception exception, string developerMessage = "")
    {
        _reportCrash.DeveloperMessage = developerMessage;
        _reportCrash.Silent = true;
        _reportCrash.Send(exception);
    }
}

只需在上面的示例中_reportCrash 中设置ToEmail和电子邮件即可开始接收崩溃报告。

如果要使用特殊消息处理单个异常的异常报告,可以这样写:

代码语言:javascript
复制
const string path = "test.txt";
try
{
    if (!File.Exists(path))
    {
        throw new FileNotFoundException(
            "File Not found when trying to write argument exception to the file", argumentException);
    }
}
catch (Exception exception)
{
    Program.SendReport(exception, "Value of path variable is " + path);
}

WPF中使用,需要在App.xaml.cs 文件中订阅 AppDomain.CurrentDomain.UnhandledException。

代码语言:javascript
复制
public partial class App : Application
{
    private static ReportCrash _reportCrash;

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
        Application.Current.DispatcherUnhandledException += DispatcherOnUnhandledException;
        TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
        _reportCrash = new ReportCrash("Email where you want to receive crash reports")
        {
            Silent = true
        };
        _reportCrash.RetryFailedReports();
    }

    private void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
    {
        SendReport(unobservedTaskExceptionEventArgs.Exception);
    }

    private void DispatcherOnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs dispatcherUnhandledExceptionEventArgs)
    {
        SendReport(dispatcherUnhandledExceptionEventArgs.Exception);
    }

    private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
    {
        SendReport((Exception)unhandledExceptionEventArgs.ExceptionObject);
    }

    public static void SendReport(Exception exception, string developerMessage = "")
    {
        _reportCrash.Silent = false;
        _reportCrash.Send(exception);
    }

    public static void SendReportSilently(Exception exception, string developerMessage = "")
    {
        _reportCrash.Silent = true;
        _reportCrash.Send(exception);
    }
}

同理,设置ToEmail和电子邮件即可开始接收崩溃报告。

2

配置选项

显示屏幕截图选项

设置ShowScreenshotTab 设置为 true

代码语言:javascript
复制
reportCrash.ShowScreenshotTab = true

以静默方式发送报告

可以通过将 Silent 属性设置为 true 以静默方式发送崩溃报告。

代码语言:javascript
复制
reportCrash.Silent = true;

使用 Web 代理发送报表

可以通过在 SendReport 方法中添加以下行来使用 Web 代理发送崩溃报告

代码语言:javascript
复制
reportCrash.WebProxy = new WebProxy("Web proxy address"),

使用 SMTP 将崩溃报告直接发送到电子邮件

可以使用 SMTP 服务器代替 DrDump 服务发送崩溃报告,如下所示。

代码语言:javascript
复制
var reportCrash = new ReportCrash
{
    AnalyzeWithDoctorDump = false,
    SmtpHost = "smtp.gmail.com",
    Port = 587,
    EnableSSL = true,
    UserName = "Your Gmail account email",
    Password = "Your Gmail account password",
    ToEmail = "Email address where you want receive crash reports",
    FromEmail = "Your Gmail account email or alias"
};
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-12-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DotNet NB 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云 BI
腾讯云 BI(Business Intelligence,BI)提供从数据源接入、数据建模到数据可视化分析全流程的BI能力,帮助经营者快速获取决策数据依据。系统采用敏捷自助式设计,使用者仅需通过简单拖拽即可完成原本复杂的报表开发过程,并支持报表的分享、推送等企业协作场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档