Skip to content
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

Fix logging exceptions from background thread #6273

Merged
merged 1 commit into from
Aug 7, 2018

Conversation

zivkan
Copy link
Member

@zivkan zivkan commented Aug 7, 2018

QuietLog uses Elmah, which uses DI resolving at log time, rather than being contructed at HTTP request time. Autofac MVC's lifetime manager requires a HttpContext, which is null in a background thread, so there was no good/easy way to resolve the Elmah logger in a background thread. Logging only to app insights instead.

joelverhagen
joelverhagen previously approved these changes Aug 7, 2018
Copy link
Member

@joelverhagen joelverhagen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this looks fine. Could we track Elmah and AI diverging? There may already be an issue since Elmah sometimes has this AI doesn't have. Please get one more sign off.

@@ -34,7 +37,7 @@ protected override Task SendMessageAsync(MailMessage mailMessage)
catch (Exception ex)
{
// Log but swallow the exception.
QuietLog.LogHandledException(ex);
telemetryService.TrackException(ex, _ => { });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use the overload that doesn't require an Action?

@joelverhagen
Copy link
Member

We have a monitor on exception in Elmah which can trigger when exceptions go over a certain point. Since we know ErrorLog is a singleton and therefore can be safely passed into this background task, can we pass it to a QuietLog overload? Then we get both logging.

@joelverhagen joelverhagen dismissed their stale review August 7, 2018 18:49

Spoke offline about other options.

The previous code would try to resolve a service from DI in a background thread without a HttpContext, and Autofac would throw an exception. So, pass the required service to the background thread when it can be resolved.
@zivkan zivkan force-pushed the zivkan-background-mail-exceptions branch from 23e8c69 to e44825c Compare August 7, 2018 20:23
@zivkan zivkan changed the title Report background email exceptions in AppInsights only Fix logging exceptions from background thread Aug 7, 2018
}

private ErrorLog errorLog;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: readonly

@joelverhagen joelverhagen merged commit d1610bb into dev Aug 7, 2018
@joelverhagen joelverhagen deleted the zivkan-background-mail-exceptions branch August 7, 2018 20:40
zivkan added a commit that referenced this pull request Aug 9, 2018
zivkan added a commit that referenced this pull request Aug 9, 2018
* Revert "Don't throw when SmtpUri is not set (#6278)"

This reverts commit e60e75f.

* Revert "Fix logging exceptions from background thread (#6273)"

This reverts commit d1610bb.

* Revert "Improve email sending (#6154)"

This reverts commit 2616a99.
zivkan added a commit that referenced this pull request Aug 10, 2018
* Revert "Don't throw when SmtpUri is not set (#6278)"

This reverts commit e60e75f.

* Revert "Fix logging exceptions from background thread (#6273)"

This reverts commit d1610bb.

* Revert "Improve email sending (#6154)"

This reverts commit 2616a99.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants