From 9e93e99025d898243a355db4060e6d251401e32d Mon Sep 17 00:00:00 2001 From: Adam Simon Date: Thu, 22 Aug 2024 17:47:52 +0200 Subject: [PATCH] Include Ray ID in log messages only when available --- .../BasicConfigCatClientIntegrationTests.cs | 8 +-- src/ConfigCatClient/Logging/LogMessages.cs | 70 ++++++++++++------- 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/ConfigCat.Client.Tests/BasicConfigCatClientIntegrationTests.cs b/src/ConfigCat.Client.Tests/BasicConfigCatClientIntegrationTests.cs index 50f88840..6fba615d 100644 --- a/src/ConfigCat.Client.Tests/BasicConfigCatClientIntegrationTests.cs +++ b/src/ConfigCat.Client.Tests/BasicConfigCatClientIntegrationTests.cs @@ -493,11 +493,11 @@ public async Task ShouldIncludeRayIdInLogMessagesWhenHttpResponseIsNotSuccessful var errors = logEvents.Where(evt => evt.EventId == 1100).ToArray(); Assert.AreEqual(1, errors.Length); - var rayId = errors[0].Message.ArgValues[0] as string; - Assert.IsNotNull(rayId); - Assert.AreNotEqual("", rayId); - Assert.AreNotEqual(LoggerExtensions.FormatRayId(null), rayId); + var error = errors[0].Message; + Assert.AreEqual(1, error.ArgValues.Length); + Assert.IsTrue(error.ArgValues[0] is string); + var rayId = (string)error.ArgValues[0]!; StringAssert.Contains(errors[0].Message.InvariantFormattedMessage, rayId); } } diff --git a/src/ConfigCatClient/Logging/LogMessages.cs b/src/ConfigCatClient/Logging/LogMessages.cs index 12a6fbdb..0b8f67cf 100644 --- a/src/ConfigCatClient/Logging/LogMessages.cs +++ b/src/ConfigCatClient/Logging/LogMessages.cs @@ -38,15 +38,24 @@ public static FormattableLogMessage ForceRefreshError(this LoggerWrapper logger, $"Error occurred in the `{methodName}` method.", "METHOD_NAME"); - public static FormattableLogMessage FetchFailedDueToInvalidSdkKey(this LoggerWrapper logger, string? rayId) => logger.LogInterpolated( - LogLevel.Error, 1100, - $"Your SDK Key seems to be wrong. You can find the valid SDK Key at https://app.configcat.com/sdkkey (Ray ID: {FormatRayId(rayId)})", - "RAY_ID"); - - public static FormattableLogMessage FetchFailedDueToUnexpectedHttpResponse(this LoggerWrapper logger, int statusCode, string? reasonPhrase, string? rayId) => logger.LogInterpolated( - LogLevel.Error, 1101, - $"Unexpected HTTP response was received while trying to fetch config JSON: {statusCode} {reasonPhrase} (Ray ID: {FormatRayId(rayId)})", - "STATUS_CODE", "REASON_PHRASE", "RAY_ID"); + public static FormattableLogMessage FetchFailedDueToInvalidSdkKey(this LoggerWrapper logger, string? rayId) => rayId is not null + ? logger.LogInterpolated( + LogLevel.Error, 1100, + $"Your SDK Key seems to be wrong. You can find the valid SDK Key at https://app.configcat.com/sdkkey (Ray ID: {rayId})", + "RAY_ID") + : logger.Log( + LogLevel.Error, 1100, + "Your SDK Key seems to be wrong. You can find the valid SDK Key at https://app.configcat.com/sdkkey"); + + public static FormattableLogMessage FetchFailedDueToUnexpectedHttpResponse(this LoggerWrapper logger, int statusCode, string? reasonPhrase, string? rayId) => rayId is not null + ? logger.LogInterpolated( + LogLevel.Error, 1101, + $"Unexpected HTTP response was received while trying to fetch config JSON: {statusCode} {reasonPhrase} (Ray ID: {rayId})", + "STATUS_CODE", "REASON_PHRASE", "RAY_ID") + : logger.LogInterpolated( + LogLevel.Error, 1101, + $"Unexpected HTTP response was received while trying to fetch config JSON: {statusCode} {reasonPhrase}", + "STATUS_CODE", "REASON_PHRASE"); public static FormattableLogMessage FetchFailedDueToRequestTimeout(this LoggerWrapper logger, TimeSpan timeout, Exception ex) => logger.LogInterpolated( LogLevel.Error, 1102, ex, @@ -57,20 +66,33 @@ public static FormattableLogMessage FetchFailedDueToUnexpectedError(this LoggerW LogLevel.Error, 1103, ex, "Unexpected error occurred while trying to fetch config JSON. It is most likely due to a local network issue. Please make sure your application can reach the ConfigCat CDN servers (or your proxy server) over HTTP."); - public static FormattableLogMessage FetchFailedDueToRedirectLoop(this LoggerWrapper logger, string? rayId) => logger.LogInterpolated( - LogLevel.Error, 1104, - $"Redirection loop encountered while trying to fetch config JSON. Please contact us at https://configcat.com/support/ (Ray ID: {FormatRayId(rayId)})", - "RAY_ID"); - - public static FormattableLogMessage FetchReceived200WithInvalidBody(this LoggerWrapper logger, string? rayId, Exception? ex) => logger.LogInterpolated( - LogLevel.Error, 1105, ex, - $"Fetching config JSON was successful but the HTTP response content was invalid. (Ray ID: {FormatRayId(rayId)})", - "RAY_ID"); - - public static FormattableLogMessage FetchReceived304WhenLocalCacheIsEmpty(this LoggerWrapper logger, int statusCode, string? reasonPhrase, string? rayId) => logger.LogInterpolated( - LogLevel.Error, 1106, - $"Unexpected HTTP response was received when no config JSON is cached locally: {statusCode} {reasonPhrase} (Ray ID: {FormatRayId(rayId)})", - "STATUS_CODE", "REASON_PHRASE", "RAY_ID"); + public static FormattableLogMessage FetchFailedDueToRedirectLoop(this LoggerWrapper logger, string? rayId) => rayId is not null + ? logger.LogInterpolated( + LogLevel.Error, 1104, + $"Redirection loop encountered while trying to fetch config JSON. Please contact us at https://configcat.com/support/ (Ray ID: {rayId})", + "RAY_ID") + : logger.Log( + LogLevel.Error, 1104, + "Redirection loop encountered while trying to fetch config JSON. Please contact us at https://configcat.com/support/"); + + public static FormattableLogMessage FetchReceived200WithInvalidBody(this LoggerWrapper logger, string? rayId, Exception? ex) => rayId is not null + ? logger.LogInterpolated( + LogLevel.Error, 1105, ex, + $"Fetching config JSON was successful but the HTTP response content was invalid. (Ray ID: {rayId})", + "RAY_ID") + : logger.Log( + LogLevel.Error, 1105, ex, + "Fetching config JSON was successful but the HTTP response content was invalid."); + + public static FormattableLogMessage FetchReceived304WhenLocalCacheIsEmpty(this LoggerWrapper logger, int statusCode, string? reasonPhrase, string? rayId) => rayId is not null + ? logger.LogInterpolated( + LogLevel.Error, 1106, + $"Unexpected HTTP response was received when no config JSON is cached locally: {statusCode} {reasonPhrase} (Ray ID: {rayId})", + "STATUS_CODE", "REASON_PHRASE", "RAY_ID") + : logger.LogInterpolated( + LogLevel.Error, 1106, + $"Unexpected HTTP response was received when no config JSON is cached locally: {statusCode} {reasonPhrase}", + "STATUS_CODE", "REASON_PHRASE"); public static FormattableLogMessage AutoPollConfigServiceErrorDuringPolling(this LoggerWrapper logger, Exception ex) => logger.Log( LogLevel.Error, 1200, ex, @@ -197,6 +219,4 @@ public static FormattableLogMessage LocalFileDataSourceReloadsFile(this LoggerWr #region SDK-specific info messages (6000-6999) #endregion - - internal static string FormatRayId(string? value) => value ?? "n/a"; }