Skip to content

Commit 3c236a4

Browse files
Meir017mgravell
andauthored
chore: Replaces string interpolation with structured logging - ResultProcessor (#2925)
* Migrates logging to structured LoggerMessage methods Replaces manual string interpolation with compile-time generated LoggerMessage methods for better performance and structured logging. Adds 16 new LoggerMessage extension methods covering response logging and auto-configuration scenarios for Redis server connections. Improves logging efficiency by eliminating runtime string formatting overhead and enables better log analysis through consistent message templates. * merge read-only-replica methods --------- Co-authored-by: Marc Gravell <[email protected]>
1 parent 2f52c95 commit 3c236a4

File tree

2 files changed

+108
-17
lines changed

2 files changed

+108
-17
lines changed

src/StackExchange.Redis/LoggerExtensions.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,4 +488,95 @@ internal static void LogWithThreadPoolStats(this ILogger? log, string message)
488488
EventId = 70,
489489
Message = "{Server}: Flushing outbound buffer")]
490490
internal static partial void LogInformationFlushingOutboundBuffer(this ILogger logger, ServerEndPointLogValue server);
491+
492+
// ResultProcessor logging methods
493+
[LoggerMessage(
494+
Level = LogLevel.Information,
495+
EventId = 71,
496+
Message = "Response from {BridgeName} / {CommandAndKey}: {Result}")]
497+
internal static partial void LogInformationResponse(this ILogger logger, string? bridgeName, string commandAndKey, RawResult result);
498+
499+
[LoggerMessage(
500+
Level = LogLevel.Information,
501+
EventId = 72,
502+
Message = "{Server}: Auto-configured role: replica")]
503+
internal static partial void LogInformationAutoConfiguredRoleReplica(this ILogger logger, ServerEndPointLogValue server);
504+
505+
[LoggerMessage(
506+
Level = LogLevel.Information,
507+
EventId = 73,
508+
Message = "{Server}: Auto-configured (CLIENT) connection-id: {ConnectionId}")]
509+
internal static partial void LogInformationAutoConfiguredClientConnectionId(this ILogger logger, ServerEndPointLogValue server, long connectionId);
510+
511+
[LoggerMessage(
512+
Level = LogLevel.Information,
513+
EventId = 74,
514+
Message = "{Server}: Auto-configured (INFO) role: {Role}")]
515+
internal static partial void LogInformationAutoConfiguredInfoRole(this ILogger logger, ServerEndPointLogValue server, string role);
516+
517+
[LoggerMessage(
518+
Level = LogLevel.Information,
519+
EventId = 75,
520+
Message = "{Server}: Auto-configured (INFO) version: {Version}")]
521+
internal static partial void LogInformationAutoConfiguredInfoVersion(this ILogger logger, ServerEndPointLogValue server, Version version);
522+
523+
[LoggerMessage(
524+
Level = LogLevel.Information,
525+
EventId = 76,
526+
Message = "{Server}: Auto-configured (INFO) server-type: {ServerType}")]
527+
internal static partial void LogInformationAutoConfiguredInfoServerType(this ILogger logger, ServerEndPointLogValue server, ServerType serverType);
528+
529+
[LoggerMessage(
530+
Level = LogLevel.Information,
531+
EventId = 77,
532+
Message = "{Server}: Auto-configured (SENTINEL) server-type: sentinel")]
533+
internal static partial void LogInformationAutoConfiguredSentinelServerType(this ILogger logger, ServerEndPointLogValue server);
534+
535+
[LoggerMessage(
536+
Level = LogLevel.Information,
537+
EventId = 78,
538+
Message = "{Server}: Auto-configured (CONFIG) timeout: {TimeoutSeconds}s")]
539+
internal static partial void LogInformationAutoConfiguredConfigTimeout(this ILogger logger, ServerEndPointLogValue server, int timeoutSeconds);
540+
541+
[LoggerMessage(
542+
Level = LogLevel.Information,
543+
EventId = 79,
544+
Message = "{Server}: Auto-configured (CONFIG) databases: {DatabaseCount}")]
545+
internal static partial void LogInformationAutoConfiguredConfigDatabases(this ILogger logger, ServerEndPointLogValue server, int databaseCount);
546+
547+
[LoggerMessage(
548+
Level = LogLevel.Information,
549+
EventId = 81,
550+
Message = "{Server}: Auto-configured (CONFIG) read-only replica: {ReadOnlyReplica}")]
551+
internal static partial void LogInformationAutoConfiguredConfigReadOnlyReplica(this ILogger logger, ServerEndPointLogValue server, bool readOnlyReplica);
552+
553+
[LoggerMessage(
554+
Level = LogLevel.Information,
555+
EventId = 82,
556+
Message = "{Server}: Auto-configured (HELLO) server-version: {Version}")]
557+
internal static partial void LogInformationAutoConfiguredHelloServerVersion(this ILogger logger, ServerEndPointLogValue server, Version version);
558+
559+
[LoggerMessage(
560+
Level = LogLevel.Information,
561+
EventId = 83,
562+
Message = "{Server}: Auto-configured (HELLO) protocol: {Protocol}")]
563+
internal static partial void LogInformationAutoConfiguredHelloProtocol(this ILogger logger, ServerEndPointLogValue server, RedisProtocol protocol);
564+
565+
[LoggerMessage(
566+
Level = LogLevel.Information,
567+
EventId = 84,
568+
Message = "{Server}: Auto-configured (HELLO) connection-id: {ConnectionId}")]
569+
internal static partial void LogInformationAutoConfiguredHelloConnectionId(this ILogger logger, ServerEndPointLogValue server, long connectionId);
570+
571+
[LoggerMessage(
572+
Level = LogLevel.Information,
573+
EventId = 85,
574+
Message = "{Server}: Auto-configured (HELLO) server-type: {ServerType}")]
575+
internal static partial void LogInformationAutoConfiguredHelloServerType(this ILogger logger, ServerEndPointLogValue server, ServerType serverType);
576+
577+
[LoggerMessage(
578+
Level = LogLevel.Information,
579+
EventId = 86,
580+
Message = "{Server}: Auto-configured (HELLO) role: {Role}")]
581+
internal static partial void LogInformationAutoConfiguredHelloRole(this ILogger logger, ServerEndPointLogValue server, string role);
491582
}

src/StackExchange.Redis/ResultProcessor.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public virtual bool SetResult(PhysicalConnection connection, Message message, in
233233
{
234234
try
235235
{
236-
logging.Log?.LogInformation($"Response from {bridge?.Name} / {message.CommandAndKey}: {result}");
236+
logging.Log?.LogInformationResponse(bridge?.Name, message.CommandAndKey, result);
237237
}
238238
catch { }
239239
}
@@ -816,7 +816,7 @@ public override bool SetResult(PhysicalConnection connection, Message message, i
816816
if (bridge != null)
817817
{
818818
var server = bridge.ServerEndPoint;
819-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured role: replica");
819+
Log?.LogInformationAutoConfiguredRoleReplica(new(server));
820820
server.IsReplica = true;
821821
}
822822
}
@@ -837,7 +837,7 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
837837
if (result.TryGetInt64(out long clientId))
838838
{
839839
connection.ConnectionId = clientId;
840-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (CLIENT) connection-id: {clientId}");
840+
Log?.LogInformationAutoConfiguredClientConnectionId(new(server), clientId);
841841

842842
SetResult(message, true);
843843
return true;
@@ -871,7 +871,7 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
871871
if (TryParseRole(val, out bool isReplica))
872872
{
873873
server.IsReplica = isReplica;
874-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (INFO) role: {(isReplica ? "replica" : "primary")}");
874+
Log?.LogInformationAutoConfiguredInfoRole(new(server), isReplica ? "replica" : "primary");
875875
}
876876
}
877877
else if ((val = Extract(line, "master_host:")) != null)
@@ -887,15 +887,15 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
887887
if (Format.TryParseVersion(val, out Version? version))
888888
{
889889
server.Version = version;
890-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (INFO) version: " + version);
890+
Log?.LogInformationAutoConfiguredInfoVersion(new(server), version);
891891
}
892892
}
893893
else if ((val = Extract(line, "redis_mode:")) != null)
894894
{
895895
if (TryParseServerType(val, out var serverType))
896896
{
897897
server.ServerType = serverType;
898-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (INFO) server-type: {serverType}");
898+
Log?.LogInformationAutoConfiguredInfoServerType(new(server), serverType);
899899
}
900900
}
901901
else if ((val = Extract(line, "run_id:")) != null)
@@ -913,7 +913,7 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
913913
else if (message?.Command == RedisCommand.SENTINEL)
914914
{
915915
server.ServerType = ServerType.Sentinel;
916-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (SENTINEL) server-type: sentinel");
916+
Log?.LogInformationAutoConfiguredSentinelServerType(new(server));
917917
}
918918
SetResult(message, true);
919919
return true;
@@ -941,14 +941,14 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
941941
{
942942
targetSeconds = (timeoutSeconds * 3) / 4;
943943
}
944-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (CONFIG) timeout: " + targetSeconds + "s");
944+
Log?.LogInformationAutoConfiguredConfigTimeout(new(server), targetSeconds);
945945
server.WriteEverySeconds = targetSeconds;
946946
}
947947
}
948948
else if (key.IsEqual(CommonReplies.databases) && val.TryGetInt64(out i64))
949949
{
950950
int dbCount = checked((int)i64);
951-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (CONFIG) databases: " + dbCount);
951+
Log?.LogInformationAutoConfiguredConfigDatabases(new(server), dbCount);
952952
server.Databases = dbCount;
953953
if (dbCount > 1)
954954
{
@@ -960,12 +960,12 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
960960
if (val.IsEqual(CommonReplies.yes))
961961
{
962962
server.ReplicaReadOnly = true;
963-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (CONFIG) read-only replica: true");
963+
Log?.LogInformationAutoConfiguredConfigReadOnlyReplica(new(server), true);
964964
}
965965
else if (val.IsEqual(CommonReplies.no))
966966
{
967967
server.ReplicaReadOnly = false;
968-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (CONFIG) read-only replica: false");
968+
Log?.LogInformationAutoConfiguredConfigReadOnlyReplica(new(server), false);
969969
}
970970
}
971971
}
@@ -982,34 +982,34 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
982982
if (key.IsEqual(CommonReplies.version) && Format.TryParseVersion(val.GetString(), out var version))
983983
{
984984
server.Version = version;
985-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (HELLO) server-version: {version}");
985+
Log?.LogInformationAutoConfiguredHelloServerVersion(new(server), version);
986986
}
987987
else if (key.IsEqual(CommonReplies.proto) && val.TryGetInt64(out var i64))
988988
{
989989
connection.SetProtocol(i64 >= 3 ? RedisProtocol.Resp3 : RedisProtocol.Resp2);
990-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (HELLO) protocol: {connection.Protocol}");
990+
Log?.LogInformationAutoConfiguredHelloProtocol(new(server), connection.Protocol ?? RedisProtocol.Resp2);
991991
}
992992
else if (key.IsEqual(CommonReplies.id) && val.TryGetInt64(out i64))
993993
{
994994
connection.ConnectionId = i64;
995-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (HELLO) connection-id: {i64}");
995+
Log?.LogInformationAutoConfiguredHelloConnectionId(new(server), i64);
996996
}
997997
else if (key.IsEqual(CommonReplies.mode) && TryParseServerType(val.GetString(), out var serverType))
998998
{
999999
server.ServerType = serverType;
1000-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (HELLO) server-type: {serverType}");
1000+
Log?.LogInformationAutoConfiguredHelloServerType(new(server), serverType);
10011001
}
10021002
else if (key.IsEqual(CommonReplies.role) && TryParseRole(val.GetString(), out bool isReplica))
10031003
{
10041004
server.IsReplica = isReplica;
1005-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (HELLO) role: {(isReplica ? "replica" : "primary")}");
1005+
Log?.LogInformationAutoConfiguredHelloRole(new(server), isReplica ? "replica" : "primary");
10061006
}
10071007
}
10081008
}
10091009
else if (message?.Command == RedisCommand.SENTINEL)
10101010
{
10111011
server.ServerType = ServerType.Sentinel;
1012-
Log?.LogInformation($"{Format.ToString(server)}: Auto-configured (SENTINEL) server-type: sentinel");
1012+
Log?.LogInformationAutoConfiguredSentinelServerType(new(server));
10131013
}
10141014
SetResult(message, true);
10151015
return true;

0 commit comments

Comments
 (0)