0

I have an ExpressionTemplate that allows for ..dynamicProps as part of a log message. This code is in my Program.cs file.

string jsonTemplate = "{ { timestamp: @t, level: @l, message: @m,"
       + "foobar:Foobar, "
       + "..dynamicProps ,"
       + " @x } }\n";

var loggerConfig = new LoggerConfiguration()
     .MinimumLevel.Debug()
     .WriteTo.Console(new ExpressionTemplate(jsonTemplate))
     .Enrich.FromLogContext();

Log.Logger = loggerConfig.CreateLogger();

Then in my method where I wish to Log, I have this:

CustomLog dynamicProps = new()
{
  item = "SomeItem",
  val = DateTime.Now.Ticks,
};
using (LogContext.PushProperty("dynamicProps", dynamicProps, true))
{
    using (LogContext.PushProperty("Foobar", "some foobar thing"))
    {
        _logger.LogDebug("Starting!");
    }
}

That works fine when I am NOT running with an AOT compiled version:

{"timestamp":"2025-04-23T11:58:09.5282819-06:00","level":"Debug","message":"Starting!","foobar":"some foobar thing","item":"SomeItem","val":638810062895182809}

However, when I publish for AOT, I do not get the properties from my CustomLog

{"timestamp":"2025-04-23T11:58:53.6560097-06:00","level":"Debug","message":"Starting!","foobar":"some foobar thing"}

My CustomLog class looks like this:

[JsonSerializable(typeof(CustomLog),GenerationMode = JsonSourceGenerationMode.Default)]
public class CustomLog {
   [JsonInclude]
   public string item { get; set; }
   [JsonInclude]
   public long val { get; set; }
}

I would like to see my CustomLog properties in my log message, but I can't seem to get that to work. I have tried it without the JsonSerializable attribute with the same results.
I know that Serilog has a few special behaviors that it does not support for AOT because AOT and reflection do not play in the same sandbox. However, is there any way I can get my dynamicProps class to work in a log message (when AOT compiled)?
That would save me a ton of work.
I have several log messages that I need to publish first class json properties to (for my logging visualizer to consume) and I was hoping that the ExpressionTemplate would work - it does unless I use AOT.
Any guidance is greatly appreciated - this is a slim-downed repro of my larger project that contains a dozen or so dynamicProps oriented messages.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.