.NET Error Tracking installation
- 1
Install the .NET SDK
RequiredFor ASP.NET Core projects, install the PostHog.AspNetCore package:
TerminalFor other .NET projects, such as console applications, MAUI, or Blazor, install the core
PostHogpackage:Terminal - 2
Configure PostHog
RequiredFor ASP.NET Core, add PostHog to your
Program.csfile:C#Configure your project token and host in
appsettings.json:JSONFor non-ASP.NET Core projects, initialize the core client as a singleton:
C#You can find your project token and instance address in the project settings page in PostHog.
- 3
Capture exceptions
RequiredThe .NET SDK supports manual exception capture with
CaptureException. It builds the$exceptionevent for you, including exception type, message, stack frames, inner exceptions, aggregate exceptions, source context when available, and .NET runtime metadata.PDB uploads are not yet supportedFile names, line numbers, and source context depend on debug information already available from the captured .NET stack trace. PostHog doesn't support uploading .NET PDB files yet, so production builds without runtime-accessible debug information may show less detailed stack frames (issue).
The examples below assume
posthogis anIPostHogClientfrom dependency injection, or your singletonPostHogClient.C#Pass additional properties to add request, tenant, or domain context to the exception:
C#You can also attach feature flag values to the exception event with a feature flag snapshot:
C#The next steps cover automatic capture. ASP.NET Core apps can capture request exceptions, and any .NET app can capture unhandled runtime exceptions.
- 4
Capture ASP.NET Core request exceptions
RecommendedASP.NET Core automatic capture is available in
PostHog.AspNetCoreversion 2.7.0 and later. Add the PostHog request context middleware after building the app, and enable exception capture:C#Place it before the request handlers whose exceptions you want to capture. The PostHog middleware captures the exception with the active request context and adds the HTTP response status.
How the exception appears in PostHog depends on where it stops:
- The exception escapes the PostHog middleware. PostHog captures it as unhandled, then throws it again. Exception-handling middleware that you register before the PostHog middleware can still handle it.
- Exception-handling middleware that runs after the PostHog middleware handles the exception. In
PostHog.AspNetCoreversion 2.10.0 and later, PostHog still captures it, and marks it as handled. For example, this applies toUseExceptionHandler.
C# - 5
Capture unhandled runtime exceptions
OptionalSome exceptions never reach a request. A background thread can crash the process, or a fire-and-forget task can fail with nobody observing it. In
PostHogversion 2.23.0 and later, enableCaptureUnhandledExceptionsto capture these:C#For ASP.NET Core, set it in
appsettings.json:JSONThe option is off by default. When you enable it, the client listens to two .NET runtime events:
Runtime event What PostHog captures AppDomain.UnhandledExceptionAn exception that no code caught. If it terminates the process, PostHog marks it as fataland waits up to two seconds to send queued events before the process exits.TaskScheduler.UnobservedTaskExceptionAn exception from a task that no code awaited or observed. PostHog reports the exception that the task threw, not the AggregateExceptionthat the runtime wraps it in.PostHog marks both as unhandled. If the ASP.NET Core middleware already captured an exception, the runtime events don't capture it again.
Dispose the client to stop listening to the runtime events.
Logged exceptions are not capturedAn exception that your code or a framework catches and logs doesn't reach either runtime event. For example, the .NET host logs a failed
BackgroundServiceinstead of leaving it unhandled. Capture these withCaptureException(issue). Verify error tracking
RecommendedTrigger and capture a test exception to confirm events are being sent to PostHog. You should see it appear in the Error Tracking tab.
C#For serverless environments, call
FlushAsync()before the process exits so queued exception events are sent.





