Unhandled exceptions in generic host don't have breadcrumbs
See original GitHub issuePackage
Sentry.Serilog
.NET Flavor
.NET
.NET Version
6.0.7
OS
Linux
SDK Version
3.20.0
Self-Hosted Sentry Version
No response
Steps to Reproduce
I’m integrating Sentry into a dotnet C# console app that uses Serilog and generic host.
To reproduce:
- See snippet below for a simple example app
- Set up your DSN in code
- Install dependencies, run it.
Here’s relevant code that shows how app is all hooked up:
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Sentry;
using Serilog;
using (var host = CreateHostBuilder().Build())
{
await host.StartAsync();
var lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
// Start Martini CLI
var martiniCLI = host.Services.GetRequiredService<MartiniCLI>();
await martiniCLI.RunAsync(lifetime.ApplicationStopping);
// Once completed, automatically stop the application
lifetime.StopApplication();
await host.WaitForShutdownAsync();
}
static IHostBuilder CreateHostBuilder() =>
new HostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseConsoleLifetime()
.ConfigureHostConfiguration(hostConfigBuilder => hostConfigBuilder.AddEnvironmentVariables(prefix: "DOTNET_"))
.ConfigureServices(services => services.AddTransient<MartiniCLI>())
.ConfigureLogging((_, logging) =>
{
logging.AddSimpleConsole().SetMinimumLevel(LogLevel.Debug);
// Configure sentry logger
var sentryLogger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Sentry(debug: true,
minimumBreadcrumbLevel: Serilog.Events.LogEventLevel.Debug,
environment: "Development",
dsn: "")
.CreateLogger();
logging.AddSerilog(sentryLogger);
});
class MartiniCLI
{
readonly ILogger<MartiniCLI> _logger;
public MartiniCLI(ILogger<MartiniCLI> logger)
{
_logger = logger;
}
public async Task RunAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Start");
_logger.LogDebug("Debug log");
SentrySdk.AddBreadcrumb("Manually added breadcrumb", "info", level: BreadcrumbLevel.Debug);
_logger.LogInformation("Delay for 5 seconds");
await Task.Delay(5000, cancellationToken);
_logger.LogInformation("Delay finished");
throw new Exception("Unhandled exception!");
// _logger.LogError("Fake error!");
}
}
Expected Result
I would expect errors (both unhandled exceptions and _logger.LogError
) to be logged in Sentry with breadcrumbs.
Actual Result
Errors logged manually using _logger.LogError
are logged in Sentry with breadcrumbs. Unhandled exceptions are logged in Sentry but without breadcrumbs.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
c# - .NET Generic Host : Prevent application crash on ...
Is there a solution to protect the Generic Host, as well as the IHostedService against unhandled exceptions ?
Read more >NET 6 breaking change: Exception handling in hosting - .NET
In previous versions, when a BackgroundService throws an unhandled exception, the exception is lost and the service appears unresponsive.
Read more >when other than exception, and raise application error
What I'm trying to say there is - if the error is raised, caught and handled (not re-raised to the client) by PLSQL...
Read more >Error: "Unhandled Exception has occurred in your application ...
In MYOB PDF Manager, you may receive the following error when saving a document to Document Manager from PDF Manager: "Unhandled Exception has...
Read more >FragmentBreadCrumbs | Android Developers
Helper class for showing "bread crumbs" representing the fragment stack in an activity. This is intended to be used with ActionBar.
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 FreeTop 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
Top GitHub Comments
Since there’s a viable workaround, I suggest using that for now until we can get a better fix in place.
Thanks.
Just to add a workaround, you can try/catch around the entire worker logic and manually capture the exception. If doing so, you’ll want to flag it as “unhandled” like this:
Or in the originally reported code without changing anything else: