Home > Enterprise >  .NET Entity Framework Core 3.1 - Nlog leaking memory when :format=@ when calling update on a big mod
.NET Entity Framework Core 3.1 - Nlog leaking memory when :format=@ when calling update on a big mod

Time:01-06

Happens in the save below:

context.Update(myBigObject);

await context.SaveChangesAsync(); -> Hangs here and RAM goes up until all the memory is finished

It only happens when a db error occurs ("String or binary data would be truncated" to be exact, meaning trying to stuff too large string in a field that's too small).

It happens when nlog config is: ${exception:format=@}

One "fix" is to change it to: ${exception:format=toString} -> But then I lose all the inner exception logging

See nlog docs on the difference between :format=@ and :format=toString:

https://github.com/NLog/NLog/wiki/Exception-Layout-Renderer

It's happening to more people than me (see bottom comment) and happening in both Serilog and Nlog (so maby it's a EF Core thing):

https://github.com/dotnet/efcore/issues/24663#issuecomment-1349965403

Any idea how to fix without using :format=toString in nlog config?

CodePudding user response:

NLog 4.7 allows you to override the reflection for a specific exception-type (Ex. Microsoft.EntityFrameworkCore.DbUpdateException) like this:

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

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

  • Related