NLog callback possible? -
i in process of converting home grown logging system nlog , wondering if there way add event logger or otherwise support mechanism when log message can callback final formatted message , loglevel. use send server messages connected client.
thx
this mcve of talking in comments. create target accepts callback functions:
[target("myfirst")] public sealed class myfirsttarget : targetwithlayout { private readonly action<string>[] _callbacks; public myfirsttarget(params action<string>[] callbacks) { _callbacks = callbacks; } protected override void write(logeventinfo logevent) { foreach (var callback in _callbacks) { callback(logevent.formattedmessage); } } }
configure nlog use target. programmatically since callbacks passed in constructor. can configure target in nlog.config, target need singleton can register callbacks in code.
class program { public static void main() { logmanager.configuration.addtarget("myfirst", new myfirsttarget(s => debug.writeline(s))); var logger = logmanager.getcurrentclasslogger(); logger.debug("test"); } }
with no other nlog configuration (copy code empty project , add nlog nuget package), emit message debug window.
Comments
Post a Comment