Logging to elmah.io from Google Cloud Functions

Logging to elmah.io from Google Cloud Functions uses our integration with Microsoft.Extensions.Logging. To start logging, install the Elmah.Io.Extensions.Logging NuGet package through the Cloud Shell or locally:

Install-Package Elmah.Io.Extensions.Logging
dotnet add package Elmah.Io.Extensions.Logging
<PackageReference Include="Elmah.Io.Extensions.Logging" Version="5.*" />
paket add Elmah.Io.Extensions.Logging

In the root of your Function app create a new file named Startup.cs:

public class Startup : FunctionsStartup
{
    public override void ConfigureServices(WebHostBuilderContext ctx, IServiceCollection services)
    {
        services.AddLogging(logging =>
        {
            logging.AddElmahIo(o =>
            {
                o.ApiKey = "API_KEY";
                o.LogId = new Guid("LOG_ID");
            });
            logging.AddFilter<ElmahIoLoggerProvider>(null, LogLevel.Warning);
        });
    }
}

Replace API_KEY with your API key (Where is my API key?) and LOG_ID with the ID of the log to store messages in (Where is my log ID?).

The filter tells Google Cloud Functions to log warnings and above to elmah.io only. If you want to log detailed information about what goes on inside Google Cloud Functions, you can lower the log level.

Decorate your function with a FunctionsStartup attribute:

[FunctionsStartup(typeof(Startup))]    
public class Function : IHttpFunction
{
    // ...
}

All uncaught exceptions happening in your function as well as log messages sent from Google Cloud Functions are now stored in elmah.io.

To log messages manually, you can inject an ILogger in your function:

public class Function : IHttpFunction
{
    private ILogger<Function> _logger;

    public Function(ILogger<Function> logger)
    {
        _logger = logger;
    }

    // ...
}

Then log messages using the injected logger:

_logger.LogWarning("Your log message goes here");


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