Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup Sentry in .NET Azure functions

Sentry documentation only mentions the AspNetCore version

https://sentry.io/getcodepl/aspnet-core/getting-started/dotnet-aspnetcore/

like image 264
GetoX Avatar asked Sep 07 '25 01:09

GetoX


1 Answers

Solution depends on Azure function type. In-process or isolated

Isolated process Azure function

Microsoft.Azure.Functions.Worker v.2.x

Due to changes in Microsoft Azure SDK, version of Sentry.AspNetCore doesn't fit with the new Azure Function builder approach. But there is a workaround:

  1. Install package Install-Package Sentry.AspNetCore -Version 4.13.0
  2. Setup in Program.cs
using Sentry.Azure.Functions.Worker;
...
//NOTE: WORKAROUND
var context = new HostBuilderContext(new Dictionary<object, object>())
{
    Configuration = builder.Configuration,
};

builder.UseSentry(context, options =>
{
    options.Dsn = "https://[email protected]/0";
    // When configuring for the first time, to see what the SDK is doing:
    options.Debug = true;
    // Set traces_sample_rate to 1.0 to capture 100% of transactions for performance monitoring.
    // We recommend adjusting this value in production.
    options.TracesSampleRate = 1.0;
});

It should be fixed in Sentry.AspNetCore 5.x version.

Base on: https://github.com/getsentry/sentry-dotnet/issues/3773

Microsoft.Azure.Functions.Worker v.1.x

  1. Install package Install-Package Sentry.AspNetCore -Version 4.13.0
  2. Setup in Program.cs
using Sentry.Azure.Functions.Worker;

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults((host, builder) =>
    {
        builder.UseSentry(host, options =>
        {
            options.Dsn = "https://[email protected]/0";
            // When configuring for the first time, to see what the SDK is doing:
            options.Debug = true;
            // Set traces_sample_rate to 1.0 to capture 100% of transactions for performance monitoring.
            // We recommend adjusting this value in production.
            options.TracesSampleRate = 1.0;
        });
    })
    .Build();
await host.RunAsync();

Source

In process Azure function

  1. Install package Install-Package Sentry.AspNetCore
  2. Setup handling in azure function

        [FunctionName("Function1")]
        public static async Task RunAsync([TimerTrigger("0 */5 * * * *", RunOnStartup = true)] TimerInfo myTimer, ILogger log)
        {
            using (SentrySdk.Init())
            {
                try
                {
                   //Code
                }
                catch (Exception ex)
                {
                    SentrySdk.CaptureException(ex);
                    throw;  //if you want to pass it into Azure
                }
            }
        }
  1. Configure DSN in local.settings.json
  "Values": {
    "SENTRY_DSN": "https://your-dsn-url.ingest.sentry.io/xxx"
  }
like image 185
GetoX Avatar answered Sep 08 '25 21:09

GetoX