2

I am trying to use structured logging and log an object.

I run into this error when trying to log the object: System.IO.FileNotFoundException: 'Could not find file 'C:\Non-Existent-File'.' This exception is in the FileInfo Length property.

Is there a way to have NLog ignore this one property or just log Bar.ToString()?

public class Foo()
{
     public FileInfo Bar {get;set} = FileInfo("C:\Non-Existent File");
     // More properties
}

public class App
{
    private static readonly Logger MyLogger = LogManager.GetLogger("MyLogger");

    public App()
    {
        MyLogger.Info("Foo is [{@foo}]", Foo);
    }
}

I tried setting the json layout MaxRecursionLimit=0 and that didn't seem to work.

These examples are very close and could be used to work around if needed, but don't quite answer my question:

JasonC
  • 139
  • 8
  • If you want Foo.ToString() with property-reflection, then you should just replace `@` with `$`. Ex. `"Foo is [{$foo}]"`. You can also register custom handling of `FileInfo` by using `RegisterObjectTransformation`. See also https://github.com/NLog/NLog/wiki/How-to-use-structured-logging#transform-captured-properties – Rolf Kristensen Mar 16 '23 at 20:25

0 Answers0