Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Logging shouldn't throw after dispose #578

Merged
merged 2 commits into from
Apr 4, 2017

Conversation

davidfowl
Copy link
Member

  • Check complete adding before adding new messages
    to the list.
  • Added a test

#563

PS: I didn't measure the performance of checking that bool on every write.

_messageQueue.Add(message);
if (!_messageQueue.IsAddingCompleted)
{
_messageQueue.Add(message);
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure how often this happens in production or if it ever does, but Add blocks if the queue is full, so this could throw still.

@@ -27,7 +27,10 @@ public ConsoleLoggerProcessor()

public virtual void EnqueueMessage(LogMessageEntry message)
{
_messageQueue.Add(message);
if (!_messageQueue.IsAddingCompleted)
Copy link
Member

Choose a reason for hiding this comment

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

Other threads can call EnqueueMessage while Dispose is being called, so this doesn't get rid of the issue, just decreases how often it happens.

@pakrym
Copy link
Contributor

pakrym commented Mar 24, 2017

We need to fix hosting issue first, maybe this exception is not that bad too, it prevents loosing log messages.

@davidfowl
Copy link
Member Author

@pakrym no we don't. We need to fix both bugs. Logging is one of those things that really should never throw. People don't put try catch statements around their logs.

@davidfowl
Copy link
Member Author

This is it still racey as @BrennanConroy points out. I thought I read that CompleteAdding waits on any outstanding adds to finishing before switching the state but it just waits on other calls to CompleteAdding to complete. Add does wait on CompleteAdding, but then it throws.

I still think this should never throw after add and this lessens the chances of that (plus this never happened before since we wrote directly to the console). I'll see if there's any other collection we could use.

@davidfowl
Copy link
Member Author

Will revisit

@pakrym
Copy link
Contributor

pakrym commented Apr 3, 2017

I changed this PR to catch InvalidOperationException thrown by .Add and log messages even if processor is disposed.

var logger = new ConsoleLogger(_loggerName, filter: null, includeScopes: false, loggerProcessor: processor);
logger.Console = console;
processor.Dispose();
logger.LogInformation("Logging after dispose");
Copy link
Member

Choose a reason for hiding this comment

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

Could we change this test to check that "Logging after dispose" gets logged

davidfowl and others added 2 commits April 4, 2017 09:03
- Check complete adding before adding new messages
to the list.
- Added a test
@BrennanConroy BrennanConroy force-pushed the davidfowl/throw-after-dispose-noop branch from b1ac083 to 81424d2 Compare April 4, 2017 16:09
@BrennanConroy BrennanConroy merged commit 81424d2 into dev Apr 4, 2017
@davidfowl
Copy link
Member Author

👏

@BrennanConroy BrennanConroy deleted the davidfowl/throw-after-dispose-noop branch April 4, 2017 16:19
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants