I have a custom logger, I would like to create an extension method called
SetContext(uniqueKey, object)
where I can set a property in my custom logger and log that context when logging a message.
So the code would look like this
_logger
.SetContext(User.Email(), User)
.LogError(1, new Exception("foo foo foo"), "bleep Bloop");
The problem I seem to be facing is that the ILogger, returned via DI in my controller, and in turn used by my extension method is not actually my custom ILogger implementation.
public static ILogger SetContext(this ILogger logger, string uniqueUserIdentifier, object context)
{
var redisPublisherLogger = logger as RedisPublisherLogger;
if (redisPublisherLogger == null)
{
return logger;
}
redisPublisherLogger.Context = new LoggingContext
{
ContextData = context,
UniqueUserIdentifier = uniqueUserIdentifier
};
return logger;
}
So is there anyway I can go about doing something like this, I'm trying to avoid overriding all the logging methods on ILogger.
Any help would be appreciated! Thanks
So BeginScope allows you to provide a context for the log... This link explains it quite nicely... https://nblumhardt.com/2016/11/ilogger-beginscope/
but an example that I did was:
public static ILogger SetContext(this ILogger logger, string uniqueUserIdentifier, object context)
{
var scope = logger.BeginScope(new LoggingContext
{
ContextData = context,
UniqueUserIdentifier = uniqueUserIdentifier
});
scope.Dispose();
return logger;
}
and then in my ILogger implementation I created:
private string _loggingContext;
and for the BeginScope implementation I did this:
public IDisposable BeginScope<TState>(TState state)
{
var s = state as IDisposable;
_loggingContext = JsonConvert.SerializeObject(s);
return s;
}
so now I had my specific context data available for use...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With