From 1c06149fd85a9d0581c85eac569082a4158a5cb7 Mon Sep 17 00:00:00 2001 From: Chris/0 Date: Tue, 27 Jul 2021 14:23:09 -0400 Subject: [PATCH] Use Structured Logging in a More Structured Way In several places, empty curly braces (`{}`) were used in place of structured logging properties. These were processed as literal curly braces, and log context was lost. Where the value of the property was an exception, an Exception-specialized overload was chosen instead. This did not alter any places where string interpolation was used instead of structured logging properties. That could be work for the future. --- src/Amazon.CloudWatch.EMF/Environment/ECSEnvironment.cs | 2 +- src/Amazon.CloudWatch.EMF/Logger/MetricsLogger.cs | 2 +- src/Amazon.CloudWatch.EMF/Sink/AgentSink.cs | 6 +++--- src/Amazon.CloudWatch.EMF/Sink/ConsoleSink.cs | 2 +- src/Amazon.CloudWatch.EMF/Sink/Endpoint.cs | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Amazon.CloudWatch.EMF/Environment/ECSEnvironment.cs b/src/Amazon.CloudWatch.EMF/Environment/ECSEnvironment.cs index 94cab85..70bda8b 100644 --- a/src/Amazon.CloudWatch.EMF/Environment/ECSEnvironment.cs +++ b/src/Amazon.CloudWatch.EMF/Environment/ECSEnvironment.cs @@ -151,7 +151,7 @@ private void CheckAndSetFluentHost() _fluentBitEndpoint = string.Format("tcp://%s:%d", fluentHost, Constants.DEFAULT_AGENT_PORT); _configuration.AgentEndPoint = _fluentBitEndpoint; - _logger.LogInformation("Using FluentBit configuration. Endpoint: {}", _fluentBitEndpoint); + _logger.LogInformation("Using FluentBit configuration. Endpoint: {Endpoint}", _fluentBitEndpoint); } private void FormatImageName() diff --git a/src/Amazon.CloudWatch.EMF/Logger/MetricsLogger.cs b/src/Amazon.CloudWatch.EMF/Logger/MetricsLogger.cs index f021f7b..ec1222c 100644 --- a/src/Amazon.CloudWatch.EMF/Logger/MetricsLogger.cs +++ b/src/Amazon.CloudWatch.EMF/Logger/MetricsLogger.cs @@ -82,7 +82,7 @@ public void Flush() throw new InvalidOperationException(message); } - _logger.LogDebug("Sending data to sink. {}", _environment.Sink.GetType().Name); + _logger.LogDebug("Sending data to sink: {SinkName}", _environment.Sink.GetType().Name); _environment.Sink.Accept(_context); _context = _context.CreateCopyWithContext(); diff --git a/src/Amazon.CloudWatch.EMF/Sink/AgentSink.cs b/src/Amazon.CloudWatch.EMF/Sink/AgentSink.cs index 29741e1..9d58847 100644 --- a/src/Amazon.CloudWatch.EMF/Sink/AgentSink.cs +++ b/src/Amazon.CloudWatch.EMF/Sink/AgentSink.cs @@ -68,7 +68,7 @@ public void Accept(MetricsContext metricsContext) } catch (InvalidOperationException e) { - _logger.LogError("Attempted to publish data after the sink has been shutdown. {}", e); + _logger.LogError(e, "Attempted to publish data after the sink has been shutdown."); } _logger.LogDebug("Data queued successfully."); @@ -76,7 +76,7 @@ public void Accept(MetricsContext metricsContext) } catch (Exception e) { - _logger.LogError("Failed to serialize the metrics with the exception: {}", e); + _logger.LogError(e, "Failed to serialize the metrics."); } } @@ -118,7 +118,7 @@ private Task RunSenderThread(ILoggerFactory loggerFactory) } catch (Exception e) { - logger.LogWarning("Failed to write message to socket. Backing off and trying again. {}", e.Message); + logger.LogWarning(e, "Failed to write message to socket. Backing off and trying again."); Thread.Sleep(1000); // TODO: backoff } } diff --git a/src/Amazon.CloudWatch.EMF/Sink/ConsoleSink.cs b/src/Amazon.CloudWatch.EMF/Sink/ConsoleSink.cs index 1caa835..107d6a9 100644 --- a/src/Amazon.CloudWatch.EMF/Sink/ConsoleSink.cs +++ b/src/Amazon.CloudWatch.EMF/Sink/ConsoleSink.cs @@ -35,7 +35,7 @@ public void Accept(MetricsContext context) } catch (Exception e) { - _logger.LogError("Failed to serialize a MetricsContext: {}", e); + _logger.LogError(e, "Failed to serialize a MetricsContext."); } } diff --git a/src/Amazon.CloudWatch.EMF/Sink/Endpoint.cs b/src/Amazon.CloudWatch.EMF/Sink/Endpoint.cs index 09d9f35..f99fd3e 100644 --- a/src/Amazon.CloudWatch.EMF/Sink/Endpoint.cs +++ b/src/Amazon.CloudWatch.EMF/Sink/Endpoint.cs @@ -31,7 +31,7 @@ public Endpoint(string url, ILoggerFactory loggerFactory) } catch (UriFormatException) { - logger.LogWarning("Failed to parse the endpoint: {} ", url); + logger.LogWarning("Failed to parse the endpoint: {Url} ", url); SetDefault(); } @@ -51,7 +51,7 @@ public Endpoint(string url, ILoggerFactory loggerFactory) catch (Exception) { logger.LogWarning( - "Unsupported protocol: {}. Would use default endpoint: {}", + "Unsupported protocol: {Url}. Would use default endpoint: {Endpoint}", url, DEFAULT_TCP_ENDPOINT);