Included Instrumentation

ASP.NET integration, once enabled, captures each incoming HTTP request and turns it into a transaction with the help of a custom middleware. To enable tracing on ASP.NET, you need to update your Global.asax.cs file like this:

Copied
using System;
using System.Configuration;
using System.Web.Mvc;
using System.Web.Routing;
using Sentry;
using Sentry.AspNet;

namespace AspNetMvc
{
    public class MvcApplication : System.Web.HttpApplication
    {
        private IDisposable _sentry;

        protected void Application_Start()
        {
            // Set up the sentry SDK
            _sentry = SentrySdk.Init(o =>
            {
                // We store the DSN inside Web.config; make sure to use your own DSN!
                o.Dsn = https://examplePublicKey@o0.ingest.sentry.io/0

                // Get ASP.NET integration
                o.AddAspNet();

                // When configuring for the first time, to see what the SDK is doing:
                o.Debug = true;

                // This option will enable Sentry's tracing features. You still need to start transactions and spans.
                o.EnableTracing = true;

                // Example sample rate for your transactions: captures 10% of transactions
                o.TracesSampleRate = 0.1;
            });
        }

        // Global error catcher
        protected void Application_Error() => Server.CaptureLastError();

        protected void Application_End()
        {
            // Close the Sentry SDK (flushes queued events to Sentry)
            _sentry?.Dispose();
        }

        protected void Application_BeginRequest()
        {
            // Start a transaction that encompasses the current request
            Context.StartSentryTransaction();
        }

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

Transaction names follow the pattern <HTTP method> <Route>; for example, a request to the following action will create a transaction named GET /person/{id}:

Copied
using System;
using System.Web.Mvc;

public class HomeController : Controller
{
    [HttpGet("/person/{id}")]
    public ActionResult Person(string id) { /* ... */ }
}

Transactions created by this integration are automatically added to the current scope. If you want to start your own spans from the automatically created transaction, you can use the following approach:

Copied
using System;
using System.Web.Mvc;
using Sentry;

public class HomeController : Controller
{
    [HttpGet("/person/{id}")]
    public ActionResult Person(string id)
    {
        var childSpan = SentrySdk.GetSpan()?.StartChild("additional-work");
        try
        {
            // Do the work that gets measured.

            childSpan?.Finish(SpanStatus.Ok);
        }
        catch (Exception e)
        {
            childSpan?.Finish(e);
            throw;
        }
    }
}
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").