Sentry for Azure Functions (.NET)

Learn about Sentry's .NET integration with Azure Functions.

Sentry provides an integration with Azure Functions through the Sentry.Extensions.Hosting and Sentry.OpenTelemetry.Exporter NuGet packages.

Add the Sentry dependencies to your Azure Functions application:

Copied
dotnet add package Sentry.Extensions.Hosting -v 7.0.0
dotnet add package Sentry.OpenTelemetry.Exporter -v 7.0.0
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.Http

This package extends Sentry.Extensions.Logging. This means you also get the ILogger<T> integration and the other core features available in the Sentry SDK.

Error Monitoring starts working as soon as you call UseSentry on the host builder. Logs sent through Microsoft.Extensions.Logging need EnableLogs = true, and Application Metrics need no extra configuration. You'll learn how to send them in Send Logs and Send Application Metrics.

UseSentry starts Sentry when the host is built and closes it when the host stops. It also adds Sentry's ILogger integration, which captures LogError and higher as events. See the Generic Host guide for its configuration options.

This guide also enables Tracing through OpenTelemetry. Sentry's OTLP exporter is added to the OpenTelemetry TracerProviderBuilder, and UseOtlp() tells the Sentry SDK to use OpenTelemetry for tracing.

The snippet also turns on:

  • Tracing: via AddSentryOtlpExporter and UseOtlp(). Don't also set TracesSampleRate; OpenTelemetry handles tracing. See OpenTelemetry (OTLP) for sampling and trace propagation.
  • Logs: via EnableLogs

Don't need a feature? Remove its options from the snippet.

Copied
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Trace;

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .UseSentry(options =>
    {
        options.Dsn = "___PUBLIC_DSN___";
        options.EnableLogs = true;
        options.UseOtlp(); // <-- Configure Sentry to use OpenTelemetry for tracing
        options.Debug = true;
    })
    .ConfigureServices(services =>
    {
        services.AddOpenTelemetry().WithTracing(builder =>
        {
            builder
                .AddSentryOtlpExporter("___PUBLIC_DSN___") // <-- Send traces to Sentry over OTLP
                .AddHttpClientInstrumentation(); // From OpenTelemetry.Instrumentation.Http, instruments outgoing HTTP requests
        });
    })
    .Build();

await host.RunAsync();

Apps that use the ASP.NET Core integration, with ConfigureFunctionsWebApplication() instead of ConfigureFunctionsWorkerDefaults(), call the same UseSentry, not the one from Sentry.AspNetCore.

Use Microsoft.Extensions.Logging to send structured logs from your application. Set EnableLogs = true in the options you pass to UseSentry.

See Generic Host for setup details.

See Set Up Logs for all available logging methods and formatting options.

Copied
using Microsoft.Extensions.Logging;

public sealed class OrderService
{
    private readonly ILogger<OrderService> _logger;

    public OrderService(ILogger<OrderService> logger) => _logger = logger;

    public void Process()
    {
        _logger.LogInformation("Order processed {OrderId}", "order-123");
        _logger.LogWarning("Rate limit reached for {Endpoint}", "/api/orders");
    }
}

Metrics are enabled by default in Sentry .NET SDK 6.1.0 and later. Use SentrySdk.Metrics to send counters, gauges, and distributions from your app.

See Set Up Metrics for all available metric types and configuration options.

Copied
SentrySdk.Metrics.EmitCounter("button_click", 5,
    new Dictionary<string, object> { ["browser"] = "Firefox" });
SentrySdk.Metrics.EmitGauge("page_load", 15.0, MeasurementUnit.Duration.Millisecond,
    new Dictionary<string, object> { ["page"] = "/home" });
SentrySdk.Metrics.EmitDistribution("api_latency", 187, MeasurementUnit.Duration.Millisecond,
    new Dictionary<string, object> { ["endpoint"] = "/api/orders" });

Let's test your setup and confirm that data reaches your Sentry project.

Copied
try
{
    throw null;
}
catch (Exception ex)
{
    SentrySdk.CaptureException(ex);
}

Run any log and metric examples you've added after SDK initialization. To verify automatic tracing, exercise an instrumented operation, such as an incoming HTTP request or a supported UI interaction.

Logs and metrics are batched. In an async test method, flush pending data before a short-lived test exits:

Copied
await SentrySdk.FlushAsync();

Check Issues for the captured error, Logs for log messages, Metrics for application metrics, and Traces for sampled transactions. Error capture alone doesn't verify the other features.

At this point, you should have integrated Sentry into your Azure Functions application. Here's what to explore next:

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").