Simple example project demonstrating Sentry integration for .NET APIs.
Sentry is an error tracking and performance monitoring tool that helps developers identify and fix issues in their applications. This example project showcases how to integrate Sentry into a .NET API application.
- .NET SDK installed (version 6.0 or later)
- An active Sentry account (you can sign up for free at sentry.io)
-
Install the Sentry .NET SDK
- Via .NET CLI:
dotnet add package Sentry.AspNetCore -v 6.0.0
- Via Package Manager:
dotnet add package Sentry.AspNetCore -v 6.0.0
- Via .NET CLI:
-
Configure Sentry in your application
-
In
Program.csadd the following configuration:builder.WebHost.UseSentry(options => { // A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable. // See https://docs.sentry.io/product/sentry-basics/dsn-explainer/ options.Dsn = builder.Configuration["SENTRY_DSN"]; // Enable Sentry performance monitoring options.TracesSampleRate = 1.0; #if DEBUG // Log debug information about the Sentry SDK options.Debug = true; #endif // Configure the minimum Log Level of Breadcrumbs and Events options.MinimumBreadcrumbLevel = LogLevel.Information; options.MinimumEventLevel = LogLevel.Error; // This option enables Logs sent to Sentry // Configure the minimum Log Level of Structured-Logs via e.g. "appsettings.json" and "appsettings.{HostEnvironment}.json" options.EnableLogs = true; });
-
-
Verify the integration by capturing a test error in your application.
SentrySdk.CaptureMessage("Sentry is successfully integrated!");
- In the root of the project (
Sentry), run the following commands to build the Docker image for the API and push it to Docker Hub:docker build --no-cache -f SentryApi/Dockerfile -t ${DOCKER_USERNAME}/sentry-api:latest . docker push ${DOCKER_USERNAME}/sentry-api:latest
