From 92930404c7f6423d6076054d1fa008c42408fd11 Mon Sep 17 00:00:00 2001 From: Zoltan David Date: Tue, 9 Aug 2022 14:31:57 +0200 Subject: [PATCH 01/13] improves logging based the latest onboarding tests --- src/ConfigCatClient.sln | 4 +-- .../Evaluate/EvaluateLogger.cs | 11 +++----- .../Evaluate/RolloutEvaluator.cs | 26 +++++++++---------- src/ConfigCatClient/Logging/ConsoleLogger.cs | 25 +++++++++++++----- src/ConfigCatClient/Logging/LogLevel.cs | 10 +++---- 5 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/ConfigCatClient.sln b/src/ConfigCatClient.sln index b7ddced9..6f2e6a0c 100644 --- a/src/ConfigCatClient.sln +++ b/src/ConfigCatClient.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30907.101 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32630.192 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfigCatClient", "ConfigCatClient\ConfigCatClient.csproj", "{3046A17C-28AC-49A7-8B7B-E618FB5992F0}" EndProject diff --git a/src/ConfigCatClient/Evaluate/EvaluateLogger.cs b/src/ConfigCatClient/Evaluate/EvaluateLogger.cs index b24a7de6..10ff89d3 100644 --- a/src/ConfigCatClient/Evaluate/EvaluateLogger.cs +++ b/src/ConfigCatClient/Evaluate/EvaluateLogger.cs @@ -25,18 +25,13 @@ public override string ToString() { var result = new StringBuilder(); - result.AppendLine($" Evaluate '{KeyName}'"); - - result.AppendLine($" VariationId: {this.VariationId ?? "null"}"); - - result.AppendLine($" User object: {this.User.Serialize()}"); - + result.Append($"Evaluating '{KeyName}'"); + result.AppendLine($" for user '{this.User.Serialize()}'"); foreach (var o in this.Operations) { result.AppendLine(" " + o); } - - result.AppendLine($" Returning: {this.ReturnValue}"); + result.Append($" Returning '{this.ReturnValue}' (VariationId: '{this.VariationId ?? "null"}')."); return result.ToString(); } diff --git a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs index 51a9aece..37cf76ca 100644 --- a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs +++ b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs @@ -69,7 +69,7 @@ private EvaluateResult EvaluateLogic(IDictionary settings, stri if (!settings.TryGetValue(key, out var setting)) { var keys = string.Join(",", settings.Keys.Select(s => $"'{s}'").ToArray()); - this.log.Error($"Evaluating '{key}' failed. Returning default value: '{logDefaultValue}'. Here are the available keys: {keys}."); + this.log.Error($"Evaluating '{key}' failed (key not found in ConfigCat). Returning the sourcecode-level default value: '{logDefaultValue}'. Here are the available keys: {keys}."); return null; } @@ -110,7 +110,7 @@ private EvaluateResult EvaluateLogic(IDictionary settings, stri } else if (setting.RolloutRules.Any() || setting.RolloutPercentageItems.Any()) { - this.log.Warning($"Evaluating '{key}'. UserObject missing! You should pass a UserObject to GetValue() or GetValueAsync(), in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object"); + this.log.Warning($"Cannot evaluate targeting rules and % options for '{key}'. You should pass a UserObject to GetValue() or GetValueAsync(), in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object"); } // regular evaluate @@ -129,7 +129,7 @@ private EvaluateResult EvaluateLogic(IDictionary settings, stri } finally { - this.log.Information(evaluateLog.ToString()); + this.log.Information($"{evaluateLog}"); } } @@ -155,7 +155,7 @@ private static bool TryEvaluateVariations(ICollection if (hashScale >= bucket) continue; result.Value = variation.Value; result.VariationId = variation.VariationId; - evaluateLog.Log($"Evaluating % options, '{key}' evaluated to '{variation.Value}'."); + evaluateLog.Log($"- % options: evaluated to '{variation.Value}'."); return true; } } @@ -185,7 +185,7 @@ private static bool TryEvaluateRules(ICollection rules, User use continue; } - string l = $"Evaluate rule: '{comparisonAttributeValue}' {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}' => "; + string l = $"- rule eval: ['{comparisonAttributeValue}' {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}'] => "; switch (rule.Comparator) { @@ -196,7 +196,7 @@ private static bool TryEvaluateRules(ICollection rules, User use .Select(t => t.Trim()) .Contains(comparisonAttributeValue)) { - logger.Log(l + "match"); + logger.Log(l + "MATCH"); return true; } @@ -212,7 +212,7 @@ private static bool TryEvaluateRules(ICollection rules, User use .Select(t => t.Trim()) .Contains(comparisonAttributeValue)) { - logger.Log(l + "match"); + logger.Log(l + "MATCH"); return true; } @@ -224,7 +224,7 @@ private static bool TryEvaluateRules(ICollection rules, User use if (comparisonAttributeValue.Contains(rule.ComparisonValue)) { - logger.Log(l + "match"); + logger.Log(l + "MATCH"); return true; } @@ -236,7 +236,7 @@ private static bool TryEvaluateRules(ICollection rules, User use if (!comparisonAttributeValue.Contains(rule.ComparisonValue)) { - logger.Log(l + "match"); + logger.Log(l + "MATCH"); return true; } @@ -253,7 +253,7 @@ private static bool TryEvaluateRules(ICollection rules, User use if (EvaluateSemVer(comparisonAttributeValue, rule.ComparisonValue, rule.Comparator)) { - logger.Log(l + "match"); + logger.Log(l + "MATCH"); return true; } @@ -271,7 +271,7 @@ private static bool TryEvaluateRules(ICollection rules, User use if (EvaluateNumber(comparisonAttributeValue, rule.ComparisonValue, rule.Comparator)) { - logger.Log(l + "match"); + logger.Log(l + "MATCH"); return true; } @@ -285,7 +285,7 @@ private static bool TryEvaluateRules(ICollection rules, User use .Select(t => t.Trim()) .Contains(comparisonAttributeValue.Hash())) { - logger.Log(l + "match"); + logger.Log(l + "MATCH"); return true; } @@ -299,7 +299,7 @@ private static bool TryEvaluateRules(ICollection rules, User use .Select(t => t.Trim()) .Contains(comparisonAttributeValue.Hash())) { - logger.Log(l + "match"); + logger.Log(l + "MATCH"); return true; } diff --git a/src/ConfigCatClient/Logging/ConsoleLogger.cs b/src/ConfigCatClient/Logging/ConsoleLogger.cs index d72d0f65..14a1cea6 100644 --- a/src/ConfigCatClient/Logging/ConsoleLogger.cs +++ b/src/ConfigCatClient/Logging/ConsoleLogger.cs @@ -27,30 +27,43 @@ public ConsoleLogger(LogLevel logLevel) /// public void Debug(string message) { - Console.WriteLine(FormatMessage(LogLevel.Debug, message)); + PrintMessage(LogLevel.Debug, message); } /// public void Error(string message) { - Console.WriteLine(FormatMessage(LogLevel.Error, message)); + PrintMessage(LogLevel.Error, message); } /// public void Information(string message) { - Console.WriteLine(FormatMessage(LogLevel.Info, message)); + PrintMessage(LogLevel.Info, message); } /// public void Warning(string message) { - Console.WriteLine(FormatMessage(LogLevel.Warning, message)); + PrintMessage(LogLevel.Warning, message); } - private string FormatMessage(LogLevel logLevel, string message) + private void PrintMessage(LogLevel logLevel, string message) { - return $"ConfigCat - {logLevel} - {message}"; + var originalColor = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Cyan; + Console.Write("ConfigCat"); + switch (logLevel) + { + case LogLevel.Error: Console.ForegroundColor = ConsoleColor.Red; break; + case LogLevel.Warning: Console.ForegroundColor = ConsoleColor.Yellow; break; + case LogLevel.Info: Console.ForegroundColor = ConsoleColor.Green; break; + case LogLevel.Debug: Console.ForegroundColor = ConsoleColor.Blue; break; + } + Console.Write($".{logLevel}"); + //Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine($": {message}"); + Console.ForegroundColor = originalColor; } } } diff --git a/src/ConfigCatClient/Logging/LogLevel.cs b/src/ConfigCatClient/Logging/LogLevel.cs index cfcb12f5..6246a11b 100644 --- a/src/ConfigCatClient/Logging/LogLevel.cs +++ b/src/ConfigCatClient/Logging/LogLevel.cs @@ -7,23 +7,23 @@ public enum LogLevel { /// - /// No tracing and any debugging messages. + /// No messages are logged. /// Off = 0, /// - /// Error messages. + /// Error messages are logged. All other messages are discarded. /// Error = 1, /// - /// Error and warning messages. + /// Warning and Error messages should be logged. Information and Debug messages are discarded. /// Warning = 2, /// - /// Information, Error and Warning messages. + /// Information, Warning and Error are logged. Debug messages are discarded. /// Info = 3, /// - /// All messages + /// All messages should be logged. /// Debug = 4 } From 2e64e787517e6e65f5350185e986e511c4dcc5ba Mon Sep 17 00:00:00 2001 From: Zoltan David Date: Wed, 10 Aug 2022 14:28:10 +0200 Subject: [PATCH 02/13] adds missing log messages + simplifies logger format --- src/ConfigCatClient/Evaluate/RolloutEvaluator.cs | 2 ++ src/ConfigCatClient/Logging/ConsoleLogger.cs | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs index 37cf76ca..141b517d 100644 --- a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs +++ b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs @@ -176,12 +176,14 @@ private static bool TryEvaluateRules(ICollection rules, User use if (!user.AllAttributes.ContainsKey(rule.ComparisonAttribute)) { + logger.Log($"- rule eval: ['null' {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}'] => no match"); continue; } var comparisonAttributeValue = user.AllAttributes[rule.ComparisonAttribute]; if (string.IsNullOrEmpty(comparisonAttributeValue)) { + logger.Log($"- rule eval: ['null' {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}'] => no match"); continue; } diff --git a/src/ConfigCatClient/Logging/ConsoleLogger.cs b/src/ConfigCatClient/Logging/ConsoleLogger.cs index 14a1cea6..abf50a14 100644 --- a/src/ConfigCatClient/Logging/ConsoleLogger.cs +++ b/src/ConfigCatClient/Logging/ConsoleLogger.cs @@ -52,7 +52,7 @@ private void PrintMessage(LogLevel logLevel, string message) { var originalColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Cyan; - Console.Write("ConfigCat"); + Console.Write("ConfigCat."); switch (logLevel) { case LogLevel.Error: Console.ForegroundColor = ConsoleColor.Red; break; @@ -60,9 +60,9 @@ private void PrintMessage(LogLevel logLevel, string message) case LogLevel.Info: Console.ForegroundColor = ConsoleColor.Green; break; case LogLevel.Debug: Console.ForegroundColor = ConsoleColor.Blue; break; } - Console.Write($".{logLevel}"); - //Console.ForegroundColor = ConsoleColor.Cyan; - Console.WriteLine($": {message}"); + Console.Write("{0,-7}", logLevel.ToString().ToUpper()); + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine($" {message}"); Console.ForegroundColor = originalColor; } } From 0fc6a9d73bc065cf021eedc0c68a222585d93514 Mon Sep 17 00:00:00 2001 From: Zoltan David <54935463+zoltan-david@users.noreply.github.com> Date: Wed, 10 Aug 2022 15:21:12 +0200 Subject: [PATCH 03/13] Update src/ConfigCatClient/Evaluate/RolloutEvaluator.cs Co-authored-by: Lajos Szoke <63732287+laliconfigcat@users.noreply.github.com> --- src/ConfigCatClient/Evaluate/RolloutEvaluator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs index 141b517d..bc031990 100644 --- a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs +++ b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs @@ -176,7 +176,7 @@ private static bool TryEvaluateRules(ICollection rules, User use if (!user.AllAttributes.ContainsKey(rule.ComparisonAttribute)) { - logger.Log($"- rule eval: ['null' {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}'] => no match"); + logger.Log($"- rule evaluation: ['null' {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}'] => no match"); continue; } From 439231f98fbe47da4b7cb6d3470f73a68424b770 Mon Sep 17 00:00:00 2001 From: Zoltan David <54935463+zoltan-david@users.noreply.github.com> Date: Wed, 10 Aug 2022 15:21:52 +0200 Subject: [PATCH 04/13] change "eval" to "evaluation" in log messages Co-authored-by: Lajos Szoke <63732287+laliconfigcat@users.noreply.github.com> --- src/ConfigCatClient/Evaluate/RolloutEvaluator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs index bc031990..0b83acdc 100644 --- a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs +++ b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs @@ -187,7 +187,7 @@ private static bool TryEvaluateRules(ICollection rules, User use continue; } - string l = $"- rule eval: ['{comparisonAttributeValue}' {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}'] => "; + string l = $"- rule evaluation: ['{comparisonAttributeValue}' {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}'] => "; switch (rule.Comparator) { From 17cc495a52936ed7063a853f85f8d845ca1c419a Mon Sep 17 00:00:00 2001 From: Zoltan David <54935463+zoltan-david@users.noreply.github.com> Date: Wed, 10 Aug 2022 15:23:30 +0200 Subject: [PATCH 05/13] update logging messages: change "eval" to "evaluation" Co-authored-by: Lajos Szoke <63732287+laliconfigcat@users.noreply.github.com> --- src/ConfigCatClient/Evaluate/RolloutEvaluator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs index 0b83acdc..9820bcea 100644 --- a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs +++ b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs @@ -183,7 +183,7 @@ private static bool TryEvaluateRules(ICollection rules, User use var comparisonAttributeValue = user.AllAttributes[rule.ComparisonAttribute]; if (string.IsNullOrEmpty(comparisonAttributeValue)) { - logger.Log($"- rule eval: ['null' {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}'] => no match"); + logger.Log($"- rule evaluation: ['null' {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}'] => no match"); continue; } From f602833cdca0e9e76261080855ffe11e20c83324 Mon Sep 17 00:00:00 2001 From: Zoltan David Date: Thu, 11 Aug 2022 13:22:09 +0200 Subject: [PATCH 06/13] thread safe Console.Write + more easy to understand explanations --- .../Evaluate/EvaluateLogger.cs | 7 ++-- .../Evaluate/RolloutEvaluator.cs | 35 +++++++++++-------- src/ConfigCatClient/Logging/ConsoleLogger.cs | 29 ++++++++------- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/ConfigCatClient/Evaluate/EvaluateLogger.cs b/src/ConfigCatClient/Evaluate/EvaluateLogger.cs index 10ff89d3..6aeb030f 100644 --- a/src/ConfigCatClient/Evaluate/EvaluateLogger.cs +++ b/src/ConfigCatClient/Evaluate/EvaluateLogger.cs @@ -25,8 +25,7 @@ public override string ToString() { var result = new StringBuilder(); - result.Append($"Evaluating '{KeyName}'"); - result.AppendLine($" for user '{this.User.Serialize()}'"); + result.AppendLine($"Evaluating '{KeyName}'"); foreach (var o in this.Operations) { result.AppendLine(" " + o); @@ -56,8 +55,8 @@ public static string FormatComparator(Comparator comparator) Comparator.NumberGreaterThanEqual => ">=", Comparator.NumberEqual => "=", Comparator.NumberNotEqual => "!=", - Comparator.SensitiveOneOf => "IS ONE OF (Sensitive)", - Comparator.SensitiveNotOneOf => "IS NOT ONE OF (Sensitive)", + Comparator.SensitiveOneOf => "IS ONE OF (hashed)", + Comparator.SensitiveNotOneOf => "IS NOT ONE OF (hashed)", _ => comparator.ToString() }; } diff --git a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs index 9820bcea..92fff823 100644 --- a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs +++ b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs @@ -110,7 +110,7 @@ private EvaluateResult EvaluateLogic(IDictionary settings, stri } else if (setting.RolloutRules.Any() || setting.RolloutPercentageItems.Any()) { - this.log.Warning($"Cannot evaluate targeting rules and % options for '{key}'. You should pass a UserObject to GetValue() or GetValueAsync(), in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object"); + this.log.Warning($"Cannot evaluate targeting rules and % options for '{key}' (UserObject mising). You should pass a UserObject to GetValue() or GetValueAsync(), in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object"); } // regular evaluate @@ -145,6 +145,7 @@ private static bool TryEvaluateVariations(ICollection var hashValue = hashCandidate.Hash().Substring(0, 7); var hashScale = int.Parse(hashValue, NumberStyles.HexNumber) % 100; + evaluateLog.Log($"applying the % option that matches the User's pseudo-random: {hashScale} (this value is sticky and consitent across all SDKs)"); var bucket = 0; @@ -152,10 +153,14 @@ private static bool TryEvaluateVariations(ICollection { bucket += variation.Percentage; - if (hashScale >= bucket) continue; + if (hashScale >= bucket) + { + evaluateLog.Log($"- % option: [IF {bucket} >= {hashScale} THEN '{variation.Value}'] => no match"); + continue; + } result.Value = variation.Value; result.VariationId = variation.VariationId; - evaluateLog.Log($"- % options: evaluated to '{variation.Value}'."); + evaluateLog.Log($"- % option: [IF {bucket} >= {hashScale} THEN '{variation.Value}'] => MATCH, applying % option"); return true; } } @@ -169,26 +174,26 @@ private static bool TryEvaluateRules(ICollection rules, User use if (rules is { Count: > 0 }) { + logger.Log($"applying the first targeting rule that matches the User :'{user.Serialize()}'"); foreach (var rule in rules.OrderBy(o => o.Order)) { result.Value = rule.Value; result.VariationId = rule.VariationId; + string l = $"- rule: [IF User.{rule.ComparisonAttribute} {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}' THEN {rule.Value}] => "; if (!user.AllAttributes.ContainsKey(rule.ComparisonAttribute)) { - logger.Log($"- rule evaluation: ['null' {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}'] => no match"); + logger.Log(l + "no match"); continue; } var comparisonAttributeValue = user.AllAttributes[rule.ComparisonAttribute]; if (string.IsNullOrEmpty(comparisonAttributeValue)) { - logger.Log($"- rule evaluation: ['null' {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}'] => no match"); + logger.Log(l + "no match"); continue; } - string l = $"- rule evaluation: ['{comparisonAttributeValue}' {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}'] => "; - switch (rule.Comparator) { case Comparator.In: @@ -198,7 +203,7 @@ private static bool TryEvaluateRules(ICollection rules, User use .Select(t => t.Trim()) .Contains(comparisonAttributeValue)) { - logger.Log(l + "MATCH"); + logger.Log(l + "MATCH, applying rule"); return true; } @@ -214,7 +219,7 @@ private static bool TryEvaluateRules(ICollection rules, User use .Select(t => t.Trim()) .Contains(comparisonAttributeValue)) { - logger.Log(l + "MATCH"); + logger.Log(l + "MATCH, applying rule"); return true; } @@ -226,7 +231,7 @@ private static bool TryEvaluateRules(ICollection rules, User use if (comparisonAttributeValue.Contains(rule.ComparisonValue)) { - logger.Log(l + "MATCH"); + logger.Log(l + "MATCH, applying rule"); return true; } @@ -238,7 +243,7 @@ private static bool TryEvaluateRules(ICollection rules, User use if (!comparisonAttributeValue.Contains(rule.ComparisonValue)) { - logger.Log(l + "MATCH"); + logger.Log(l + "MATCH, applying rule"); return true; } @@ -255,7 +260,7 @@ private static bool TryEvaluateRules(ICollection rules, User use if (EvaluateSemVer(comparisonAttributeValue, rule.ComparisonValue, rule.Comparator)) { - logger.Log(l + "MATCH"); + logger.Log(l + "MATCH, applying rule"); return true; } @@ -273,7 +278,7 @@ private static bool TryEvaluateRules(ICollection rules, User use if (EvaluateNumber(comparisonAttributeValue, rule.ComparisonValue, rule.Comparator)) { - logger.Log(l + "MATCH"); + logger.Log(l + "MATCH, applying rule"); return true; } @@ -287,7 +292,7 @@ private static bool TryEvaluateRules(ICollection rules, User use .Select(t => t.Trim()) .Contains(comparisonAttributeValue.Hash())) { - logger.Log(l + "MATCH"); + logger.Log(l + "MATCH, applying rule"); return true; } @@ -301,7 +306,7 @@ private static bool TryEvaluateRules(ICollection rules, User use .Select(t => t.Trim()) .Contains(comparisonAttributeValue.Hash())) { - logger.Log(l + "MATCH"); + logger.Log(l + "MATCH, applying rule"); return true; } diff --git a/src/ConfigCatClient/Logging/ConsoleLogger.cs b/src/ConfigCatClient/Logging/ConsoleLogger.cs index abf50a14..257a7f1d 100644 --- a/src/ConfigCatClient/Logging/ConsoleLogger.cs +++ b/src/ConfigCatClient/Logging/ConsoleLogger.cs @@ -7,6 +7,8 @@ namespace ConfigCat.Client /// public class ConsoleLogger : ILogger { + private object _lock = new object(); + /// public LogLevel LogLevel { get; set; } @@ -50,20 +52,23 @@ public void Warning(string message) private void PrintMessage(LogLevel logLevel, string message) { - var originalColor = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Cyan; - Console.Write("ConfigCat."); - switch (logLevel) + lock (this._lock) { - case LogLevel.Error: Console.ForegroundColor = ConsoleColor.Red; break; - case LogLevel.Warning: Console.ForegroundColor = ConsoleColor.Yellow; break; - case LogLevel.Info: Console.ForegroundColor = ConsoleColor.Green; break; - case LogLevel.Debug: Console.ForegroundColor = ConsoleColor.Blue; break; + var originalColor = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Cyan; + Console.Write("ConfigCat."); + switch (logLevel) + { + case LogLevel.Error: Console.ForegroundColor = ConsoleColor.Red; break; + case LogLevel.Warning: Console.ForegroundColor = ConsoleColor.Yellow; break; + case LogLevel.Info: Console.ForegroundColor = ConsoleColor.Green; break; + case LogLevel.Debug: Console.ForegroundColor = ConsoleColor.Blue; break; + } + Console.Write("{0,-7}", logLevel.ToString().ToUpper()); + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine($" {message}"); + Console.ForegroundColor = originalColor; } - Console.Write("{0,-7}", logLevel.ToString().ToUpper()); - Console.ForegroundColor = ConsoleColor.Cyan; - Console.WriteLine($" {message}"); - Console.ForegroundColor = originalColor; } } } From 0e9cc54668f65f819d09bc31d57c30fbdd8b7099 Mon Sep 17 00:00:00 2001 From: Zoltan David Date: Fri, 12 Aug 2022 11:41:40 +0200 Subject: [PATCH 07/13] removes lock + adds back threadsafe printing --- src/ConfigCatClient/Logging/ConsoleLogger.cs | 28 +++++++------------- 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/ConfigCatClient/Logging/ConsoleLogger.cs b/src/ConfigCatClient/Logging/ConsoleLogger.cs index 257a7f1d..103699b0 100644 --- a/src/ConfigCatClient/Logging/ConsoleLogger.cs +++ b/src/ConfigCatClient/Logging/ConsoleLogger.cs @@ -7,8 +7,6 @@ namespace ConfigCat.Client /// public class ConsoleLogger : ILogger { - private object _lock = new object(); - /// public LogLevel LogLevel { get; set; } @@ -52,23 +50,17 @@ public void Warning(string message) private void PrintMessage(LogLevel logLevel, string message) { - lock (this._lock) + const string defaultColorCode = "\u001B[36m"; + const string resetCode = "\u001B[0m"; + var logLevelColorCode = logLevel switch { - var originalColor = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Cyan; - Console.Write("ConfigCat."); - switch (logLevel) - { - case LogLevel.Error: Console.ForegroundColor = ConsoleColor.Red; break; - case LogLevel.Warning: Console.ForegroundColor = ConsoleColor.Yellow; break; - case LogLevel.Info: Console.ForegroundColor = ConsoleColor.Green; break; - case LogLevel.Debug: Console.ForegroundColor = ConsoleColor.Blue; break; - } - Console.Write("{0,-7}", logLevel.ToString().ToUpper()); - Console.ForegroundColor = ConsoleColor.Cyan; - Console.WriteLine($" {message}"); - Console.ForegroundColor = originalColor; - } + LogLevel.Error => "\u001B[31m", + LogLevel.Warning => "\u001B[33m", + LogLevel.Info => "\u001B[32m", + _ => "\u001B[32m" + }; + + Console.WriteLine($"{logLevelColorCode}CONFIGCAT {logLevel.ToString().ToUpper(),-7} {defaultColorCode}{message}{resetCode}"); } } } From 268f2ea13c31b315bce3aac78f3ada2892ee39ba Mon Sep 17 00:00:00 2001 From: Zoltan David Date: Fri, 12 Aug 2022 11:55:25 +0200 Subject: [PATCH 08/13] consistent formatting of messages --- src/ConfigCatClient/Evaluate/RolloutEvaluator.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs index 92fff823..0404dda0 100644 --- a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs +++ b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs @@ -24,7 +24,7 @@ public T Evaluate(IDictionary settings, string key, T defaul { if (settings.Count == 0) { - this.log.Error($"Config JSON is not present. Returning defaultValue: [{defaultValue}]."); + this.log.Error($"Config JSON is not present. Returning the defaultValue defined in the app source code: '{defaultValue}'."); return defaultValue; } @@ -40,7 +40,7 @@ public object Evaluate(IDictionary settings, string key, object { if (settings.Count == 0) { - this.log.Error($"Config JSON is not present. Returning defaultValue: [{defaultValue}]."); + this.log.Error($"Config JSON is not present. Returning the defaultValue defined in the app source code: '{defaultValue}'."); return defaultValue; } @@ -56,7 +56,7 @@ public string EvaluateVariationId(IDictionary settings, string { if (settings.Count == 0) { - this.log.Error($"Config JSON is not present. Returning defaultVariationId: [{defaultVariationId}]."); + this.log.Error($"Config JSON is not present. Returning defaultVariationId: '{defaultVariationId}'."); return defaultVariationId; } @@ -69,7 +69,7 @@ private EvaluateResult EvaluateLogic(IDictionary settings, stri if (!settings.TryGetValue(key, out var setting)) { var keys = string.Join(",", settings.Keys.Select(s => $"'{s}'").ToArray()); - this.log.Error($"Evaluating '{key}' failed (key not found in ConfigCat). Returning the sourcecode-level default value: '{logDefaultValue}'. Here are the available keys: {keys}."); + this.log.Error($"Evaluating '{key}' failed (key not found in ConfigCat). Returning the defaultValue defined in the app source code: '{logDefaultValue}'. Here are the available keys: {keys}."); return null; } @@ -145,7 +145,7 @@ private static bool TryEvaluateVariations(ICollection var hashValue = hashCandidate.Hash().Substring(0, 7); var hashScale = int.Parse(hashValue, NumberStyles.HexNumber) % 100; - evaluateLog.Log($"applying the % option that matches the User's pseudo-random: {hashScale} (this value is sticky and consitent across all SDKs)"); + evaluateLog.Log($"Applying the % option that matches the User's pseudo-random '{hashScale}' (this value is sticky and consitent across all SDKs):"); var bucket = 0; @@ -174,7 +174,7 @@ private static bool TryEvaluateRules(ICollection rules, User use if (rules is { Count: > 0 }) { - logger.Log($"applying the first targeting rule that matches the User :'{user.Serialize()}'"); + logger.Log($"Applying the first targeting rule that matches the User '{user.Serialize()}':"); foreach (var rule in rules.OrderBy(o => o.Order)) { result.Value = rule.Value; From 11518d920dac79839790329301f5d0480c4793ec Mon Sep 17 00:00:00 2001 From: Zoltan David Date: Fri, 12 Aug 2022 12:08:44 +0200 Subject: [PATCH 09/13] typo fix --- src/ConfigCatClient/Evaluate/RolloutEvaluator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs index 0404dda0..1ec0c34a 100644 --- a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs +++ b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs @@ -110,7 +110,7 @@ private EvaluateResult EvaluateLogic(IDictionary settings, stri } else if (setting.RolloutRules.Any() || setting.RolloutPercentageItems.Any()) { - this.log.Warning($"Cannot evaluate targeting rules and % options for '{key}' (UserObject mising). You should pass a UserObject to GetValue() or GetValueAsync(), in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object"); + this.log.Warning($"Cannot evaluate targeting rules and % options for '{key}' (UserObject missing). You should pass a UserObject to GetValue() or GetValueAsync(), in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object"); } // regular evaluate From 614702c6c4b17f6e8f7daada4e78c24907471b18 Mon Sep 17 00:00:00 2001 From: Zoltan David Date: Fri, 12 Aug 2022 13:42:45 +0200 Subject: [PATCH 10/13] important typo fix --- src/ConfigCatClient/Evaluate/RolloutEvaluator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs index 1ec0c34a..64b1c26c 100644 --- a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs +++ b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs @@ -155,12 +155,12 @@ private static bool TryEvaluateVariations(ICollection if (hashScale >= bucket) { - evaluateLog.Log($"- % option: [IF {bucket} >= {hashScale} THEN '{variation.Value}'] => no match"); + evaluateLog.Log($"- % option: [IF {bucket} > {hashScale} THEN '{variation.Value}'] => no match"); continue; } result.Value = variation.Value; result.VariationId = variation.VariationId; - evaluateLog.Log($"- % option: [IF {bucket} >= {hashScale} THEN '{variation.Value}'] => MATCH, applying % option"); + evaluateLog.Log($"- % option: [IF {bucket} > {hashScale} THEN '{variation.Value}'] => MATCH, applying % option"); return true; } } From cd6bc407f5ff02a7a9a894d61124dc3a710ef5d3 Mon Sep 17 00:00:00 2001 From: Zoltan David Date: Mon, 29 Aug 2022 16:13:55 +0200 Subject: [PATCH 11/13] log messages: removes coloring + adds spaces --- src/ConfigCatClient/Evaluate/EvaluateLogger.cs | 4 ++-- src/ConfigCatClient/Evaluate/RolloutEvaluator.cs | 6 +++--- src/ConfigCatClient/Logging/ConsoleLogger.cs | 13 ++----------- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/ConfigCatClient/Evaluate/EvaluateLogger.cs b/src/ConfigCatClient/Evaluate/EvaluateLogger.cs index 6aeb030f..236d7514 100644 --- a/src/ConfigCatClient/Evaluate/EvaluateLogger.cs +++ b/src/ConfigCatClient/Evaluate/EvaluateLogger.cs @@ -28,9 +28,9 @@ public override string ToString() result.AppendLine($"Evaluating '{KeyName}'"); foreach (var o in this.Operations) { - result.AppendLine(" " + o); + result.AppendLine(" " + o); } - result.Append($" Returning '{this.ReturnValue}' (VariationId: '{this.VariationId ?? "null"}')."); + result.Append($" Returning '{this.ReturnValue}' (VariationId: '{this.VariationId ?? "null"}')."); return result.ToString(); } diff --git a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs index 64b1c26c..eed988e2 100644 --- a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs +++ b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs @@ -155,12 +155,12 @@ private static bool TryEvaluateVariations(ICollection if (hashScale >= bucket) { - evaluateLog.Log($"- % option: [IF {bucket} > {hashScale} THEN '{variation.Value}'] => no match"); + evaluateLog.Log($" - % option: [IF {bucket} > {hashScale} THEN '{variation.Value}'] => no match"); continue; } result.Value = variation.Value; result.VariationId = variation.VariationId; - evaluateLog.Log($"- % option: [IF {bucket} > {hashScale} THEN '{variation.Value}'] => MATCH, applying % option"); + evaluateLog.Log($" - % option: [IF {bucket} > {hashScale} THEN '{variation.Value}'] => MATCH, applying % option"); return true; } } @@ -180,7 +180,7 @@ private static bool TryEvaluateRules(ICollection rules, User use result.Value = rule.Value; result.VariationId = rule.VariationId; - string l = $"- rule: [IF User.{rule.ComparisonAttribute} {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}' THEN {rule.Value}] => "; + string l = $" - rule: [IF User.{rule.ComparisonAttribute} {EvaluateLogger.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}' THEN {rule.Value}] => "; if (!user.AllAttributes.ContainsKey(rule.ComparisonAttribute)) { logger.Log(l + "no match"); diff --git a/src/ConfigCatClient/Logging/ConsoleLogger.cs b/src/ConfigCatClient/Logging/ConsoleLogger.cs index 103699b0..7b0d03dd 100644 --- a/src/ConfigCatClient/Logging/ConsoleLogger.cs +++ b/src/ConfigCatClient/Logging/ConsoleLogger.cs @@ -50,17 +50,8 @@ public void Warning(string message) private void PrintMessage(LogLevel logLevel, string message) { - const string defaultColorCode = "\u001B[36m"; - const string resetCode = "\u001B[0m"; - var logLevelColorCode = logLevel switch - { - LogLevel.Error => "\u001B[31m", - LogLevel.Warning => "\u001B[33m", - LogLevel.Info => "\u001B[32m", - _ => "\u001B[32m" - }; - - Console.WriteLine($"{logLevelColorCode}CONFIGCAT {logLevel.ToString().ToUpper(),-7} {defaultColorCode}{message}{resetCode}"); + string logLevelPadded = logLevel.ToString().ToUpper().PadRight(7); + Console.WriteLine($"ConfigCat.{logLevelPadded} {message}"); } } } From 6f6a08b8a5e16423f5785b0a2b18687fbf412d43 Mon Sep 17 00:00:00 2001 From: Zoltan David Date: Mon, 29 Aug 2022 16:50:50 +0200 Subject: [PATCH 12/13] fixes typo in the log message --- src/ConfigCatClient/Evaluate/RolloutEvaluator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs index eed988e2..c793b382 100644 --- a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs +++ b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs @@ -110,7 +110,7 @@ private EvaluateResult EvaluateLogic(IDictionary settings, stri } else if (setting.RolloutRules.Any() || setting.RolloutPercentageItems.Any()) { - this.log.Warning($"Cannot evaluate targeting rules and % options for '{key}' (UserObject missing). You should pass a UserObject to GetValue() or GetValueAsync(), in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object"); + this.log.Warning($"Cannot evaluate targeting rules and % options for '{key}' (UserObject missing). You should pass a UserObject to GetValue() or GetValueAsync() in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object"); } // regular evaluate From 9a397f828b55b8b0f9b35a897f4f5ee31caa01fe Mon Sep 17 00:00:00 2001 From: Zoltan David Date: Tue, 30 Aug 2022 17:06:27 +0200 Subject: [PATCH 13/13] typo fix + better log message --- src/ConfigCatClient/Evaluate/RolloutEvaluator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs index c793b382..330348b1 100644 --- a/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs +++ b/src/ConfigCatClient/Evaluate/RolloutEvaluator.cs @@ -69,7 +69,7 @@ private EvaluateResult EvaluateLogic(IDictionary settings, stri if (!settings.TryGetValue(key, out var setting)) { var keys = string.Join(",", settings.Keys.Select(s => $"'{s}'").ToArray()); - this.log.Error($"Evaluating '{key}' failed (key not found in ConfigCat). Returning the defaultValue defined in the app source code: '{logDefaultValue}'. Here are the available keys: {keys}."); + this.log.Error($"Evaluating '{key}' failed (key not found in ConfigCat). Returning the defaultValue that you defined in the source code: '{logDefaultValue}'. Here are the available keys: {keys}."); return null; } @@ -145,7 +145,7 @@ private static bool TryEvaluateVariations(ICollection var hashValue = hashCandidate.Hash().Substring(0, 7); var hashScale = int.Parse(hashValue, NumberStyles.HexNumber) % 100; - evaluateLog.Log($"Applying the % option that matches the User's pseudo-random '{hashScale}' (this value is sticky and consitent across all SDKs):"); + evaluateLog.Log($"Applying the % option that matches the User's pseudo-random '{hashScale}' (this value is sticky and consistent across all SDKs):"); var bucket = 0;