Search code examples
entity-framework-corenlog

Excluding one Entry property in DBUpdateException from log in NLog


I have an entity with byte[] property, when DBUpdateException occurs it pours it into a json log, since DbUpdateException contains Entries list, which includes every single entry's\entity property. I wonder if there is a way to prevent this one property to be logged. I'm using NLog 5.2.5 (JsonLayout), EF Core 5.


Solution

  • You can override the property-relection for an object-type like this:

    LogManager.Setup().SetupSerialization(s =>
       s.RegisterObjectTransformation<DBUpdateException>(ex => new {
          Type = ex.GetType().ToString(),
          Message = ex.Message,
          StackTrace = ex.StackTrace,
          Source = ex.Source,
          Data = ex.Data,
          InnerException = ex.InnerException,
       })
    );
    

    See also: https://github.com/NLog/NLog/wiki/How-to-use-structured-logging#customize-object-reflection