You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/en/tracing/trace_collection/custom_instrumentation/dotnet/dd-api.md
+54Lines changed: 54 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -181,6 +181,60 @@ public class ShoppingCartController : Controller
181
181
}
182
182
```
183
183
184
+
### Usage with ASP.NET `IHttpModule`
185
+
186
+
To access the current request span from a custom ASP.NET `IHttpModule`, it is best to read `Tracer.Instance.ActiveScope` in the `PreRequestHandlerExecute` event (or `AcquireRequestState` if you require session state).
187
+
188
+
While Datadog creates the request span at the start of the ASP.NET pipeline, the execution order of `IHttpModules` is not guaranteed. If your module runs before Datadog's, `ActiveScope` may be `null` during early events like `BeginRequest`. The `PreRequestHandlerExecute` event occurs late enough in the lifecycle to ensure the Datadog module has run and the span is available.
189
+
190
+
Keep in mind that ActiveScope can still be `null` for requests that are not traced (due to sampling or filtering) or when instrumentation is disabled.
191
+
192
+
```csharp
193
+
usingSystem;
194
+
usingSystem.Web;
195
+
usingDatadog.Trace;
196
+
197
+
publicclassMyCustomModule : IHttpModule
198
+
{
199
+
publicvoidInit(HttpApplicationcontext)
200
+
{
201
+
// Prefer reading ActiveScope late in the pipeline
To mark errors that occur in your code, use the `Span.SetException(Exception)` method. The method marks the span as an error and adds [related span metadata][5] to provide insight into the exception.
0 commit comments