-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
throwExceptions="true" SqlException is not rethrown #277
Comments
You should receive the exception when throw exceptions are enabled. I'll |
Thank you |
Steps:
|
It turns out, that 'throwExceptions' sets the global property LogManager.ThrowExceptions. But this property is seldom used, as PetrMachek pointed out, most catch blocks rely solely on the MustBeRethrown() extension method. |
Good catch! Yes I remember seeing that before. I think the exception handling is boilerplate code en difficult to fix. IMO they should be replaced with something like:
What do you think? |
What is the second one for? What do you expect an exception handler to return? |
Its wrapping code with returns something. For example int test(n) {
return handleException (() => 2 / n);
} |
I think I see, but with this quite new way you would have to refactor a lot of code, don't you? And is it really a benefit over
I just ask since it seems like a heavy refactoring. |
You're right on that. So maybe there is something between editing MustBeRethrown and the handleException approach? |
I guess switching to Roslyn and c#6 is no option? Since it supports exception filters for quite this kind of scenario (not depending to specific CLR, I think)
or maybe even
So, if not... I counted option I - cleanup structure
option II - alter MustBeRethrown behavior
Shall we start a voting? 😄 EDIT: |
Option III - replace .MustBeRethrown with .HandleException:Replace try
{...}
catch(Exception ex)
{
// internallogger usage?
if (ex.MustBeRethrown())
{
throw;
}
// internallogger usage?
} With try
{...}
catch(Exception ex)
{
ex.HandleException();
} This extension will:
This is somewhat between option I and II. pros/cons
You're right about Roslyn, It wont work outside desktop .Net, yet. |
I dismissed the concept of an extension method for the exceptions, cause I found that rethrowing from elsewhere and putting different handling behaviours in anonymous methods a too heavy price initially. Obviously you're not as condemning as me about that.
I believe to remember, that wrap exception as inner exception into a new one is still better than |
I think you are talking about option 1? As those uses anonymous methods. Option 3 is just a compiler trick without any performance penalty.
I would be nice if we fix that :) Maybe everything should be a Error.
Agree,.
Or just public static void HandleException(this Exception ex, LogLevel = LogLevel.Error, string message = null, params object[] args = null) // with rethrow and standard logging But I'm a bit confused? Which option has your vote? |
PS: related #637 |
I think I actually was still collecting facts before coming to a concluding vote. Where would you place these extension methods? |
OK, didn't catch that.
I depends. With the following exceptions we like to stop immediately (so no logging and rethrow)
Maybe also All other we like to log before an exception.
https://github.com/NLog/NLog/blob/master/src/NLog/Internal/ExceptionHelper.cs |
These exceptions are the ones, that are listed in |
Well sometimes it seems it's a good idea to keep to code but also add a comment (that most of the time it will not be catched) |
We have NLog configured to write logs to database. When the SqlException is thrown, then it's not propagated to our code even if there is parameter throwExceptions in NLog.config.
Exception is thrown in DatabaseTarget.cs line 488
int result = command.ExecuteNonQuery();
Should it behave in this way?
Thank you :)
Petr
The text was updated successfully, but these errors were encountered: