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

.NET
227
0
0
2024-02-15

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

1

使用代码

先安装nuget包:

PM> Install-Package CrashReporter.NET.Official

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

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和电子邮件即可开始接收崩溃报告。

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

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。

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

reportCrash.ShowScreenshotTab = true

以静默方式发送报告

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

reportCrash.Silent = true;

使用 Web 代理发送报表

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

reportCrash.WebProxy = new WebProxy("Web proxy address"),

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

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

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"
};