using NLog; using NLog.Config; using NLog.Targets; namespace FsToolbox.Cli.Helper { /// /// Central logging setup using NLog with console and file targets. /// public static class LoggingService { private static bool _initialized; /// /// Configures NLog with console and rolling file targets. /// public static void Initialize() { if (_initialized) return; var config = new LoggingConfiguration(); var layout = "${longdate} | ${uppercase:${level}} | ${logger} | ${message}${onexception:inner=${newline}${exception:format=tostring}}"; var consoleTarget = new ConsoleTarget("console") { Layout = layout }; var fileTarget = new FileTarget("file") { FileName = "${basedir}/logs/fstool.log", ArchiveEvery = FileArchivePeriod.Day, ArchiveNumbering = ArchiveNumberingMode.Rolling, MaxArchiveFiles = 7, Layout = layout, CreateDirs = true }; config.AddRule(LogLevel.Info, LogLevel.Fatal, consoleTarget); config.AddRule(LogLevel.Info, LogLevel.Fatal, fileTarget); LogManager.Configuration = config; _initialized = true; } /// /// Gets a logger for the specified type. /// public static Logger GetLogger() => LogManager.GetLogger(typeof(T).FullName); /// /// Gets a logger for the specified name. /// public static Logger GetLogger(string name) => LogManager.GetLogger(name); } }