.net - How do I render a template and its arguments manually in Serilog? -
.net - How do I render a template and its arguments manually in Serilog? -
how implement next method?
string buildserilogmessage(string messagetemplate, params object[] propertyvalues);
the arguments same ones accepted ilogger.debug
etc.
the reason why want because want preserve template/values in intermediate exception needs have single string message well. this:
// exception designed loggable serilog [serializable] public class structuredexception : exception { public string messagetemplate { get; private set; } public object[] propertyvalues { get; private set; } public structuredexception(string messagetemplate, params object[] propertyvalues) : base(buildmessage(messagetemplate, propertyvalues)) { messagetemplate = messagetemplate; propertyvalues = propertyvalues; } public structuredexception(exception inner, string messagetemplate, params object[] propertyvalues) : base(buildmessage(messagetemplate, propertyvalues), inner) { messagetemplate = messagetemplate; propertyvalues = propertyvalues; } private static string buildmessage(string messagetemplate, object[] propertyvalues) { // ??? } }
there's an illustration in akka.net repo of beingness done, though scalar values supported in arguments.
the basic notion utilize messagetemplateparser
template:
var parser = new messagetemplateparser(); var template = parser.parse(messagetemplate);
then zip tokens template arguments:
var properties = template.tokens .oftype<propertytoken>() .zip(propertyvalues, tuple.create) .todictionary( p => p.item1.propertyname, p => new scalarvalue(p.item2));
finally rendering:
var rendered = template.render(properties);
(apologies in advance bits may not compile here.)
.net serilog
Comments
Post a Comment