Given the following class, how do I suppress the Credentials property from being logged by Serilog?
public class SmtpConfigInfo
{
public string SmtpHostName { get; set; }
public int Port { get; set; }
[NotLogged]
public System.Net.ICredentialsByHost Credentials { get; set; }
}
Ideally, I would like to be able to suppress only the "Password" property of Credentials. If not, it's acceptable for me to see the whole Credentials being suppressed for now. However, when I log with Serilog as follows,
seriLogger.Error("{@ConfigInfo}", MySmtpConfigInfo)
the [NotLogged] attribute is ignored and I see all properties of Credentials including UserName and Password in the clear. I saw a documentation that seems to indicate that if my Credentials property were broken down to UserName and Password properties, then applying [NotLogged] to Password could work. Is that the only option I have using "Serilog.Extras.Attributed"? Thanks much!
To enable the NotLogged attribute you need to add .Destructure.UsingAttributes() - if this is missing the attribute will be ignored.
Edit: as @seangwright pointed out,
Destructure.UsingAttributesis available in theDestructurama.Attributednuget package, so make sure you add a reference to that library in your project before you continue.
Example:
var log = new LoggerConfiguration()
.Destructure.UsingAttributes()
.CreateLogger();
Alternatively, the long-form solution is something like:
var log = new LoggerConfiguration()
.Destructure.ByTransforming<SmtpConfigInfo>(info => new {
info.SmtpHostName,
info.SmtpPort,
Credentials = new { UserName = info.Credentials.UserName }
})
.CreateLogger();
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