Suppose we are writing the following structured log message
logger.Info("User's Password is {Password}", "1234567890");
I would like to mask a password property because it is a sensitive data. I found this issue but I think it is a very difficult way.
For example, I have found the extension that resolves a similar task for serilog. It is very simple to use. But I didn't find any useful information for Nlog.
How to achieve it with nlog library? I will appreciate any suggestions.
You could use RegisterObjectTransformation
, introduced in NLog 4.7.
For example:
LogManager.Setup().SetupSerialization(s =>
s.RegisterObjectTransformation<object>(o =>
{
var props = o.GetType().GetProperties();
var propsDict = props.ToDictionary(p => p.Name, p => p.GetValue(o));
propsDict.Remove("password");
return propsDict;
}));
Please note, in terms of performance you maybe need something like a reflection cache and smart optimizations.