Open DmitryMak opened 4 years ago
Try using this line instead:
LogicalThreadContext.Properties["FirstName"] = "Alice";
We rely on IDisposable returned from Push. Could you help us understand how this will fix the ArgumentNullException?
Look at https://github.com/urielha/log4stash/blob/master/src/log4stash/LogEvent/BasicLoggingEventParser.cs line 111 method. It takes its values from the properties of the thread context and not the stack, these are different storages. Therefore, you need to use properties with the current code. Or, you can add a pull request with a way to sort it out.
Based on my experiments it is a bug ConvertToArrayFilter. When I turn it off by removing <ToArray>
in the config, the exception disappears. The underlying issue that we were having also goes away. The ArgumentNullException prevented further logging. Removing <ToArray>
filter fixed that.
So based on that, the issue is not related to Properties vs Stack. We've been using Stack for a long time and it always, and still, works.
You are right, it does work, my bad :) Anyways, it seems either somehow instead of "Alice" you send null or somehow the value is overwritten to null in log4stash
You are right, it does work, my bad :) Anyways, it seems either somehow instead of "Alice" you send null or somehow the value is overwritten to null in log4stash
We remove "Alice" by calling IDisposable.Dispose. I updated the bug description with a code sample.
From what I see, since you dispose "Alice" before logging without popping it from the stack it stays null and therefore causes the exception, I debugged log4stash with a sample code similar to yours and that's what I saw
My understanding is that explicit Pop call is not required. Based on log4net documentation for the Push, they seem to recommend this pattern:
using(log4net.LogicalThreadContext.Stacks["NDC"].Push("Stack_Message"))
{
log.Warn("This should have an ThreadContext Stack message");
}
It was my understanding too, but in fact that's what happens when debugging the code
I have do a bit more research on this, but IMHO ToArray filter needs to be fixed to behave like ToString filter. ToString does not crash in the same scenario.
We are getting this exception when trying to log with log4stash 3.0.0 and log4net 2.0.11.
Not sure if it is related but we use custom log4net code like LogicalThreadContext.Stacks["FirstName"].Push("Alice")
With log4stash config like
Edit: Code to reproduce the issue: