Specify how complex types are logged to Serilog by excluding individual properties.
Install from NuGet:
Install-Package Destructurama.ByIgnoring
Mark properties to ignore on target types:
Log.Logger = new LoggerConfiguration()
.Destructure.ByIgnoringProperties<User>(u => u.Password)
// Other logger configuration
.CreateLogger()
Mark properties to ignore on assignable types:
Log.Logger = new LoggerConfiguration()
.Destructure.ByIgnoringPropertiesOfTypeAssignableTo<User>(u => u.Password)
// Other logger configuration
.CreateLogger()
When these types are destructured, all instance (that is not static) properties except the specified ones will be passed through:
Log.Information("Logged on {@User}", new User { Username = "nick", Password = "This is ignored" });
// Prints `Logged on User { Username: "nick" }`
The results are available here.