andreycha opened a new pull request, #101:
URL: https://github.com/apache/logging-log4net/pull/101

   **Change**
   Add `Peek()` method to `ThreadContextStack` and `LogicalThreadContextStack` 
to be able to get the most recent value from the context without removing it.
   
   **Motivation**
   In the scope of [ElasticCommonSchema .NET integration for 
log4net](https://github.com/elastic/ecs-dotnet/tree/main/src/Elastic.CommonSchema.Log4net)
 log entry properties are being read and written to ECS format. In case of 
context stacks [current 
implementation](https://github.com/elastic/ecs-dotnet/blob/main/src/Elastic.CommonSchema.Log4net/LoggingEventConverter.cs#L85)
 takes string representation of the stack. It works fine when stack (i.e., 
property) has a single value, but there are also cases when stack contains 
multiple values (same or different ones). With the proposed changes it will be 
able to take the most recent value, in the context of which the current log 
entry is being written.
   
   Consider following scenario:
   
   ```cs
   using (var _ = LogicalThreadContext.Stacks["Id"].Push("123"))
   {
       log.Info("Log 1"); // currently has "123" in the log metadata
   
       using (var __ = LogicalThreadContext.Stacks["Id"].Push("456"))
       {
           log.Info("Log 2"); // currently has "123 456" in the log metadata; 
should have "456" instead
   
           using (var ___ = LogicalThreadContext.Stacks["Id"].Push("789"))
           {
               log.Info("Log 3"); // currently has "123 456 789" in the log 
metadata; should have "789" instead
           }
       }
   }
   ```
   
   **Alternative solutions considered**
   An alternative approach might be to just pop the most recent value and then 
push it again onto the stack. Problem here is that it creates a new scope which 
will not be closed as no one will call `Dispose()` on the returned result from 
`Push()`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to