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.

RequestLoggingMiddleware does not swallow exception

See original GitHub issue

I am using AspNetCore with ApplicationInsights and Serilog. In development environment exceptions are logged twice , because RequestLoggingMiddleware is not swallowing exceptions and DeveloperExceptionPageMiddleware is handling the exception and logging it.

Shouldn’t LogCompletion return true to swallow the exception?

private bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector, int statusCode, double elapsedMs, Exception ex) { var level = statusCode > 499 ? LogEventLevel.Error : LogEventLevel.Information; if (!Log.IsEnabled(level)) return false; if (!collector.TryComplete(out var properties)) properties = NoProperties; var logEventProperties = properties.Concat(new[] { new LogEventProperty("RequestMethod", new ScalarValue( httpContext.Request.Method)), new LogEventProperty("RequestPath", new ScalarValue(GetPath(httpContext))), new LogEventProperty("StatusCode", new ScalarValue(statusCode)), new LogEventProperty("Elapsed", new ScalarValue(elapsedMs)) }); Log.Write(new LogEvent(DateTimeOffset.UtcNow, level, ex, _messageTemplate, logEventProperties)); return false; // true?! }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
daugercommented, Oct 31, 2019

The above explanation makes sense to me, but it might be worth noting in the documentation that exceptions will be logged twice when the RequestLoggingMiddleware is enabled. I spent a few hours tracking this down and I am guessing others will too. It might help folks save time if they happen to notice it.

1reaction
nblumhardtcommented, Sep 6, 2019

Howdy! There are a couple of things going on 👍

LogCompletion() returns bool false so that it can be used directly in the when clause of the exception handler (the expression there needs to evaluate to a Boolean, and this is the cleanest way to do that).

The exception filter is used so that enriched properties are captured at the site where the exception is thrown, rather than where it’s caught - this is one of the “hidden” features of when clauses - they are executed at a rather surprising time, which just happens to be convenient for capturing logs.

Overall, swallowing the exception isn’t an option in the middleware because we’d prevent custom error handling pipelines like the developer exception page, etc., from being invoked - I did give it a try at one point, but it was unworkable.

HTH!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it OK to swallow all exceptions except the critical ones in ...
The reason it is generally advised to not swallow exceptions is that it can hide bugs. For example, you are doing something other...
Read more >
Do Not Swallow The Exceptions | Justin James
Throwing away exceptions in your code is just a bad practice and makes it harder to support your application. It may make it...
Read more >
Error hiding
In computer programming, error hiding (or error swallowing) is the practice of catching an error or exception, and then continuing without logging, ...
Read more >
Catch and swallow exception
Swallowing exceptions, without re-throwing or logging them, is a bad practice. The stack trace, and other useful information for debugging, is lost.
Read more >
Would you PLEASE stop swallowing exceptions on event ...
Hello, I have some logic that executes within a RowUpdated event. It worked just fine, until recently. Users started reporting incorrect ...
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