Skip to content

Sample Usage

Peter van der Woude edited this page Jul 9, 2021 · 48 revisions

To include ExceptionReporter.NET within your application either add a Reference to ExceptionReporter.NET.dll to your project (and manually include the dependencies of DotNetZip, SimpleMapi and Handlebars.NET) or use Nuget.

Install-Package ExceptionReporter

Directly Invoke

You might have a high-level point in the code where most exceptions will be thrown. In this case, simply catch the Exception and invoke ExceptionReporter.NET.

Here's a medium-sized example:

 using ExceptionReporting;
 
 try 
 {
    //... some code that can throw
 }
 catch (Exception exception) 
 {
     var er = new ExceptionReporter 
     {
       Config = 
       {
         AppName = "PhotoFuzz",
         CompanyName = "Fuzz Pty Ltd",
         TitleText = "PhotoFuzz Error Report",
         EmailReportAddress = "support@fuzz.com",
         TakeScreenshot = true,   // attached if sending email
         FilesToAttach = new[] { "c:/app/config.ini", "c:/app/error.log" },
         SendMethod = ReportSendMethod.SMTP,  // also WebService/SimpleMAPI
         SmtpServer = "127.0.0.1",
         SmtpPort = 2500,
         SmtpUsername = "jon",
         SmtpPassword = "1234",
         SmtpFromAddress = "test@test.com"      
       }
     }
     
     // files to attach are automatically zipped into a single file and 
     // attached, if sending via email (SMTP/SimpleMAPI)
     
     er.Show(exception);    // can also pass multiple exceptions here  
     // er.Send(exception)  // sends without showing dialog (SMTP/WebService)
  }

WinForms

Attach an event handler to the Application.ThreadException event before calling Application.Run()

using ExceptionReporting;
 
[STAThread]
static void Main()
{ 
  // Add the event handler for handling UI thread exceptions to the event.
  Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

  // Set the unhandled exception mode to force all Windows Forms errors to go through our handler.
  Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

  // Add the event handler for handling non-UI thread exceptions to the event. 
  AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  //...
  Application.Run(new Form1());
}

private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
  ExceptionReporter er = new ExceptionReporter();
  er.Show(e.Exception);
}

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
  // without thread it won't be possible to copy to clipboard or open save file dialog because 
  // it must occur on the STA Thread
  var thread = new Thread(() =>
  {
    ExceptionReporter er = new ExceptionReporter();
    er.Show((Exception)e.ExceptionObject);
  });
  thread.SetApartmentState(ApartmentState.STA);
  thread.Start();
  thread.Join();
}

WPF

Attach an event handler to the DispatcherUnhandledException event

public App()
{
  DispatcherUnhandledException += OnDispatcherUnhandledException;
}

void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
  e.Handled = true;
  ExceptionReporter er = new ExceptionReporter();
  er.Show(e.Exception);
}