c# - can I pass a custom property to NLOG and output to file? -
edit 4: "from" seems reserved word in nlog. changing "fromid" worked. awesome way pass variables nlog , still keep code clean !!!! thank mike!!!
edit 3. idea.:
implemented helper class mike suggested below:
public class nloghelper { // // class properties // private logger m_logger; private dictionary<string, object> m_properties; // // constructor // public nloghelper(logger logger) { m_logger = logger; m_properties = new dictionary<string, object>(); } // // setting logger properties per instancce // public void set(string key, object value) { m_properties.add(key, value); } // // loggers // public void debug(string format, params object[] args) { m_logger.debug() .message(format, args) .properties(m_properties) .write(); }
and in main code, have:
private nloghelper m_logger; public void start() { m_logger = new nloghelper(logmanager.getcurrentclasslogger()); m_logger.set("from", "qrt123"); // class setting. m_logger.debug("hello "); }
and target set in config file follows:
<target xsi:type="file" name ="logfile" filename="c:\qrt\logs\qrtlog-${shortdate}.log" layout ="${date}|${level}|${event-properties:item=from}|${message} "/>
but output has blank in place of 'from' property ???
so i'm there... not seem work??
edit 2: trying create own version of nlog call:
private void log_debug (string message) { logeventinfo theevent = new logeventinfo(loglevel.debug, "what this?", message); theevent.properties["employeeid"] = m_employeeid; m_logger.log(theevent); }
the issue have format string calls (but huge performance deal)... seems hack??
ideally, declare properties in custom layout renderer , instead of setting properties in configuration file, each instance of class have property set... [id = m_id]
whole class. way whenever nlog called class, id property set , nlog's custom layout renderer can use property output it. making sense??
i'm new nlog , have been looking @ custom renderers. basically, goal have log statements be: _logger.debug ("my name {0}", "ed", id=87);
and i'd rendered like: layout = ${id} ${date} ${level} ${message}
that's it. ${id} can have default value of 0. fine. ideally, i'd every call have ability specify id without needing have 3 lines everytime want log.
i've seen custom renderers allowing me customize output i'm not sure how can customize properties pass without
https://github.com/nlog/nlog/wiki/extending%20nlog shows how can add properties don't know how call them.
also, https://github.com/nlog/nlog/wiki/event-context-layout-renderer shows how can set custom properties involved creation of logeventinfo object every time want log something.
nlog custom layoutrenderer shows how customize output.. again... not how customize inputs.
this console app in c# targeting .net 4.0 using vs2013
thanks -ed
event properties (used called event-context) built-in way want. if using nlog 3.2+ can use fluent api, may bit more appealing creating logeventinfo
objects. can access api by using namespace nlog.fluent
.
your layout defined this:
${event-properties:item=id} ${date} ${level} ${message}
then using fluent api, log this:
_logger.debug() .message("my name {0}", "ed") .property("id", 87) .write();
other setting properties per event above, other option set properties per thread using mdc or mdls.
nlog dosen't have way (that have found) of setting per-logger properties. internally, nlog caches logger
instances logger name, not guarantee same instance of logger
returned given logger name. example if call logmanager.getcurrentclasslogger()
in constructor of class, most of time same instance of logger instances of class. in case, not able have separate values on logger, per instance of class.
perhaps create logging helper class can instantiate in class. helper class can initialized per-instance property values logged every message. helper class provide convenience methods log messages above, 1 line of code. this:
// example of class needs use logging public class myclass { private loggerhelper _logger; public myclass(int id) { _logger = new loggerhelper(logmanager.getcurrentclasslogger()); // per-instance values _logger.set("id", id); } public void dostuff() { _logger.debug("my name {0}", "ed"); } } // example of "stateful" logger public class loggerhelper { private logger _logger; private dictionary<string, object> _properties; public loggerhelper(logger logger) { _logger = logger; _properties = new dictionary<string, object>(); } public void set(string key, object value) { _properties.add(key, value); } public void debug(string format, params object[] args) { _logger.debug() .message(format, args) .properties(_properties) .write(); } }
this work same layout above.
Comments
Post a Comment