ASP.NET

Sentry provides an integration with ASP.NET through the Sentry.AspNet NuGet package.

Install

Add the Sentry dependency:

Copied
Install-Package Sentry.AspNet -Version 3.10.0

You can combine this integration with a logging library like log4net, NLog or Serilog to include both request data as well as your logs as breadcrumbs. The logging integrations also capture events when an error is logged.

Configuring

You configure the SDK in the Global.asax.cs:

Copied
using System;
using System.Web;
using Sentry;
using Sentry.AspNet;
using Sentry.EntityFramework; // if you also installed Sentry.EntityFramework
using Sentry.Extensibility;

public class MvcApplication : HttpApplication
{
    private IDisposable _sentry;

    protected void Application_Start()
    {
        // Initialize Sentry to capture AppDomain unhandled exceptions and more.
        _sentry = SentrySdk.Init(o =>
        {
            o.AddAspNet();
            o.Dsn = "https://examplePublicKey@o0.ingest.sentry.io/0";
            // When configuring for the first time, to see what the SDK is doing:
            o.Debug = true;
            // Set TracesSampleRate to 1.0 to capture 100%
            // of transactions for performance monitoring.
            // We recommend adjusting this value in production
            o.TracesSampleRate = 1.0;
            // if you also installed the Sentry.EntityFramework package
            o.AddEntityFramework();
        });
    }

    protected void Application_Error()
    {
        var exception = Server.GetLastError();
        SentrySdk.CaptureException(exception);
    }

    protected void Application_BeginRequest()
    {
        Context.StartSentryTransaction();
    }

    protected void Application_EndRequest()
    {
        Context.FinishSentryTransaction();
    }

    protected void Application_End()
    {
        // Flushes out events before shutting down.
        _sentry?.Dispose();
    }
}

Capturing the affected user

When opting-in to SendDefaultPii, the SDK will automatically read the user from the request by inspecting HttpContext.User. Default claim values like NameIdentifier for the Id will be used.

Copied
SentrySdk.Init(o =>
{
  o.AddAspNet();
  o.Dsn = "https://examplePublicKey@o0.ingest.sentry.io/0";
  // Opt-in to send things like UserId and UserName if a user is logged-in
  o.SendDefaultPii = true;
});

SendDefaultPii

Although this setting is part of the Sentry package, in the context of ASP.NET Core, it means reporting the user by reading the frameworks HttpContext.User. The strategy to create the SentryUser can be customized.

IncludeRequestPayload

It's helpful to troubleshoot a problem in the API when the payload that hit the endpoint is logged. Opt-in to send the request body to Sentry:

Copied
SentrySdk.Init(o =>
{
  o.AddAspNet(RequestSize.Always);
}

Troubleshooting

For information about Troubleshooting, please visit the troubleshooting page.

Samples

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