/// <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)); } // 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)}.{nameof(Bitcoin)}.*", settings.LogLevel, mainTarget); if (settings.DebugArgs.Any() && settings.DebugArgs[0] != "1") { 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); // Apply new rules. LogManager.ReconfigExistingLoggers(); }