I am 2 hours deep but cant find a solution: How to change the error layouit for log.error(Exception, string) within the code (no config file)?
This is just a random config, since question contains has no clues or requirements on input or output (Besides solution must be implemented with code). This will redirect all Error-LogEvents to a seperate file with its own custom Layout.
var config = new NLog.Config.LoggingConfiguration();
// Targets where to log to: File and Console
var logfile = new NLog.Targets.FileTarget("logfile") { FileName = "file.txt" };
var errorfile = new NLog.Targets.FileTarget("errorfile") { FileName = "error.txt" };
errorfile.Layout = "${longtime}|${level}|${message}|${exception}";
// Rules for mapping loggers to targets
config.AddRule(LogLevel.Error, LogLevel.Fatal, errorfile, "*", true); // Final
config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile, "*");
// Apply config
NLog.LogManager.Configuration = config;
You could also have the two file-targets pointing to the same file, but this is only possible if using KeepFileOpen=false
(default).
Alternative you can make use of When-condition to control if additional output should be included. See also: https://github.com/NLog/NLog/wiki/When-Layout-Renderer