-
Notifications
You must be signed in to change notification settings - Fork 34
Add exception to IDiagnosticContext #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Sorry about the long turnaround on this. Just to make sure I understand the scenario - could you alternatively place the |
@nblumhardt Yes, but then Serilog would not log the actual response, since ProblemDetails transforms exceptions to HTTP responses. |
Thanks; checked out the middleware, thought this over some more - I'm on board with it 👍 I'll need to go through the PR again and think some more about back/forwards compatibility, seems like it's on the right track. |
One thing you will miss out on via this method that I should mention: the Serilog request logging middleware uses an exception filter so that it can log exceptions with |
Aha, I see. Would it be possible to pass the same data via the |
It's possible to clone, preserve, and (temporarily) reinstate the ambient |
No rush, but if there is anything I can do to help move this forward, let me know. I'm happy to help. |
src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs
Outdated
Show resolved
Hide resolved
src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs
Show resolved
Hide resolved
I think the main lingering questions are:
We can discuss these in a bit more detail on the corresponding Serilog.AspNetCore PR; I think that apart from the minor doc comments, this one is ready to go 👍 |
@nblumhardt Thanks, I've updated the xmldoc and added an obsolete attribute to To address your questions:
|
Brilliant, thanks @angularsen 👍 |
Just realised that we should bump the package major version to reflect the breaking interface change; I'll do that directly now on |
@angularsen I also use Hellang.Middleware.ProblemDetails middleware. What should I do to integrate this PR's new functionality with my existing code? |
@lonix1 Good question, we use a global MVC exception filter to obtain the exception before it is passed back out the middleware chain. A few other ways to do it:
MVC exception filterTo access exceptions thrown by API/MVC actions and set in using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
namespace Foo;
public class SerilogExceptionEnricherMvcFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
// Enrich the request log event with exception info that is swallowed by Hellang.ProblemDetails middleware in order to produce
// a ProblemDetails HTTP response before Serilog's RequestLoggingMiddleware has the chance to observe it.
var dc = context.HttpContext.RequestServices.GetRequiredService<IDiagnosticContext>();
dc.SetException(context.Exception);
}
} Add as global MVC filter in Startup.csWherever you configure MVC in your public sealed class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc(opt =>
{
// Global MVC filters.
opt.Filters.Add<SerilogEnricherMvcFilter>();
});
}
} |
Fixes: serilog/serilog-aspnetcore#270
Related PR: serilog/serilog-aspnetcore#271
For applications using middleware like Hellang.Middleware.ProblemDetails, where unhandled exceptions are swallowed, the application can pass on the exception to Serilog.AspNetCore's
RequestLoggingMiddleware
viaIDiagnosticContext
.Changes
IDiagnosticContext.SetException(Exception e)
DiagnosticContextCollector.TryComplete(out IEnumerable<LogEventProperty>, out Exception)
to not make a breaking change.