Is it valid Serilog code to put a "$" in front of a string?

Viewed 328

I am not familiar with Serilog. I am going over the code because as of yet it is not writing its information to Splunk. I think I have figured out the problem but I would like someone to confirm my hypothesis.

splunkLog.Information($"An account for user '{userName}' was successfully created");

Is there any reason for an $ to be placed in front of a string like this? All I can find in the documentation is the $ is used in front of properties for serialization. I can't find any reference to using it in front of a string.

Thanks in advance

1 Answers

The line:

splunkLog.Information($"An account for user '{userName}' was successfully created");

is considered bad form with Serilog, because it will produce an unstructured log message (text log), with userName formatted into the string before Serilog sees it.

The alternative:

splunkLog.Information("An account for user '{UserName}' was successfully created", userName);

will produce a structured log event. The message will be the same, but Serilog will also capture a separate property UserName with the value of userName in it. Much nicer if you want to search/analyze logs based on the username.

Related