Serilog.Sinks.SentrySDK 1.0.6

dotnet add package Serilog.Sinks.SentrySDK --version 1.0.6                
NuGet\Install-Package Serilog.Sinks.SentrySDK -Version 1.0.6                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Serilog.Sinks.SentrySDK" Version="1.0.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Serilog.Sinks.SentrySDK --version 1.0.6                
#r "nuget: Serilog.Sinks.SentrySDK, 1.0.6"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Serilog.Sinks.SentrySDK as a Cake Addin
#addin nuget:?package=Serilog.Sinks.SentrySDK&version=1.0.6

// Install Serilog.Sinks.SentrySDK as a Cake Tool
#tool nuget:?package=Serilog.Sinks.SentrySDK&version=1.0.6                

Serilog.Sinks.SentrySDK

A Serilog sink for Sentry that simplifies error and log management in your applications.

Based on serilog-contrib/serilog-sinks-sentry

Project Status

.NET Core Test .NET Core CI CodeQL codecov

Available Packages

Package Nuget
Serilog.Sinks.SentrySDK Package Link NuGet
Serilog.Sinks.SentrySDK.AspNetCore Package Link NuGet
Package Nuget
Serilog.Sinks.SentrySDK Package Link NuGet
Serilog.Sinks.SentrySDK.AspNetCore Package Link NuGet

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

Demos demonstrating how to use this library can be found here.

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.
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 3,898 3/11/2024
1.0.5 56,576 7/17/2023