-
Notifications
You must be signed in to change notification settings - Fork 165
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
${aspnet-request}: could write some warnings when request isn't available #31
Comments
Hey, can you give some more info? You get an exception? Can you post relevant config and c# code? I don't understand this issue as AspNetRequestValueLayoutRenderer isn't using |
@komlachsv can you give us some more info? it seems that checking the property |
We can't prevent this. If |
What other implications will setting Would it be an option to wrap the fetching of Request in a utility method, trapping any HttpException thrown? |
I hide all errors, so logging could fail without knowing it. But that's a good case? I don't like crashing applications because logging failed. Also you can check the internalLogger. |
How about just catching the System.Web.HttpException from the property access? I suppose one would have to give up type inferrence and declare |
I also feel that to handle this very specific and well defined issue with a try/catch would be the best. Wrapped up in something so the issue becomes hidden in the layout code. |
That is indeed possible and not difficult to implement. But what should be do at the And then we have the same code which is already in |
@304NotModified - In this case, we are talking about catching one particular exception in a well-defined case (as @vegar said), not about handling exceptions in general. Looking at the null check it seems painfully obvious that Again, we are talking about catching the exception only when accessing the property, not silencing If you think of accessing |
There are more reasons why we can have an exception in this case, e.g. in .NET CORE when the session isn't setup. So |
@304NotModified - But the context is just expanding a layout renderer. Will anything else be affected by silencing this exception on property access? Or do you have a particular case in mind, when accessing the property from inside a logger would cause an exception relevant to the other parts of application be thrown? |
I also prefer pragmatism but I don't see the benefit of another catch - there's already a catch and you can fully control the behavior. With a plain return in the catch, |
I think there is a subtle difference between catching this particular exception and exceptions from logger in general. It's that while enabling exceptions from logger in general can be useful to debug some other logger issues (i.e. faults inside other layout renderers), letting this particular exception pass is useless, because it's cause is a well-known result broken design, not some unknown error worth investigating. While other exceptions or exceptions in other context can mean different things, it seems to me that this exception in this context can only really mean that the request is not available in current execution context (please point me out if I'm wrong with any of these assumptions). If so, why not just think of it as an equivalent of a if-not-null check? |
Let's look at this from a different angle: What would you do if the request was not available in the current context, but the property accessor did not throw an exception - it returned null instead. Would you consider it an error state? Would you log to the internal logger that 'something might be wrong here, cause I wanted to log the current request url, but there are no request available' ? In my code, I like to differentiate between exceptions that I expect and know how to handle and exceptions that I did not expect. If another place (a different layer in my code or others code) throws exception, and I know why, and I know how to recover, I'll not see it as an exception. I'll see it more as 'control flow' than 'exception'. So if we look away from .net core, which I have no knowledge of, I see no reason to treat the exception thrown inside of the property getter as an exception. It should have returned null, but it doesn't, but we know that an (http)exception thrown in this getter means only one thing: The request does not exist. Yes, we can just suppress all exceptions thrown in the logging layer - which as you say might be a good idea in production anyway. Maybe. But to me, that sounds like taking down the security net, just because once in a while some things are gonna fall down which we do not wanna catch in that net. And when it comes to the internal logger - I want to be sure that errors popping up in both internal and external logs are error that actually need attention. As soon as we start logging perfectly valid situations as errors, we'll start ignoring the bells and whistles; The alarm goes off - but who cares? I bet it's just the request object not available again... NLog is a great framework. I really appreciate the work you all have done. This is a part that did not work well for us, though, and we have chosen to reimplemented the layout renderers that we need - with try/catchs - and moved on. |
@vegar - I agree with you, and I also decided to silence this exception on my own - but insteadof re-implementing, I've chosen to extend But I'd like to see this handled in the library itself rather than by placing a workaround code in the application. |
I guess it's two against one ;) OK, I will fix this or sending a PR would also be nice. I don't like empty catches, so writing to the internal log level on debug looks to me the best. Please be aware that I didn't ignored this request because of purism. It's difficult to implement exception handling in special cases and still have consistent behaviour. For example, the database target was also catching exception differently in the past, with probably a good reason, and that was an issue which multiple users reported over the years. |
I fully agree: It's a difficult thing to get right. If you can wait a couple of days, I'll see if I can push a PR. When it comes to .net core - is the source for the Request property getter different there? The code already contains different code paths for .net core and '.net classic'. Should the behavior be kept as today for .net core? |
With .NET CORE almost everything is different, especially with ASP.NET. New types, interfaces, properties, missing features and Dependency injection. And there are a lot of breaking changes between rc1 and rc2 (latter not released, unclear when) |
It turns out it's very difficult to write a test that expose the error. Do you have some tips? I've tried two strategies:
Both 'fails' by not rethrowing the exception. The internal How can I recreate the scenario we see in our production applications, where the exception is thrown and halting the application? A third option would be to inspect the |
You can set |
Fixed and released in 4.2.1 :) |
If don't check this:
in method
AspNetRequestValueLayoutRenderer::Append
if try write to log from Global.asax.cs on start application, we have warning:
The text was updated successfully, but these errors were encountered: