Skip to content

Commit

Permalink
Include Ray ID in log messages only when available
Browse files Browse the repository at this point in the history
  • Loading branch information
adams85 committed Aug 22, 2024
1 parent 28fc0af commit 9e93e99
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
70 changes: 45 additions & 25 deletions src/ConfigCatClient/Logging/LogMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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";
}

0 comments on commit 9e93e99

Please sign in to comment.