.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#For ASP.NET Core, you can opt into automatic capture for unhandled request exceptions in the next step. Other .NET apps continue to use
CaptureExceptionmanually. - 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 unhandled exceptions you want to capture. If you use exception-handling middleware, register it first so it can handle the exception after PostHog throws it again. The PostHog middleware captures the exception with the active request context and adds the HTTP response status.
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.