question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

How to use Serilog + Sentry + AspNetCore ?

See original GitHub issue

Hi šŸ‘‹ Iā€™m trying to understand the steps involved to leverging Sentry.IO with an ASP.NET Core 2.2 website BUT using Serilog as the logging mechanism.

Currently, weā€™re using Serilog to log Info+ messages to <some place>: Console or <some website>. Works great.

But weā€™d also like to use Sentry.io as a sink for our manual errors or (unexpected) exceptions that could get thrown.

What is the trick to setting this up please?

(for bonus points, we define our serilog settings in our appsettings.xxxx.config file(s) so we donā€™t hardcode anything in the code but use the specific environment files to handle specific sinks and settings -per environment-).

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
bruno-garciacommented, Aug 9, 2019

The issue here is that weā€™re using 2 separate integrations. Both can actually initialize the SDK but you only want (need) the SDK initialized once.

If you were using only Serilog, outside an ASP.NET Core app, you could define everything in the Serilog config. But if you want to capture request data, capture exceptions in the middleware pipeline, capture errors handled by the error page, and all the other features. then you need Sentry.AspNetCore.

The config blocks Sentry and Serilog are separate because they rely on the ASP.NET Core configuration system. and the packages donā€™t depend on each other. The way they can ā€˜share stateā€™ is because both of them depend on Sentry package which has AsyncLocal and the Sentry.AspNetCore pushes a scope for each web request. So now your _logger.LogX entries can be kept per-request and sent as breadcrumbs for example, or include request data.

So itā€™s not like you are configuring the Sentry Serilog Sink outside the Serilog config. You are configuring the Sentry Serilog sink just right. All itā€™s specific to it are those two logging levels. You are configuring ā€œSentryā€ via the root Sentry node in the configuration file which can live without Serilog. Still captures unhandled exceptions, including request data. In fact, if you didnā€™t have Serilog, it would capture automatically the log messages as breadcrumbs and/or errors because it integrates with Microsoft.Extensions.Logging out of the box.

Wrt configuring via Serilog: SentrySerilogOptions also derives from SentryOptions so it does have everything to init Sentry:

https://github.com/getsentry/sentry-dotnet/blob/46bf5548d347ad1e206aefa3ea91cc46ab02004f/src/Sentry.Serilog/SentrySerilogOptions.cs#L11

In this example we have only Serilog, without any other integration. Here we initialize it all via the Sink:

https://github.com/getsentry/sentry-dotnet/blob/46bf5548d347ad1e206aefa3ea91cc46ab02004f/samples/Sentry.Samples.Serilog/Program.cs#L16-L25

I understand itā€™s not ideal that the ASP.NET Core and Serilog configuration for Sentry are in different parts of the configuration system but I hope itā€™s at least now clear why it is this way.

1reaction
PureKromecommented, Aug 8, 2019

Okay. wow. What an awesome and helpful reply @bruno-garcia !

I had to read it a few times, double check the links to grok things ā€¦ but yep. Totally get it.

Iā€™ve also got a test webapp (file -> new -> aspnet core 2.2 api app) to test this out and itā€™s more or less working. Well, I can get it working 100% following what youā€™ve done, but I was hoping for something slightly different. As such, iā€™m not sure if this should be a different issue or not.

In your example, youā€™re configuring the Sentry sink via configuration. This is awesome and something that I always do because it means I can then define any sink configuration via environmental variables if required and it can easily work.

When Iā€™ve looked at your configuration, itā€™s the following:

      {
        "Name": "Sentry",
        "Args": {
          "MinimumBreadcrumbLevel": "Debug",
          "MinimumEventLevel": "Warning"
        }
      }

This makes sense ā€¦ except ā€¦ Iā€™ve always thought that a sink configuration should include all the options possible to setup/initialize the sink. As such, this has no argument for the Dsn ? I would have thought that I could define most of the common setup properties here, like MaxRequestBodySize or AttachStackTrace etc. I can see that thereā€™s already 2x properties defined: MinBreadcrumbs and MinEventLvl.

If I try the following, nothing is logged sentry:

           {
                "Name": "Sentry",
                "Args": {
                    "Dsn": "https://<snipped>"
                }
            }

but ā€¦ if i add the following in, it works now:

    "Sentry": {
        "Dsn": "https://<snipped>"
    },

It feels disjointed having to split these two out. I feel like ā€¦ if iā€™m creating the sink via WriteTo (which is what that is there for) I should be able to define most initialization properties there.

So ā€¦ a summary based on your awesome post:

  • āœ… I can log errors to Sentry following your steps!
  • āŒ I feel like the configuration of Sentry-via-Serilog should be handled via the WriteTo.

I hope this make sense and interest you ā€¦

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serilog | Sentry Documentation
Sentry provides an integration with Serilog through the Sentry.Serilog NuGet package . Installation. Add the Sentry dependency: Package Manager
Read more >
Sentry with Serilog in NET Core 3.1 worker
I have a NET Core 3.1 worker configured like this: public static IHostBuilder CreateHostBuilder(string[] args) => Host.
Read more >
Sentry.Serilog 3.35.1
Official Serilog integration for Sentry - Open-source error tracking that helps developers monitor and fix crashes in real time.
Read more >
Integrating Sentry with ASP.NET Core - YouTube
Quick demo on how to integrate the new (preview) SDK of Sentry. ... Logging into Elasticsearch using Serilog and viewing logs in Kibana...
Read more >
Structured Logging using Serilog in ASP.NET Core 7.0
OpenTelemetry is one of the sinks you can log to using Serilog. Serilog is what I already use at work to log to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found