Setting up Raygun with Azure WebJobs

| 3 min. (448 words)

Azure WebJobs provide a nice, low-impact way to run scheduled or continuous programs against Azure Websites, with easy setup and a good monitoring dashboard. When you need small, occassional housekeeping tasks run against your sites, or constant jobs, using a WebJob removes the hassle of manually copying over console apps to the server and setting them up as scheduled tasks.

Packaging a new or pre-existing console app as a WebJob is pretty painless thanks to good VS2013 integration. It’s just as easy to integrate Raygun with a WebJob, giving you deep visibility into how all your processes are erroring out at runtime, without having to manually dig through logfiles.

Deploying a console WebJob

Firstly, to get a console app up on Azure you can follow the guide here. Once you’ve added the NuGet package, right-clicked on the project and gone through the wizard, a JSON settings file will be created – from here you can deploy to an Azure Website. The History tab in the dashboard will list when the console app was run (according to the schedule you set), which means it’s ready for Raygun integration to show what exceptions occurred.

Integrating Raygun into a WebJob

You’ll need to first grab the Raygun4NET package from NuGet:

Install-Package Mindscape.Raygun4Net

To set up Raygun4NET in a console app, you can hook up to the unhandled exception delegate:

static RaygunClient _client = new RaygunClient("your_apikey");
public static void Main(string[] args)
{
  AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

  // Your code here
  ThrowAnException();
}

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
  _client.Send(e.ExceptionObject as Exception);
}

That’s all there is to it! Completing this contrived example, that ThrowAnException method might look something like this:

private static void ThrowAnException()
{
  throw new VeryStrangeBug("From a webjob");
}

class VeryStrangeBug : Exception
{
  public VeryStrangeBug(string message) : base(message) { }
}

Deploying this project by right-clicking and selecting ‘Publish as Azure WebJob’ will make it available to be run when next scheduled. The resulting exception above will be caught and sent automatically, and the resulting Raygun report looks like this:

The full stacktrace is available, ready to be debugged. If you’ve chosen to get email notifications, and if it’s the first time this exception has been thrown, you’ll get an email informing you of this. If it’s occurred before you’ll get stats on how many times it’s been reported, and the trend.

Grab your Raygun trial

Got Raygun and Azure accounts already? Dive in and hook up your WebJobs to Raygun now! We’ve also got NuGet packages for MVC and WebAPI projects so you can easily set up your Azure projects with Raygun. And if you need a Raygun account, as always there’s the free trial available here.