Exclude Dto Properties #500
Replies: 2 comments 3 replies
-
Please provide more context. What are you auditing? Which extensions are you using? |
Beta Was this translation helpful? Give feedback.
-
Assuming you use the default serialization mechanism for .NET 6 An option is to create a custom Json Converter that ignores the properties, and then register the converter to the For example, suppose you have a public class UserData
{
public string Name { get; set; }
public string Password { get; set; }
} You can implement a custom JSON converter as follows: public class UserDataConverter : JsonConverter<UserData>
{
public override void Write(Utf8JsonWriter writer, UserData value, JsonSerializerOptions options)
{
writer.WriteStartObject();
using (var document = JsonDocument.Parse(JsonSerializer.Serialize(value)))
{
foreach (var property in document.RootElement.EnumerateObject())
{
if (property.Name != "Password")
{
property.WriteTo(writer);
}
}
}
writer.WriteEndObject();
}
public override UserData Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
} And in your startup: Audit.Core.Configuration.JsonSettings.Converters.Add(new UserDataConverter()); So any |
Beta Was this translation helpful? Give feedback.
-
Hello, I want to exclude some properties of dto class when logging. Like a password field etc..
How can I do that with this lib.?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions