How to use Serilog + Sentry + AspNetCore ?
See original GitHub issueHi š 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:
- Created 4 years ago
- Comments:8 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
andSerilog
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 onSentry
package which hasAsyncLocal
and theSentry.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 withMicrosoft.Extensions.Logging
out of the box.Wrt configuring via Serilog:
SentrySerilogOptions
also derives fromSentryOptions
so it does have everything toinit
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.
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:
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, likeMaxRequestBodySize
orAttachStackTrace
etc. I can see that thereās already 2x properties defined: MinBreadcrumbs and MinEventLvl.If I try the following, nothing is logged sentry:
but ā¦ if i add the following in, it works now:
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:
WriteTo
.I hope this make sense and interest you ā¦