Build status NuGet Samples

Logging to elmah.io from Azure Functions

Logging errors from Azure Functions, requires only a few lines of code. To start logging exceptions from a Function, choose one of two methods:

Manually using Elmah.Io.Client (the stable choice)

Install the Elmah.Io.Client NuGet package into your Function project:

Install-Package Elmah.Io.Client

Wrap your Function code in try-catch:

public class Function
{
    public static void Run()
    {
        try
        {
            // Business logic goes here
        }
        catch (Exception e)
        {
            var logger = ElmahioAPI.Create("API_KEY");
            client.Messages.Error(new Guid("LOG_ID"), e, "An error message");
            throw;
        }
    }
}

Remember to replace API_KEY with your API key (Where is my API key?) and LOG_ID (Where is my log ID?) with the ID of the log you want to log to.

By re-throwing the catched exception, Azure Function features like retry works smoothly.

Automatic using Elmah.Io.Functions (the prerelease choice)

We've created a client specifically for Azure Functions.

Install the newest Elmah.Io.Functions package in your Azure Functions v2 project:

Install-Package Elmah.Io.Functions -Pre

Log all uncaught exceptions using the ElmahIoExceptionFilter attribute:

Before you start, make sure to install Microsoft.Azure.WebJobs version 2.1.0-beta4 into your Function App:

Install-Package Microsoft.Azure.WebJobs -Version 2.1.0-beta4 -Pre

Then install the Elmah.Io.Functions package:

Install-Package Elmah.Io.Functions -Pre

Log all uncaught exceptions using the ElmahIoExceptionFilter attribute:

[ElmahIoExceptionFilter("API_KEY", "LOG_ID")]
public static class Function1
{
    [FunctionName("Function1")]
    public static void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, TraceWriter log)
    {
        throw new Exception("Some exception");
    }
}

Replace API_KEY with your API key (Where is my API key?) and LOG_ID (Where is my log ID?) with your log ID.

If your function method is declared as async, remember to change the return type to Task. Without it, the function host never invoke ElmahIoExceptionFilter.

The filter also supports config variables:

[ElmahIoExceptionFilter("%apiKey%", "%logId%")]

The variables above, would require you to add your API key and log ID to your settings.json:

{
  "Values": {
    "apiKey": "API_KEY",
    "logId": "LOG_ID"
  }
}

This article was brought to you by the elmah.io team. elmah.io is the best error management system for .NET web applications. We monitor your website, alert you when errors start happening and help you fix errors fast.

See how we can help you monitor your website for crashes Monitor your website