I am replacing our log4net logger to structured logging using Serilog. I want to enforce the tagging in the code using an Enum but I'm having some difficulties how to do so.
For now my log look like that:
logger.ErrorFormat("Proxy Logic for the Item {Item} failed. Swallow exception", Item);
When ErrorFormat
signutare is
void ErrorFormat(string format, params object[] args);
but I don't know how to replace Item
to something like LogTags.TagA
. When TagA is an Enum which will give me the way to enforce a uniform standard for my tagging.
so it would look like:
public enum LogTags
{
TagA,
...
}
public class Foo
{
public void DoError()
{
logger.ErrorFormat("Proxy Logic for the Item {@LogTags.TagA} failed. Swallow exception", Item);
}
}
What is the best practice to achieve such thing?
Property names in Serilog need to be simple, non-dotted identifiers, so a name like Enum.LogTags.Item
won't work directly.
You can force this nesting with something like the pattern below, if a simple name is inadequate:
logger.ErrorFormat(
"Proxy Logic for the Item {@Enum} failed. Swallow exception",
new { LogTags = new { Item }});