Skip to content
← All projects
Project

Serilog.Sinks.SentrySDK

Listen to the audio story

Sending Serilog events to Sentry · 1:23 · AI-written script, synthetic voices

Transcript

Ava: When an app breaks in production, you want the error to land somewhere you will actually see it. That's the job of Serilog dot Sinks dot SentrySDK.

Andrew: Okay, two names to unpack. What's Serilog?

Ava: A popular logging library for dot NET. A sink is where its log events end up. This sink, built by Antoine, sends events to Sentry through the official Sentry SDK, so your normal log calls feed your error dashboard.

Andrew: So a plain log error call is enough?

Ava: Pretty much. You add the Sentry sink to your logger setup with your project address, called a DSN, and by default errors and higher go to Sentry. You can configure it in code or in a JSON settings file, including the environment, release, sampling and breadcrumbs.

Andrew: What if the logs contain private data?

Ava: There's data scrubbing for that. You provide your own scrubber, and it cleans events before they leave the app. There's also a second package for ASP dot NET Core that adds request details like user, headers and body to each event.

Andrew: Where does it come from?

Ava: It's based on the community serilog-sinks-sentry project, rebuilt around the official SDK. It's on NuGet in two packages, with automated tests, CI, CodeQL scanning and code coverage badges. The project page has the install commands and the README has the examples.

Table of contents

A Serilog sink that sends events to Sentry through the official Sentry SDK.

Based on serilog-contrib/serilog-sinks-sentry

Project Status

.NET Core Test .NET Core CI CodeQL codecov

Available Packages

PackageNuget
Serilog.Sinks.SentrySDKPackage LinkNuGet
Serilog.Sinks.SentrySDK.AspNetCorePackage LinkNuGet

Installation

The library is available as a Nuget package.

You can install it with the following command:

dotnet add package Serilog.Sinks.SentrySDK
Install-Package Serilog.Sinks.SentrySDK

Demos

Usage examples and demo-style snippets are maintained in the GitHub repository README.

Getting Started

Adding the Sentry Sink

Add the Sentry sink to your Serilog logger configuration, so that the logs will be sent to your Sentry instance. The Sentry DSN must be provided.

You can also configure Serilog using a JSON configuration. Here’s a sample:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "Serilog": {
    "Using": [
      "Serilog.Sinks.SentrySDK"
    ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "Sentry",
        "Args": {
          "dsn": "",
          "sendDefaultPii": true,
          "maxBreadcrumbs": 200,
          "maxQueueItems": 100,
          "debug": true,
          "diagnosticLevel": "Error",
          "environment": "Development",
          "operationName": "SentryConsole",
          "release": "1.0.5",
          "serverName": "SentryConsole",
          "dist": "SentryConsole",
          "tags": "SentryConsole=SentryConsole",
          "tracesSampleRate": 1.0,
          "tracesSampler": "AlwaysSample",
          "stackTraceMode": "Enhanced",
          "isGlobalModeEnabled": true,
          "sampleRate": 1.0,
          "attachStacktrace": true,
          "autoSessionTracking": true,
          "enableTracing": true
        }
      }
    ],
    "Enrich": ["FromLogContext", "WithMachineName", "WithThreadId"],
    "Properties": {
      "Application": "Sample"
    }
  }
}
var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json")
    .Build();

    var log = new LoggerConfiguration()
              .ReadFrom.Configuration(configuration)
              .Enrich.FromLogContext()
              .CreateLogger();

// By default, only messages with level errors and higher are captured
log.Error("This error goes to Sentry.");

Data Scrubbing

Data scrubbing allows you to sanitize your logs before they are sent to Sentry. This can be useful for removing sensitive information.

To use it, provide a custom IScrubber implementation when setting up the Sentry Sink:

var log = new LoggerConfiguration()
    .WriteTo.Sentry("Sentry DSN", dataScrubber: new MyDataScrubber())
    .Enrich.FromLogContext()
    .CreateLogger();

Capturing HttpContext (ASP.NET Core)

To include user, request body, and header information in the logs, some additional setup is required.

First, install the ASP.NET Core sink with the command:

dotnet add package Serilog.Sinks.SentrySDK.AspNetCore
Install-Package Serilog.Sinks.SentrySDK.AspNetCore

Then, update your logger configuration to include a custom HttpContextDestructingPolicy:

var log = new LoggerConfiguration()
    .WriteTo.Sentry("Sentry DSN")
    .Enrich.FromLogContext()

    // Add this two lines to the logger configuration
    .Destructure.With<HttpContextDestructingPolicy>()
    .Filter.ByExcluding(e => e.Exception?.CheckIfCaptured() == true)

    .CreateLogger();

Finally, add the Sentry context middleware to your Startup.cs:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    // Add this line
    app.AddSentryContext();

    // Other stuff
}

With these steps, your logs will include detailed information about the HTTP context of the requests.

Sentry SDK

Properties

  • BackgroundWorker: A property that gets or sets the worker used by the client to pass envelopes.
  • SentryScopeStateProcessor: A property to get or set the Scope state processor.
  • SendDefaultPii: A property to get or set whether to include default Personal Identifiable Information.
  • NetworkStatusListener: A property to get or set a mechanism to convey network status to the caching transport.
  • ServerName: A property to get or set the name of the server running the application.
  • AttachStacktrace: A property to get or set whether to send the stack trace of an event captured without an exception.
  • IsEnvironmentUser: A property to get or set whether to report the System.Environment.UserName as the User affected in the event.
  • SampleRate: A property to get or set the optional sample rate.
  • ShutdownTimeout: A property to get or set how long to wait for events to be sent before shutdown.
  • MaxBreadcrumbs: A property to get or set the maximum breadcrumbs.
  • MaxQueueItems: A property to get or set the maximum number of events to keep while the worker attempts to send them.
  • BeforeBreadcrumb: A property to get or set a callback function to be invoked when a breadcrumb is about to be stored.
  • BeforeSendTransaction: A property to get or set a callback to invoke before sending a transaction to Sentry.
  • MaxCacheItems: A property to get or set the maximum number of events to keep in cache.
  • Dsn: A property to get or set the Data Source Name of a given project in Sentry.
  • Environment: A property to get or set the environment the application is running.
  • Distribution: A property to get or set the distribution of the application, associated with the release set in SentryOptions.Release.
  • Release: A property to get or set the release information for the application.
  • BeforeSend: A property to get or set a callback to invoke before sending an event to Sentry.

Methods

  • AddJsonConverter(JsonConverter converter): A method to add a JsonConverter to be used when serializing or deserializing objects to JSON with the SDK.
  • SetBeforeBreadcrumb(Func<Breadcrumb, Breadcrumb?> beforeBreadcrumb): A method to set a callback function to be invoked when a breadcrumb is about to be stored.
  • SetBeforeBreadcrumb(Func<Breadcrumb, Hint, Breadcrumb?> beforeBreadcrumb): Another overload of SetBeforeBreadcrumb method that accepts a Hint.
  • SetBeforeSend(Func<SentryEvent, SentryEvent?> beforeSend): A method to configure a callback function to be invoked before sending an event to Sentry.
  • SetBeforeSend(Func<SentryEvent, Hint, SentryEvent?> beforeSend): Another overload of SetBeforeSend method that accepts a Hint.
  • SetBeforeSendTransaction(Func<Transaction, Transaction?> beforeSendTransaction): A method to configure a callback to invoke before sending a transaction to Sentry.
  • SetBeforeSendTransaction(Func<Transaction, Hint, Transaction?> beforeSendTransaction): Another overload of SetBeforeSendTransaction method that accepts a Hint.