Created
September 14, 2014 23:27
-
-
Save stefansedich/9fc1da37e3502b8a3b78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
FormatUsingSerilog("{Message} {Number}", "Foo", 2).Dump(); // Outputs: "Foo" 2 | |
FormatUsingSerilog("{@Foo}", new { Property = 2 }).Dump(); // Outputs: { Property = 2 } | |
FormatUsingSerilog("{@Foo}", new Foo { Property = 2 }).Dump(); // Outputs: UserQuery+Foo | |
} | |
static string FormatUsingSerilog(string format, params object[] args) { | |
var parser = new MessageTemplateParser(); | |
var template = parser.Parse(format); | |
var propertyTokens = template.Tokens.OfType<PropertyToken>().ToArray(); | |
var properties = new Dictionary<string, LogEventPropertyValue>(); | |
if(propertyTokens.Length != args.Length) | |
throw new FormatException("Invalid number or arguments provided."); | |
for(var i = 0; i < propertyTokens.Length; i++) { | |
var arg = args[i]; | |
properties.Add(propertyTokens[i].PropertyName, new ScalarValue(arg)); | |
} | |
return template.Render(properties); | |
} | |
class Foo { | |
public int Property { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment