C
C#2y ago
Esa

How can I inject methodname into ILogger.Debug()-call without changing useage everywhere?

Hi, I have some property I wish to inject into my log messages (Serilog). I found a way to obtain it, but it requires a new method:
public static void Debug(this ILogger logger,
string logMessage,
[CallerMemberName] string memberName = "",
params object[] propertyValues)
{
using (LogContext.PushProperty("MethodName", memberName))
logger?.Debug(logMessage, propertyValues.ToArray());
}
public static void Debug(this ILogger logger,
string logMessage,
[CallerMemberName] string memberName = "",
params object[] propertyValues)
{
using (LogContext.PushProperty("MethodName", memberName))
logger?.Debug(logMessage, propertyValues.ToArray());
}
The idea here is that I can enrich the log template with the method name of the callsite. But this isn't the same Debug method found in the ILogger interface - so I'll have to use this method instead of that one. Is there a better way to achieve the same without using this method (since I'll have to add a using statement to use it)?
1 Reply
Esa
Esa2y ago
Anybody got any suggestions for this?