Skip to content
32 changes: 31 additions & 1 deletion platform-includes/distributed-tracing/how-to-use/dotnet.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,41 @@ If you use the current version of our .NET SDK, distributed tracing works automa
- ASP.NET Core
- Azure Functions Worker

When using ASP.NET you will need to update your `Application_BeginRequest` method:
<PlatformSection supported={["dotnet.aspnet"]}>

When using ASP.NET (not ASP.NET Core), you have to hook into the request lifecycle in `Global.asax.cs` to propagate traces. There are two options, depending on whether you want this application to create its own transactions and spans.

### Create Transactions and Continue the Trace (Recommended)

In most cases you'll want your ASP.NET application to show up in your distributed traces with its own transactions and spans. Use `StartSentryTransaction` in `Application_BeginRequest` and `FinishSentryTransaction` in `Application_EndRequest`. `StartSentryTransaction` continues any incoming trace and creates a transaction for the request.

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

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

### Continue the Trace Without Creating Transactions

If you only want to propagate an incoming trace to downstream services (for example, a database or another API) without your ASP.NET application creating any transactions or spans of its own, use `StartOrContinueTrace` instead:
Comment thread
jamescrosswell marked this conversation as resolved.
Comment thread
jamescrosswell marked this conversation as resolved.

```csharp
protected void Application_BeginRequest()
{
Context.StartOrContinueTrace();
}
```

<Alert level="warning">

Use either `StartSentryTransaction` or `StartOrContinueTrace`, not both. `StartOrContinueTrace` only propagates the trace and does not create a transaction, so if you replace `StartSentryTransaction` with it, your ASP.NET application will stop sending transactions to Sentry. Since `StartSentryTransaction` already continues the incoming trace, most applications should use that method.

</Alert>

</PlatformSection>
Loading