c# - Separate log file for specific class instance using NLog -


i need write event log every instance of class separate file. historically project uses nlog logging, want try resolving issue using nlog (i've found similar topic unique log file each instance of class , it's suggests using log4net)

currently i'm getting instance of logger this:

    public static logger getinstancelogger(string name, bool init = false)     {         if (!logmanager.configuration.alltargets.any(t => t.name == name))         {             var target = new filetarget();                  target.name = name;                 target.filename = string.format("logs/{0}.${{shortdate}}.log", name);              target.layout =            "${date:format=dd.mm.yyyy hh\\:mm\\:ss.fff} thread[${threadid}] ${logger} (${level:uppercase=true}): ${message}. ${exception:format=tostring}";             var config = init ? new loggingconfiguration() : logmanager.configuration;             config.addtarget(name, target);              var ruleinfo = new loggingrule("*", loglevel.trace, target);              config.loggingrules.add(ruleinfo);              logmanager.configuration = config;              logmanager.reconfigexistingloggers();         }          var logger = logmanager.getlogger(name);          return logger;     } 

right it's writing same log files (i suppose it's caused log level). there way accomplish task using nlog?

thanks.

i came out solution using event properties of layout renderer in filename. when i'm writing new message log, i'm adding filename property logeventinfo

protected virtual void sendevent(loglevel level, string message, exception exception, string membername = null)     {         var logevent = new logeventinfo(level, _name, message);         logevent.exception = exception;         foreach (string key in _properties.keys)         {             logevent.properties[key] = _properties[key];         }         _logger.log(logevent);     } 

and in configuration file in nlog targets section:

  <targets async="true">   <target xsi:type="file" name="f" filename="${basedir}/logs/${shortdate}_${event-properties:item=name}.log"           layout="${detailedlayout}" />   <target xsi:type="file" name="errorlogfile" filename="${basedir}/logs/${shortdate}.error_${event-properties:item=name}.log"       layout="${detailedlayout}" /> </targets> 

Comments