/// <summary> /// Extends the logging rules in the "NLog.config" with node log settings rules. /// </summary> /// <param name="settings">Node log settings to extend the rules from the configuration file, or null if no extension is required.</param> /// <param name="dataFolder">Data folder to determine path to log files.</param> private static void AddFilters(LogSettings settings = null, DataFolder dataFolder = null) { if (settings == null) { return; } logSettings = settings; folder = dataFolder; // If we use "debug*" targets, which are defined in "NLog.config", make sure they log into the correct log folder in data directory. List <Target> debugTargets = LogManager.Configuration.AllTargets.Where(t => (t.Name != null) && t.Name.StartsWith("debug")).ToList(); foreach (Target debugTarget in debugTargets) { FileTarget debugFileTarget = debugTarget is AsyncTargetWrapper ? (FileTarget)((debugTarget as AsyncTargetWrapper).WrappedTarget) : (FileTarget)debugTarget; string currentFile = debugFileTarget.FileName.Render(new LogEventInfo { TimeStamp = DateTime.UtcNow }); debugFileTarget.FileName = Path.Combine(folder.LogPath, Path.GetFileName(currentFile)); if (debugFileTarget.ArchiveFileName != null) { string currentArchive = debugFileTarget.ArchiveFileName.Render(new LogEventInfo { TimeStamp = DateTime.UtcNow }); debugFileTarget.ArchiveFileName = Path.Combine(folder.LogPath, currentArchive); } } // Remove rule that forbids logging before the logging is initialized. LoggingRule nullPreInitRule = null; foreach (LoggingRule rule in LogManager.Configuration.LoggingRules) { if (rule.Final && rule.NameMatches("*") && (rule.Targets.Count > 0) && (rule.Targets[0].Name == "null")) { nullPreInitRule = rule; break; } } LogManager.Configuration.LoggingRules.Remove(nullPreInitRule); LayoutRenderer.Register("message", (logEvent) => { return(((logEvent.Parameters == null) ? logEvent.Message : string.Format(logEvent.Message, logEvent.Parameters)).Replace("\n", "\n\t")); }); var consoleTarget = new ColoredConsoleTarget { Name = "console", Layout = "[${level:lowercase=true}]\t${logger}\n\t${message}", AutoFlush = true, }; consoleTarget.RowHighlightingRules.Add(new ConsoleRowHighlightingRule("level == LogLevel.Info", ConsoleOutputColor.Gray, ConsoleOutputColor.Black)); consoleTarget.RowHighlightingRules.Add(new ConsoleRowHighlightingRule("level == LogLevel.Warn", ConsoleOutputColor.Gray, ConsoleOutputColor.Black)); consoleTarget.RowHighlightingRules.Add(new ConsoleRowHighlightingRule("level == LogLevel.Error", ConsoleOutputColor.Gray, ConsoleOutputColor.Black)); consoleTarget.WordHighlightingRules.Add(new ConsoleWordHighlightingRule("[info]", ConsoleOutputColor.DarkGreen, ConsoleOutputColor.Black)); consoleTarget.WordHighlightingRules.Add(new ConsoleWordHighlightingRule("[warn]", ConsoleOutputColor.Black, ConsoleOutputColor.Yellow)); consoleTarget.WordHighlightingRules.Add(new ConsoleWordHighlightingRule("[error]", ConsoleOutputColor.Black, ConsoleOutputColor.Red)); LogManager.Configuration.AddTarget(consoleTarget); // Logging level for console is always Info. LogManager.Configuration.LoggingRules.Add(new LoggingRule($"{nameof(Stratis)}.*", NLog.LogLevel.Info, consoleTarget)); LogManager.Configuration.LoggingRules.Add(new LoggingRule("Default", NLog.LogLevel.Warn, consoleTarget)); LogManager.Configuration.LoggingRules.Add(new LoggingRule($"{nameof(System)}", NLog.LogLevel.Warn, consoleTarget)); LogManager.Configuration.LoggingRules.Add(new LoggingRule($"{nameof(Microsoft)}", NLog.LogLevel.Warn, consoleTarget)); LogManager.Configuration.LoggingRules.Add(new LoggingRule($"{nameof(Microsoft)}.{nameof(Microsoft.AspNetCore)}", NLog.LogLevel.Error, consoleTarget)); // Configure main file target, configured using command line or node configuration file settings. var mainTarget = new FileTarget { Name = "main", FileName = Path.Combine(folder.LogPath, "node.txt"), ArchiveFileName = Path.Combine(folder.LogPath, "node-${date:universalTime=true:format=yyyy-MM-dd}.txt"), ArchiveNumbering = ArchiveNumberingMode.Sequence, ArchiveEvery = FileArchivePeriod.Day, MaxArchiveFiles = 7, Layout = "[${longdate:universalTime=true} ${threadid}${mdlc:item=id}] ${level:uppercase=true}: ${callsite} ${message}", Encoding = Encoding.UTF8 }; LogManager.Configuration.AddTarget(mainTarget); // Default logging level is Info for all components. var defaultRule = new LoggingRule($"{nameof(Stratis)}.*", settings.LogLevel, mainTarget); LogManager.Configuration.LoggingRules.Add(new LoggingRule("Default", NLog.LogLevel.Warn, mainTarget)); LogManager.Configuration.LoggingRules.Add(new LoggingRule($"{nameof(System)}", NLog.LogLevel.Warn, mainTarget)); LogManager.Configuration.LoggingRules.Add(new LoggingRule($"{nameof(Microsoft)}", NLog.LogLevel.Warn, mainTarget)); LogManager.Configuration.LoggingRules.Add(new LoggingRule($"{nameof(Microsoft)}.{nameof(Microsoft.AspNetCore)}", NLog.LogLevel.Error, mainTarget)); if (settings.DebugArgs.Any() && settings.DebugArgs[0] != "1") { lock (KeyCategories) { var usedCategories = new HashSet <string>(StringComparer.Ordinal); // Increase selected categories to Debug. foreach (string key in settings.DebugArgs) { if (!KeyCategories.TryGetValue(key.Trim(), out string category)) { // Allow direct specification - e.g. "-debug=Stratis.Bitcoin.Miner". category = key.Trim(); } if (!usedCategories.Contains(category)) { usedCategories.Add(category); var rule = new LoggingRule(category, NLog.LogLevel.Debug, mainTarget); LogManager.Configuration.LoggingRules.Add(rule); } } } } LogManager.Configuration.LoggingRules.Add(defaultRule); // Apply new rules. LogManager.ReconfigExistingLoggers(); }
/// <summary> /// Extends the logging rules in the "NLog.config" with node log settings rules. /// </summary> /// <param name="settings">Node log settings to extend the rules from the configuration file, or null if no extension is required.</param> /// <param name="dataFolder">Data folder to determine path to log files.</param> private static void AddFilters(LogSettings settings = null, DataFolder dataFolder = null) { if (settings == null) { return; } logSettings = settings; folder = dataFolder; // If we use debugFile target, which is defined in "NLog.config", we make sure it is in the log folder. Target debugTarget = LogManager.Configuration.FindTargetByName("debugFile"); if (debugTarget != null) { FileTarget debugFileTarget = debugTarget is AsyncTargetWrapper ? (FileTarget)((debugTarget as AsyncTargetWrapper).WrappedTarget) : (FileTarget)debugTarget; string currentFile = debugFileTarget.FileName.Render(new LogEventInfo { TimeStamp = DateTime.UtcNow }); debugFileTarget.FileName = Path.Combine(folder.LogPath, Path.GetFileName(currentFile)); } // Remove rule that forbids logging before the logging is initialized. LoggingRule nullPreInitRule = null; foreach (LoggingRule rule in LogManager.Configuration.LoggingRules) { if (rule.Final && rule.NameMatches($"*") && (rule.Targets.Count > 0) && (rule.Targets[0].Name == "null")) { nullPreInitRule = rule; break; } } LogManager.Configuration.LoggingRules.Remove(nullPreInitRule); // Configure main file target, configured using command line or node configuration file settings. var mainTarget = new FileTarget(); mainTarget.Name = "main"; mainTarget.FileName = Path.Combine(folder.LogPath, "node.txt"); mainTarget.ArchiveFileName = Path.Combine(folder.LogPath, "node-${date:universalTime=true:format=yyyy-MM-dd}.txt"); mainTarget.ArchiveNumbering = ArchiveNumberingMode.Sequence; mainTarget.ArchiveEvery = FileArchivePeriod.Day; mainTarget.MaxArchiveFiles = 7; mainTarget.Layout = "[${longdate:universalTime=true} ${threadid}${mdlc:item=id}] ${level:uppercase=true}: ${callsite} ${message}"; mainTarget.Encoding = Encoding.UTF8; LogManager.Configuration.AddTarget(mainTarget); // Default logging level is Info for all components. var defaultRule = new LoggingRule($"{nameof(Stratis)}.{nameof(Stratis.Bitcoin)}.*", NLog.LogLevel.Info, mainTarget); if (settings.DebugArgs.Any()) { if (settings.DebugArgs[0] == "1") { // Increase all logging to Trace. defaultRule = new LoggingRule($"{nameof(Stratis)}.{nameof(Stratis.Bitcoin)}.*", NLog.LogLevel.Trace, mainTarget); } else { HashSet <string> usedCategories = new HashSet <string>(StringComparer.Ordinal); // Increase selected categories to Trace. foreach (string key in settings.DebugArgs) { string category; if (!keyCategories.TryGetValue(key.Trim(), out category)) { // Allow direct specification - e.g. "-debug=Stratis.Bitcoin.Miner". category = key.Trim(); } if (!usedCategories.Contains(category)) { usedCategories.Add(category); var rule = new LoggingRule(category, NLog.LogLevel.Trace, mainTarget); LogManager.Configuration.LoggingRules.Add(rule); } } } } LogManager.Configuration.LoggingRules.Add(defaultRule); // Apply new rules. LogManager.ReconfigExistingLoggers(); }
/// <summary> /// Extends the logging rules in the "NLog.config" with node log settings rules. /// </summary> /// <param name="settings">Node log settings to extend the rules from the configuration file, or null if no extension is required.</param> /// <param name="dataFolder">Data folder to determine path to log files.</param> private static void AddFilters(LogSettings settings = null, DataFolder dataFolder = null) { if (settings == null) { return; } logSettings = settings; folder = dataFolder; // If we use "debug*" targets, which are defined in "NLog.config", make sure they log into the correct log folder in data directory. List <Target> debugTargets = LogManager.Configuration.AllTargets.Where(t => (t.Name != null) && t.Name.StartsWith("debug")).ToList(); foreach (Target debugTarget in debugTargets) { FileTarget debugFileTarget = debugTarget is AsyncTargetWrapper ? (FileTarget)((debugTarget as AsyncTargetWrapper).WrappedTarget) : (FileTarget)debugTarget; string currentFile = debugFileTarget.FileName.Render(new LogEventInfo { TimeStamp = DateTime.UtcNow }); debugFileTarget.FileName = Path.Combine(folder.LogPath, Path.GetFileName(currentFile)); if (debugFileTarget.ArchiveFileName != null) { string currentArchive = debugFileTarget.ArchiveFileName.Render(new LogEventInfo { TimeStamp = DateTime.UtcNow }); debugFileTarget.ArchiveFileName = Path.Combine(folder.LogPath, currentArchive); } } // Remove rule that forbids logging before the logging is initialized. LoggingRule nullPreInitRule = null; foreach (LoggingRule rule in LogManager.Configuration.LoggingRules) { if (rule.Final && rule.NameMatches("*") && (rule.Targets.Count > 0) && (rule.Targets[0].Name == "null")) { nullPreInitRule = rule; break; } } LogManager.Configuration.LoggingRules.Remove(nullPreInitRule); // Configure main file target, configured using command line or node configuration file settings. var mainTarget = new FileTarget { Name = "main", FileName = Path.Combine(folder.LogPath, "node.txt"), ArchiveFileName = Path.Combine(folder.LogPath, "node-${date:universalTime=true:format=yyyy-MM-dd}.txt"), ArchiveNumbering = ArchiveNumberingMode.Sequence, ArchiveEvery = FileArchivePeriod.Day, MaxArchiveFiles = 7, Layout = "[${longdate:universalTime=true} ${threadid}${mdlc:item=id}] ${level:uppercase=true}: ${callsite} ${message}", Encoding = Encoding.UTF8 }; LogManager.Configuration.AddTarget(mainTarget); // Default logging level is Info for all components. var defaultRule = new LoggingRule($"{nameof(Stratis)}.*", settings.LogLevel, mainTarget); if (settings.DebugArgs.Any() && settings.DebugArgs[0] != "1") { lock (KeyCategories) { var usedCategories = new HashSet <string>(StringComparer.Ordinal); // Increase selected categories to Debug. foreach (string key in settings.DebugArgs) { if (!KeyCategories.TryGetValue(key.Trim(), out string category)) { // Allow direct specification - e.g. "-debug=Stratis.Bitcoin.Miner". category = key.Trim(); } if (!usedCategories.Contains(category)) { usedCategories.Add(category); var rule = new LoggingRule(category, settings.LogLevel, mainTarget); LogManager.Configuration.LoggingRules.Add(rule); } } } } LogManager.Configuration.LoggingRules.Add(defaultRule); LogManager.Configuration.LoggingRules.Add(new LoggingRule($"Blockcore.*", settings.LogLevel, mainTarget)); LogManager.Configuration.LoggingRules.Add(new LoggingRule($"City.*", settings.LogLevel, mainTarget)); // Apply new rules. LogManager.ReconfigExistingLoggers(); }