var config = new NLog.Config.LoggingConfiguration(); var fileTarget = new NLog.Targets.FileTarget("logfile") { FileName = "log.txt" }; config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, fileTarget); NLog.LogManager.Configuration = config; var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Info("Logging to a file");
var config = new NLog.Config.LoggingConfiguration(); var consoleTarget = new NLog.Targets.ConsoleTarget("console"); config.AddRule(NLog.LogLevel.Trace, NLog.LogLevel.Fatal, consoleTarget); NLog.LogManager.Configuration = config; var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Info("Logging to the console");This example creates a `ConsoleTarget` that logs messages from `Trace` to `Fatal` levels to the console. The rest of the code is very similar to Example 1, but the target being added to the configuration is a `ConsoleTarget` instead of a `FileTarget`. Package library: NLog (version 4.7.12 or higher).