.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

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -