Shetty
Shetty

Reputation: 1892

Contextual Logging within LibLog

In Serilog I can push multiple properties to log with following code. I am doing this in a middleware to push properties from context. How can I do this using LibLog?

using (LogContext.PushProperties(
                        new PropertyEnricher(Constants.CorelationId, id),
                        new PropertyEnricher(Constants.ClientId, context.GetClientId()),
                        new PropertyEnricher(IdentityServiceConstants.RemoteIpAddress, context.Request.RemoteIpAddress)))
                {
                    await next();
                }

Where can I find out more about OpenMappedContext and NestedContext?

Upvotes: 4

Views: 1082

Answers (1)

Rob Davis
Rob Davis

Reputation: 1319

The following seems to work:

var logger = LogProvider.For<SomeType>();

using (LogProvider.OpenMappedContext("Foo", "12"))
using (LogProvider.OpenMappedContext("Bar", "34"))
using (LogProvider.OpenMappedContext("Last", "56"))
{
    logger.InfoFormat("testing {somePlaceholder}", 78);
}

There doesn't seem to be much in the way of documentation regarding the use of OpenMappedContext and OpenNestedContext. However, the LibLog src code seems to just be sucking in the PushProperty method behind the scenes for use in OpenMappedContext.

Upvotes: 4

Related Questions