---
title: ".NET Continuous Profiler: Exception and lock contention"
description: "Datadog's .NET continuous profiler captures thrown exceptions through ICorProfilerCallback::ExceptionThrown and reads each exception's _message field by resolving System.Exception's ClassID from CoreLib metadata and computing the field offset with GetClassLayout."
url: "https://swe.quest/articles/net-continuous-profiler-exception-and-lock-contention-93247ad1a6e667eb"
canonical: "https://www.datadoghq.com/blog/engineering/dotnet-continuous-profiler-part-3/"
source: "Datadog Engineering"
published: "2024-04-04"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 8
tags: ["dotnet","performance","observability","concurrency","runtimes"]
---

# .NET Continuous Profiler: Exception and lock contention

A post on [Datadog Engineering](https://swe.quest/sources/datadog.md), published 2024-04-04, 8 min read, Deep dive. Original: [https://www.datadoghq.com/blog/engineering/dotnet-continuous-profiler-part-3/](https://www.datadoghq.com/blog/engineering/dotnet-continuous-profiler-part-3/)

## Summary

Datadog's .NET continuous profiler captures thrown exceptions through ICorProfilerCallback::ExceptionThrown and reads each exception's \_message field by resolving System.Exception's ClassID from CoreLib metadata and computing the field offset with GetClassLayout. Lock contention durations come from CLR ContentionStart/ContentionStop events, delivered synchronously via EventPipeEventDelivered since .NET 5, with lock owner info added in .NET 8. Both event streams are subsampled with a PID-controller-based adaptive sampler: always at least one exception per type, and contentions bucketed by duration. Sampled values are then upscaled, using a count ratio for exceptions and a duration ratio for locks, since total wait time is what drives latency.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [.NET](https://swe.quest/tags/dotnet.md) (Languages)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Observability](https://swe.quest/tags/observability.md) (Reliability & operations)
- [Concurrency](https://swe.quest/tags/concurrency.md) (Compilers & runtimes)
- [Runtimes & VMs](https://swe.quest/tags/runtimes.md) (Compilers & runtimes)

## Prerequisites

- .NET CLR basics ([easy posts on .NET](https://swe.quest/tags/dotnet.md?difficulty=easy))
- CLR profiling API ([easy posts on Performance engineering](https://swe.quest/tags/performance.md?difficulty=easy))
- sampling profilers

## Keywords

Datadog Continuous Profiler, ICorProfilerCallback, EventPipe, CLR, PID controller, pprof

## Related posts

- [.NET Continuous Profiler: Under the hood](https://swe.quest/articles/net-continuous-profiler-under-the-hood-46b61cbffdef64a4.md): Datadog Engineering · 2024-01-09 · Hard · 9 min read · .NET, Performance engineering, Observability, Runtimes & VMs
  Datadog's .NET continuous profiler runs in-process as native C++ and Rust code loaded through the CLR's ICorProfilerCallback interface.
- [.NET Continuous Profiler: CPU and wall time profiling](https://swe.quest/articles/net-continuous-profiler-cpu-and-wall-time-profiling-5dcdacf43942cb04.md): Datadog Engineering · 2024-02-13 · Hard · 10 min read · .NET, Performance engineering, Runtimes & VMs, Observability, Linux
  Datadog's .NET continuous profiler samples managed threads every 10 ms instead of relying on ETW, perf or enter/leave hooks, which cost too much for 24/7 production use.
- [.NET Continuous Profiler: Memory usage](https://swe.quest/articles/net-continuous-profiler-memory-usage-51d84deba820efcb.md): Datadog Engineering · 2024-05-20 · Hard · 9 min read · .NET, Memory management, Observability, Performance engineering, Runtimes & VMs
  Datadog's .NET continuous profiler measures memory in three ways.
- [How we wrote a Python profiler](https://swe.quest/articles/how-we-wrote-a-python-profiler-397d52e3f1db71ec.md): Datadog Engineering · 2020-10-07 · Medium · 7 min read · Python, Performance engineering, Observability
  Datadog built an always-on statistical profiler for Python because deterministic profilers like cProfile can add 2-3x overhead, too much for production.
- [Strobelight: A profiling service built on open source technology](https://swe.quest/articles/strobelight-a-profiling-service-built-on-open-source-technology-6a094fead0e4b62a.md): Meta Engineering · 2025-01-21 · Medium · 12 min read · Performance engineering, Observability, Linux, Cost optimization, Compilers
  Strobelight is Meta's fleet-wide profiling orchestrator, running 42 profilers (mostly eBPF-based, plus ad-hoc bpftrace scripts) on every production host with safeguards against PMU counter conflicts, concurrency limits and queuing.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
