Raygun support for .Net2.0

| 3 min. (597 words)

Today we have released a new version of the Raygun4Net provider to support desktop and web projects that target .Net2.0. This has been included in the existing NuGet package, and all the source code is available on GitHub. This single package gives you error reporting for console applications, WinForms and ASP.Net projects. It only takes a few minutes to setup, and then your application will automatically send error reports to your Raygun.com account. All the error reporting information available in the modern .Net Raygun support is available in the .Net2.0 provider. This includes all the stack trace information, application version, details about the environment such as operating system and available memory, and even request and response information if it’s a web application.

Now that we have .Net2.0 support, We’ll be able to write providers for Unity3D – a multi-platform video game framework. Support for Unity3D coming soon!

How to send all unhandled exceptions

First, install Raygun4Net into your project. We recommend using NuGet to do this – right click the project and select Manage NuGet Packages, search for Raygun4Net and install. Note that for all the code examples seen below, the API key is given to you when you create a new application on Raygun.com.

Console application

If you have a console application, hook an event handler to AppDomain.CurrentDomain.UnhandledException. In the event handler, create a new instance of RaygunClient using your api key and then send the exception.

public class Program
{
  static void Main(string[] args)
  {
    AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  }

  private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  {
    RaygunClient client = new RaygunClient("YOUR_APP_API_KEY");
    client.Send((Exception)e.ExceptionObject);
  }
}

WinForms

Setting up Raygun in WinForms is similar to a console application, but it’s also a good idea to listen to Application.ThreadException. Also, make sure to attach the event handlers before the Run method is called!

static class Program
{
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Application.ThreadException += Application_ThreadException;
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    Application.Run(new MainForm());
  }

  private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  {
    RaygunClient client = new RaygunClient("YOUR_APP_API_KEY");
    client.Send((Exception)e.ExceptionObject);
  }

  private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  {
    RaygunClient client = new RaygunClient("YOUR_APP_API_KEY");
    client.Send(e.Exception);
  }
}

ASP.Net

For an ASP.Net project, Raygun can be setup entirely within Web.Config. First, add the following line within the configSelections tag:

<section name="RaygunSettings" type="Mindscape.Raygun4Net.RaygunSettings, Mindscape.Raygun4Net" />

Next, add the RaygunSettings block somewhere within the configuration tag:

<RaygunSettings apikey="YOUR_APP_API_KEY" />

This is enough to setup Raygun and start sending exceptions in code via the Send or SendInBackground methods. By taking one more step, we can setup Raygun to automatically send all unhandled exceptions. Simply add the following code to the system.webServer tag:

<modules>
  <add name="RaygunErrorModule" type="Mindscape.Raygun4Net.RaygunHttpModule"/>
</modules>

And that’s it! The RaygunSettings configuration block has additional options such as excludeHttpStatusCodes to specify exceptions to ignore such as 404s, ignoreFormDataNames to prevent raygun sending sensitive http form values, and excludeErrorsFromLocal to disable Raygun when debugging on your local machine.

No matter which platform you are using, you can also use the Send or SendInBackground methods on the RaygunClient to send exceptions manually. This is especially useful for sending handled exceptions – ones that don’t crash the application, but you still want to know about them. Other features you may be interested in is sending custom data, a list of tags, or specifying a user identity string so that you can see how many people an error is affecting. Find out more in the documentation.

If you have any questions or suggestions, we’d love to hear from you. Try the free 14 day trial to start blasting away your errors today. No credit card required.