Skip to content

Commit 8a6bcc4

Browse files
committed
Logging must not use string interpolation and no period at end:
1. Logging not using string interpolation to support structured logging removing the needs for IsWarnEnabled and IsInfoEnabled 2. Removed period at end of log lines as by convention.
1 parent 896c506 commit 8a6bcc4

File tree

2 files changed

+17
-43
lines changed

2 files changed

+17
-43
lines changed

src/ServiceControl.Audit/Auditing/AuditIngestor.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ public async Task Ingest(List<MessageContext> contexts)
7272
Log.Debug($"Forwarding {stored.Count} messages");
7373
}
7474
await Forward(stored, logQueueAddress);
75-
if (Log.IsDebugEnabled)
76-
{
77-
Log.Debug("Forwarded messages");
78-
}
75+
Log.Debug("Forwarded messages");
7976
}
8077

8178
foreach (var context in contexts)

src/ServiceControl.Audit/Auditing/AuditPersister.cs

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task<IReadOnlyList<MessageContext>> Persist(IReadOnlyList<MessageCo
4444

4545
if (Logger.IsDebugEnabled)
4646
{
47-
Logger.Debug($"Batch size {contexts.Count}");
47+
Logger.DebugFormat("Batch size {0}", contexts.Count);
4848
}
4949

5050
var storedContexts = new List<MessageContext>(contexts.Count);
@@ -84,10 +84,7 @@ public async Task<IReadOnlyList<MessageContext>> Persist(IReadOnlyList<MessageCo
8484
RecordKnownEndpoints(receivingEndpoint, knownEndpoints, processedMessage);
8585
}
8686

87-
if (Logger.IsDebugEnabled)
88-
{
89-
Logger.Debug("Adding audit message for bulk storage");
90-
}
87+
Logger.Debug("Adding audit message for bulk storage");
9188

9289
using (auditBulkInsertDurationMeter.Measure())
9390
{
@@ -98,10 +95,7 @@ public async Task<IReadOnlyList<MessageContext>> Persist(IReadOnlyList<MessageCo
9895
}
9996
else if (context.Extensions.TryGet(out SagaSnapshot sagaSnapshot))
10097
{
101-
if (Logger.IsDebugEnabled)
102-
{
103-
Logger.Debug("Adding SagaSnapshot message for bulk storage");
104-
}
98+
Logger.Debug("Adding SagaSnapshot message for bulk storage");
10599

106100
using (sagaAuditBulkInsertDurationMeter.Measure())
107101
{
@@ -116,20 +110,14 @@ public async Task<IReadOnlyList<MessageContext>> Persist(IReadOnlyList<MessageCo
116110

117111
foreach (var endpoint in knownEndpoints.Values)
118112
{
119-
if (Logger.IsDebugEnabled)
120-
{
121-
Logger.Debug($"Adding known endpoint '{endpoint.Name}' for bulk storage");
122-
}
113+
Logger.DebugFormat("Adding known endpoint '{0}' for bulk storage", endpoint.Name);
123114

124115
await unitOfWork.RecordKnownEndpoint(endpoint);
125116
}
126117
}
127118
catch (Exception e)
128119
{
129-
if (Logger.IsWarnEnabled)
130-
{
131-
Logger.Warn("Bulk insertion failed", e);
132-
}
120+
Logger.Warn("Bulk insertion failed", e);
133121

134122
// making sure to rethrow so that all messages get marked as failed
135123
throw;
@@ -138,10 +126,7 @@ public async Task<IReadOnlyList<MessageContext>> Persist(IReadOnlyList<MessageCo
138126
{
139127
if (unitOfWork != null)
140128
{
141-
if (Logger.IsDebugEnabled)
142-
{
143-
Logger.Debug("Performing bulk session dispose");
144-
}
129+
Logger.Debug("Performing bulk session dispose");
145130

146131
try
147132
{
@@ -153,10 +138,7 @@ public async Task<IReadOnlyList<MessageContext>> Persist(IReadOnlyList<MessageCo
153138
}
154139
catch (Exception e)
155140
{
156-
if (Logger.IsWarnEnabled)
157-
{
158-
Logger.Warn("Bulk insertion dispose failed", e);
159-
}
141+
Logger.Warn("Bulk insertion dispose failed", e);
160142

161143
// making sure to rethrow so that all messages get marked as failed
162144
throw;
@@ -168,9 +150,9 @@ public async Task<IReadOnlyList<MessageContext>> Persist(IReadOnlyList<MessageCo
168150
}
169151

170152
stopwatch.Stop();
171-
if (Logger.IsDebugEnabled)
153+
if (Logger.IsDebugEnabled) // Access to stopwatch and context is not cheap
172154
{
173-
Logger.Debug($"Batch size {contexts.Count} took {stopwatch.ElapsedMilliseconds} ms");
155+
Logger.DebugFormat("Batch size {0} took {1} ms", contexts.Count, stopwatch.ElapsedMilliseconds);
174156
}
175157
}
176158

@@ -223,9 +205,9 @@ void ProcessSagaAuditMessage(MessageContext context)
223205
}
224206
catch (Exception e)
225207
{
226-
if (Logger.IsWarnEnabled)
208+
if (Logger.IsWarnEnabled) // Can't use WarnFormat due to passing exception
227209
{
228-
Logger.Warn($"Processing of saga audit message '{context.NativeMessageId}' failed.", e);
210+
Logger.Warn($"Processing of saga audit message '{context.NativeMessageId}' failed", e);
229211
}
230212

231213
// releasing the failed message context early so that they can be retried outside the current batch
@@ -262,10 +244,8 @@ async Task ProcessAuditMessage(MessageContext context)
262244

263245
var auditMessage = new ProcessedMessage(context.Headers, new Dictionary<string, object>(metadata));
264246

265-
if (Logger.IsDebugEnabled)
266-
{
267-
Logger.Debug($"Emitting {commandsToEmit.Count} commands and {messagesToEmit.Count} control messages.");
268-
}
247+
Logger.DebugFormat("Emitting {0} commands and {1} control messages", commandsToEmit.Count, messagesToEmit.Count);
248+
269249
foreach (var commandToEmit in commandsToEmit)
270250
{
271251
await messageSession.Send(commandToEmit);
@@ -274,10 +254,7 @@ async Task ProcessAuditMessage(MessageContext context)
274254
await messageDispatcher.Value.Dispatch(new TransportOperations(messagesToEmit.ToArray()),
275255
new TransportTransaction()); //Do not hook into the incoming transaction
276256

277-
if (Logger.IsDebugEnabled)
278-
{
279-
Logger.Debug($"{commandsToEmit.Count} commands and {messagesToEmit.Count} control messages emitted.");
280-
}
257+
Logger.DebugFormat("{0} commands and {1} control messages emitted", commandsToEmit.Count, messagesToEmit.Count);
281258

282259
if (metadata.TryGetValue("SendingEndpoint", out var sendingEndpoint))
283260
{
@@ -294,9 +271,9 @@ await messageDispatcher.Value.Dispatch(new TransportOperations(messagesToEmit.To
294271
}
295272
catch (Exception e)
296273
{
297-
if (Logger.IsWarnEnabled)
274+
if (Logger.IsWarnEnabled) // Can't use WarnFormat due to passing exception
298275
{
299-
Logger.Warn($"Processing of message '{messageId}' failed.", e);
276+
Logger.Warn($"Processing of message '{messageId}' failed", e);
300277
}
301278

302279
// releasing the failed message context early so that they can be retried outside the current batch

0 commit comments

Comments
 (0)