You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varconfig=newNLog.Config.LoggingConfiguration();// Targets where to log to: File and Consolevarlogfile=newNLog.Targets.FileTarget("logfile"){FileName="file.txt"};varlogconsole=newNLog.Targets.ConsoleTarget("logconsole");// Rules for mapping loggers to targets config.AddRule(LogLevel.Info,LogLevel.Fatal,logconsole);config.AddRule(LogLevel.Debug,LogLevel.Fatal,logfile);// Apply config NLog.LogManager.Configuration=config;
Extended Features
Passing Custom Values
There are many ways to capture current Context and output it to the final target. Simple example:
logger.Info("Message with {myProperty}","myValue");
NLog Layout will implicitly convert from string-objects. It will automatically parse the string and compile it into the recognized Layout-Renderers.
NLog will perform validation when parsing, but errors detected will be swallowed unless having enabled LogManager.ThrowConfigExceptions. But one can also force NLog to throw parsing error no matter what:
varlayout=NLog.Layouts.Layout.FromString("${evil}",throwConfigExceptions:true);// will throw
NLog 4.7 also allows you to generate a Layout from a simple lambda-method:
!! NOTICE !! Ensure that custom method given to Layout.FromMethod is very simple. Deadlock or StackOverflow can occur if performing complex logic that triggers new logevents or acquiring locks.
Update config in code
It is possible to dive into the NLog LoggingConfiguration and assign properties directly. But NLog is much happier about using NLog Layout logic when possible. Example:
This removes the need to know the exact NLog-target names in the NLog.config, so it decouples the logic even further and makes it more resilient. This will also work excellent together with autoReload="true", as you don't have to re-apply the same settings again, as it will happen automatically. This can also be used for adjusting logging rules dynamically at runtime.
But if you really want to get your hands dirty, then here you go:
varconfiguration=LogManager.Configuration;// Update single targetvarmyFileTarget=configuration.FindTargetByName<FileTarget>("myTargetName");if(myFileTarget!=null)myFileTarget.FileName="SuperPower"+"_${shortdate}.log";LogManager.Configuration=configuration;// re-init// Update all targetsforeach(varfileTarget= configuration.AllTargets.OfType<FileTarget>()){fileTarget.FileName="SuperPower"+"_${shortdate}.log";}
LogManager.Configuration=configuration;// re-init
Combine nlog.config and config from code
Please note that combining the config file (nlog.config) and changing it in code, the reload of nlog.config could undo your changes. If you combine both, then reapply the changes on the reload event. E.g.
// On start of your programUpdateConfig();LogManager.ConfigurationChanged+=(sender,e)=>{if(e.ActivatedConfiguration!=null){//Re apply if config reloadedUpdateConfig();}};
Where UpdateConfig is
publicvoidUpdateConfig(){// Do NOT set LogManager.Configuration because that will cause recursion and stackoverflowvarfileTarget=LogManager.Configuration.FindTargetByName<FileTarget>("myTargetName");fileTarget.FileName="${basedir}/file.log";LogManager.ReconfigExistingLoggers();// Soft refresh}
Instead of trying to perform patching of NLog-configuration, then it is recommended to make use of NLog Layouts for Target- and LoggingRule-configuration forexample using ${gdc}