/// <summary> /// Adds a color rule to the list of color rules /// </summary> /// <param name="foregroundColor">The foreground color of the rule</param> /// <param name="backgroundColor">The background color of the rule</param> /// <param name="tags">The tags used to match log events to the rule</param> /// <returns>This console target config builder instance</returns> public ConsoleTargetConfigBuilder AddConsoleColorRule(string foregroundColor, string backgroundColor, params string[] tags) { // Create a new rule and set the passed tags ConsoleColorRule rule = new ConsoleColorRule(); rule.Tags = tags; // Parse the colors from the passed in string color names ConsoleColor? fColor = GetColorByName(foregroundColor); ConsoleColor? bColor = GetColorByName(backgroundColor); // And assign them if they are found if (fColor.HasValue) rule.ForegroundColor = fColor.Value; if (bColor.HasValue) rule.BackgroundColor = bColor.Value; // Add the new rule to the list ColorRules.Add(rule); return this; }
/// <summary> /// Instantiates a console target config using the passed JSON token /// </summary> /// <param name="jToken">The JSON token to parse for this console target config</param> public ConsoleTargetConfig(JToken jToken) : base(jToken, synchronous: true) { // Initialize our type, and the list of color rules Type = typeof(ConsoleTarget).FullName; ColorRules = new List<ConsoleColorRule>(); // If a JSON configuration was provided if (jToken != null) { // Get a hold of the color rules in the JSON if they are provided var colorRulesToken = jToken[ColorRulesTokenName]; if (colorRulesToken != null && colorRulesToken.Type == JTokenType.Array) { // Prepare to parse the rules var colorRulesTokens = colorRulesToken.Children(); ConsoleColorRule colorRule; JToken tags; JToken foreColorToken; JToken backColorToken; // Iterate over each of the child JSON tokens // in the rules foreach (var colorRuleToken in colorRulesTokens) { try { // Make sure that the child element is an object if (colorRuleToken.Type == JTokenType.Object) { // And if so, parse out the parts colorRule = new ConsoleColorRule(); tags = colorRuleToken[TagsTokenName]; if (tags != null && tags.Type == JTokenType.Array) colorRule.Tags = tags.Values<string>().ToList(); foreColorToken = colorRuleToken[ForeColorTokenName]; if (foreColorToken != null && foreColorToken.Type == JTokenType.String) colorRule.ForegroundColor = (ConsoleColor)Enum.Parse(ConsoleColorType, foreColorToken.Value<string>()); backColorToken = colorRuleToken[BackColorTokenName]; if (backColorToken != null && backColorToken.Type == JTokenType.String) colorRule.BackgroundColor = (ConsoleColor)Enum.Parse(ConsoleColorType, backColorToken.Value<string>()); ColorRules.Add(colorRule); } } catch (Exception e) { // Just ignore this color rule - colors are not mission critical // Log the failure Trace.WriteLine(e, TraceConfigCategory); } } } } }
/// <summary> /// Adds a color rule to the list of color rules /// </summary> /// <param name="consoleColorRule">The color rule to add</param> /// <returns>This console target config builder instance</returns> public ConsoleTargetConfigBuilder AddConsoleColorRule(ConsoleColorRule consoleColorRule) { ColorRules.Add(consoleColorRule); return this; }