Skip to main content


I just love string interpolation with dollar signs in #CSharp. so much more concise than String.Format, especially when writing log messages!
in reply to André Polykanine

Oh boy, you should never ever do that for log message ever. The reason is pretty simple, you will construct strings every time no matter if the message is logged or not. Use the proper logger template mechanics instead (learn.microsoft.com/en-us/dotn…, github.com/serilog/serilog/wik…).
in reply to André Polykanine

Keep in mind if you use a structured logger like Serilog then if you interpolate the string then it won't pick out the structured properties. It took us a while to figure that out
in reply to André Polykanine

@bigolewannabe I think someone else already answered, but since serilog supports structured logging, you can have a much better output by naming the values in the message template and passing the values in instead of using string interpolation
There are analyzers to guide you toward that pattern too
github.com/serilog/serilog/wik…
in reply to ESG

@esg @bigolewannabe What about exception messages, is it OK to have $-interpolation there or not either?
in reply to André Polykanine

@bigolewannabe No! Even worse 😅
Each logging method have an overload, taking the exception as the first parameter
ex:
```
Logger.Information(exception, "Something bad happened here");
```
This entry was edited (1 month ago)
in reply to ESG

@esg @bigolewannabe I mean, when I throw: `Throw new SomeFancyException($"...")`. Also no-no? Sorry, it's my first year in .NET 🙏🏻
in reply to André Polykanine

@esg @bigolewannabe If you throw the exception as well, then yes, it is fine because there is no other possible execution branch continuing. That said, you should always throw an exception right after construction anyway, so there's no issue there. However throwing an exception just to log it is a bit of an anti-pattern as well, you should log and then throw when possible (allows to generate logger properties which can later be filtered/used with structured logging).
in reply to MaxiTB

@maxitb @esg @bigolewannabe Oh yeah, thanks, I do log then throw, I asked just to make sure throwing with $"" messages is fine.
in reply to André Polykanine

@esg @bigolewannabe Yes it is ok and it is what you should actually do because it is not a message template.