serilog / serilog-settings-configuration

A Serilog configuration provider that reads from Microsoft.Extensions.Configuration
Apache License 2.0
446 stars 129 forks source link

Missing rendered messsage when logging using LogEvent #374

Closed yohny closed 1 year ago

yohny commented 1 year ago

I have a project where some logs are produced using LogEvent and I noticed that in those cases the renderd message (message template with injected values of propeties) is missing. To reproduce, try logging the following messages in regular and LogEvent fasion:

Log.Warning("my log message 1");
Log.Write(new LogEvent(
           DateTimeOffset.Now,
           LogEventLevel.Warning,
           null,
           new MessageTemplate("my log message 2", Enumerable.Empty<MessageTemplateToken>()),
           Enumerable.Empty<LogEventProperty>()));

Using the following ExpressionTemplate: "{ {@t, @l, @mt, @m, @x, ..@p} }\n" and the result is:

{"@t":"2023-04-17T16:32:15.5767953+02:00","@l":"Warning","@mt":"my log message 1","@m":"my log message 1","ThreadId":1,"Application":"my-app"}
{"@t":"2023-04-17T16:32:15.5799729+02:00","@l":"Warning","@mt":"my log message 2","@m":"","ThreadId":1,"Application":"my-app"}

As you can see in the second line logged using LogEvent the @m value is empty, althout it is expected to contain the rendered message (in this case the same as message template as there are no tokens present). This also happens when using RenderedCompactJsonFormatter - rendered message is empty.

nblumhardt commented 1 year ago

Hi @yohny - your message template is incorrectly constructed: even if it doesn't include any property placeholders, it should at least have one MessageTemplateTextToken with the full text of the template in it.

Using MessageTemplateParser to create the MessageTemplate will sidestep any other similar problems.

yohny commented 1 year ago

thanks, MessageTemplateParser solved the issue