-
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
Is there a way to get rid of Object Reference exception for ${aspnet-sessionid} in internal logs for .net core 2. In case logger is being called in layer without HttpContext in it #435
Comments
Can you show the full stacktrace of the exception? |
Here is how it looks. |
I guess we could swallow this exception:
But I have a hard time reproducing this issue:
The later issue is the most interesting. Since there should never be a NullReferenceException. |
Also little confusing that you are running "NetCore2" but at the same time having NLog-config in the web.config. And have misspelled <configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections> |
Would it be possible for you to modify any of the two example-projects, so they generate the NullReferenceException? |
Sure. Will do it over weekend. I think those 2 exceptions are actually correlated. As first the HttpContext fails with the exception then Nlog logs invalid operation cos sessions is was not configured. But that's an educated guess :) I guess I copy pasted it from the old project and didn't remove it. |
Also what I have tried to reproduce, but without any luck. It is possible to configure NLog using appsettings.json: https://github.com/NLog/NLog.Extensions.Logging/wiki/Json-NLog-Config |
Thanks! Does it support like separate file configuration? like old scool nlog.config something like nlog.{env}.json? |
You can load from whatever json-file you want. Doesn't have to be appsettings.json. |
Created #436 to reduce the loglevel to Debug for |
I've checked update: also checked v4.6.3 tag |
@Defee Btw. if you want to optimize your NLog-config. Then you should replace the obsolete |
Found something I think, AsyncTaskTarget: protected override void FlushAsync(AsyncContinuation asyncContinuation)
{
if (_previousTask?.IsCompleted == false || !_requestQueue.IsEmpty)
{
InternalLogger.Debug("{0} Flushing {1}", Name, _requestQueue.IsEmpty ? "empty queue" : "pending queue items");
_requestQueue.Enqueue(new AsyncLogEventInfo(null, asyncContinuation));
_lazyWriterTimer.Change(0, Timeout.Infinite);
}
else
{
InternalLogger.Debug("{0} Flushing Nothing", Name);
asyncContinuation(null);
}
} sents |
None of the targets in question uses |
Doesn't the following |
I've created the solution but wasn't able to repro Null Reference case. Will try to do some real example with db tomorrow which is more related to what I have at work. Interesting fact: If I set throwExceptions to true it hangs completely. It seems it somehow can be related with the EF Core requests to db logging. So I will dive there deeper tomorrow. |
Curious what output you get in the NLog internal logger, when application hangs completely with |
It seems it hangs right before NullReference, it gives me 2 InvalidOperation exceptions (I think because 2 targets are configured) and hangs. In simple app log it freezes right after sql command logging |
Still no luck to repro NullReference. Tried with Not exactly the same config. Used Postress. (I use MSSQL at work ). @304NotModified , @snakefoot @304NotModified where/when exactly FlushAsync is being called? |
AsyncTaskTarget is similar to TargetWithLayout or TargetWithContext. It is base-class to inherit from. |
Anyway, my point was that logevent is assumed nonNull, while there are cases where it passes as null. As there is one case, there could be multiple as the assumption seems to be non valid. I will try to add some resharper annotations to the relevant code and then we could rule out this assumption. :) |
I have to check that (limited internet/PC now, holiday :)) , but at least: manual, end of appdomain event and maybe async batch |
Among others, calls to FlushAsync (could be indirect via Flush)
|
#436 has been merged so I think this should be closed |
Hi All!
Issue Template Info:
Type (choose one):
- Question
My question is there a way to write when clause or other clause in layout to exclude the exception throwing from internal-logs in case HttpContext is not determined in the layer?
It extremely hardens troubleshooting from internal nlog logs.
NLog version: Resolved as 4.6.3
NLog.Web/NLog.Web.AspNetCore version:
<PackageReference Include="NLog.Web.AspNetCore" Version="4.8.2" />
NLog.Extensions.Logging version:
Resolved as 1.5.0 from NLog.Web.AspNetCore.
Platform: .NET Core 2
Current NLog config (xml or C#, if relevant)
Question:
Please check the section after the questions for more information.
In internal logs i get the exception Object Reference not set for an instance of an object. The transaction to db however comes just with empty session. (which is expected result)
Some kind of workaround which will check
Yes.
Message: Object Reference not set for an instance of an object.
Details: Is failing inside of the internal log file when trying to call with above layout in the layer where HttpContext is not available. Specifically it fails on ${aspnet-sessionid} retrieving.
It doesn't log the
Didn't check.
I might help.
Additional Information
I use the wrapper class to log the data in the database, obviously for some of the layers aspnet-sessionid will not be available, is there a workaround to check context in layout if it is null to display empty value/or friendly message for logs, else display session Id.
It happens on the Db target I use.
I really appreciate help as want o get rid of the Object reference is not set as an instance of a object exception in internal nlog logs.
The common wrapper class will throw the exception when you have a Data Access Layer and you call logger in it as it doesn't have a clue about HttpContext.
Possibly related question #153
The text was updated successfully, but these errors were encountered: