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

Consistent Exception handling v3 #1143

Merged
merged 44 commits into from
Jan 27, 2016
Merged

Conversation

304NotModified
Copy link
Member

Summary

Preferred way to use:

  • exception is added to the message
  • the message should add some
  • choose InternalLogger.Warn or InternalLogger.Error`

eg. (most preferred)

InternalLogger.Error(exception, "Cannot stop file watching.");

if (exception.MustBeRethrown())
{
    throw;
}

Also OK, but no custom message in the logs:

if (exception.MustBeRethrown())
{
    throw;
}

Only in special cases! (legacy, very good reaons)

if (exception.MustBeRethrownImmediately())
{
    throw;
}

Never use throw ex

Original post

  • added overloads with exception parameter, analogues to the normal logger.
  • MustBeRethrown logs to InternalLogger if the exception is not logged before to the InternalLogger
  • Warn for NLog config errors, Exception otherwise.

Fixes that:

  • Exception are not thrown as but were expected
  • LogManager.ThrowExceptions was not always checked
  • Exceptions we not always logged to the internallogger.

Fixes #637. Fixes #277

supersedes #915 and #1117

todo

@codecov-io
Copy link

Current coverage is 73.94%

Merging #1143 into master will increase coverage by +0.08% as of 444ba50

@@            master   #1143   diff @@
======================================
  Files          265     265       
  Stmts        15230   15297    +67
  Branches      1672    1679     +7
  Methods          0       0       
======================================
+ Hit          11249   11311    +62
- Partial        415     420     +5
  Missed        3566    3566       

Review entire Coverage Diff as of 444ba50


Uncovered Suggestions

  1. +0.08% via ...argets/FileTarget.cs#1722...1734
  2. +0.08% via ...gingConfiguration.cs#282...293
  3. +0.08% via ...gingConfiguration.cs#244...255
  4. See 7 more...

Powered by Codecov. Updated on successful CI builds.

@304NotModified 304NotModified added bug Bug report / Bug fix enhancement Improvement on existing feature labels Jan 9, 2016
@304NotModified 304NotModified added this to the 4.3 milestone Jan 9, 2016
@304NotModified
Copy link
Member Author

@bhaeussermann Do like to check this again? I didn't changed any plans since our nice discussion :)

if (ex != null)
{
ex.MarkedAsLoggedToInternalLogger();
formattedMessage += " Exception: " + ex.ToString();
Copy link
Contributor

Choose a reason for hiding this comment

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

How about adding the exception string to the StringBuilder below instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, thanks!

but that's OK as this is new in 4.3 and the type is still the same.
@304NotModified
Copy link
Member Author

Also,
You asked me before is throwExceptions = true is only for debugging. I confirmed that, but I was wrong. For example the FallbackGroupTarget depends on it :(

I was wrong :) The AsyncContinuation is used for that, not a try-catch, And it seems that the exceptions are always written to AsyncContinuation

@304NotModified 304NotModified removed the breaking change Breaking API change (different to semantic change) label Jan 25, 2016
@304NotModified
Copy link
Member Author

@bhaeussermann

If you like to check this again, thanks! I rechecked every call to MustBeRethrown. I changed some inconsistent test cases as I see them as bugfixes.

@@ -72,12 +72,13 @@ public void ScanTypes(Type[] types, string prefix)
}
catch (Exception exception)
{
InternalLogger.Error(exception, "Failed to add type '{0}'.", t.FullName);

if (exception.MustBeRethrown())
{
throw;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Must this not change to MustBeRethrownImmediately?

Copy link
Member Author

Choose a reason for hiding this comment

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

Why do you think that?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the only reason would be backwardscomp. I think it's bug that the exception was not thrown when throwExceptions=true

@bhaeussermann
Copy link
Contributor

You asked me before is throwExceptions = true is only for debugging. I confirmed that, but I was wrong. For example the FallbackGroupTarget depends on it :(

I was wrong :) The AsyncContinuation is used for that, not a try-catch, And it seems that the exceptions are always written to AsyncContinuation

Ok, so I assume that it is then safe to throw an exception where we did not throw an exception before if ThrowExceptions is set to true. Is that correct?

}



[Fact]
public void InvalidLoggerConfiguration_ThrowsConfigurationException_IfThrowExceptionsFlagIsSet()
Copy link
Contributor

Choose a reason for hiding this comment

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

The name should say IsNotSet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug report / Bug fix discussion enhancement Improvement on existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants