Skip to content

Commit ce450db

Browse files
committed
Add correlation id to track log messages for a change
1 parent e245d5a commit ce450db

5 files changed

Lines changed: 46 additions & 26 deletions

File tree

src/Configuration/Action.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,14 @@ public override void Run(ChangeInfo change, TriggerType trigger)
100100
if (Change != null)
101101
{
102102
Logger.WriteLine(
103-
$"The source file could not be determined. Watch path: {Change.WatchPath}, changed: {Change.FullPath}.",
103+
$"[{Change.CorrelationId}] The source file could not be determined. Watch path: {Change.WatchPath}, changed: {Change.FullPath}.",
104104
LogLevel.ERROR);
105105
}
106106
return;
107107
}
108108

109+
string correlationPrefix = Change != null ? $"[{Change.CorrelationId}] " : "";
110+
109111
try
110112
{
111113
switch (Type)
@@ -114,31 +116,31 @@ public override void Run(ChangeInfo change, TriggerType trigger)
114116
if (string.IsNullOrWhiteSpace(destination))
115117
{
116118
Logger.WriteLine(
117-
$"The file '{source}' could not be copied because the destination file could not be determined. Destination in config file: {Destination}.",
119+
$"{correlationPrefix}The file '{source}' could not be copied because the destination file could not be determined. Destination in config file: {Destination}.",
118120
LogLevel.ERROR);
119121
return;
120122
}
121123

122124
TEFS.File.Copy(source, destination, Verify, KeepTimestamps);
123-
Logger.WriteLine($"Copied {source} to {destination}. Verify: {Verify}. Keep timestamps: {KeepTimestamps}.");
125+
Logger.WriteLine($"{correlationPrefix}Copied {source} to {destination}. Verify: {Verify}. Keep timestamps: {KeepTimestamps}.");
124126
break;
125127

126128
case ActionType.Move:
127129
if (string.IsNullOrWhiteSpace(destination))
128130
{
129131
Logger.WriteLine(
130-
$"The file '{source}' could not be moved because the destination file could not be determined. Destination in config file: {Destination}.",
132+
$"{correlationPrefix}The file '{source}' could not be moved because the destination file could not be determined. Destination in config file: {Destination}.",
131133
LogLevel.ERROR);
132134
return;
133135
}
134136

135137
TEFS.File.Move(source, destination, Verify, KeepTimestamps);
136-
Logger.WriteLine($"Moved {source} to {destination}. Verify: {Verify}. Keep timestamps: {KeepTimestamps}.");
138+
Logger.WriteLine($"{correlationPrefix}Moved {source} to {destination}. Verify: {Verify}. Keep timestamps: {KeepTimestamps}.");
137139
break;
138140

139141
case ActionType.Delete:
140142
TEFS.File.Delete(source);
141-
Logger.WriteLine($"Deleted {source}.");
143+
Logger.WriteLine($"{correlationPrefix}Deleted {source}.");
142144
break;
143145
}
144146
}
@@ -147,7 +149,7 @@ public override void Run(ChangeInfo change, TriggerType trigger)
147149
{
148150
Exception exception = ex.InnerException ?? ex;
149151
Logger.WriteLine(
150-
$"Could not {Type.ToString().ToLower(System.Globalization.CultureInfo.CurrentCulture)} file '{source}.' Reason: {exception.Message}",
152+
$"{correlationPrefix}Could not {Type.ToString().ToLower(System.Globalization.CultureInfo.CurrentCulture)} file '{source}.' Reason: {exception.Message}",
151153
LogLevel.ERROR);
152154
if (ex.StackTrace != null)
153155
{

src/Configuration/ChangeInfo.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
/// </summary>
66
public class ChangeInfo
77
{
8+
/// <summary>
9+
/// Gets the unique correlation ID for this change event.
10+
/// This ID can be used to trace all operations related to this change in logs.
11+
/// </summary>
12+
public Guid CorrelationId { get; private set; }
13+
814
/// <summary>
915
/// Gets the trigger for the change.
1016
/// </summary>
@@ -37,11 +43,6 @@ public class ChangeInfo
3743
/// </summary>
3844
public string WatchPath { get; private set; }
3945

40-
/// <summary>
41-
/// Initializes an instance of the <see cref="ChangeInfo"/>.
42-
/// </summary>
43-
//public ChangeInfo() { }
44-
4546
/// <summary>
4647
/// Initializes an instance of the <see cref="ChangeInfo"/> class when
4748
/// provided with the trigger, the file/folder name, and the full path
@@ -50,6 +51,9 @@ public class ChangeInfo
5051
/// <param name="trigger">
5152
/// The type of change.
5253
/// </param>
54+
/// <param name="watchPath">
55+
/// The path being watched.
56+
/// </param>
5357
/// <param name="name">
5458
/// The name of the file or folder.
5559
/// </param>
@@ -67,6 +71,7 @@ public class ChangeInfo
6771
/// </exception>
6872
public ChangeInfo(TriggerType trigger, string watchPath, string name, string fullPath, string? oldName, string? oldPath)
6973
{
74+
CorrelationId = Guid.NewGuid();
7075
Trigger = trigger;
7176
WatchPath = watchPath ?? throw new ArgumentNullException(nameof(watchPath));
7277
Name = name ?? throw new ArgumentNullException(nameof(name));

src/Configuration/Command.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,19 @@ public override void Run(ChangeInfo change, TriggerType trigger)
7575
string? arguments = GetArguments();
7676
string? workingDirectory = GetWorkingDirectory();
7777

78+
string correlationPrefix = Change != null ? $"[{Change.CorrelationId}] " : "";
79+
7880
if (string.IsNullOrWhiteSpace(commandPath))
7981
{
80-
Logger.WriteLine($"The command was not provided. Command was not run.",
82+
Logger.WriteLine($"{correlationPrefix}The command was not provided. Command was not run.",
8183
LogLevel.ERROR);
8284
return;
8385
}
8486

8587
if (!File.Exists(commandPath))
8688
{
8789
Logger.WriteLine(
88-
$"The command '{commandPath}' was not found. Command was not run.",
90+
$"{correlationPrefix}The command '{commandPath}' was not found. Command was not run.",
8991
LogLevel.ERROR);
9092
return;
9193
}
@@ -111,11 +113,14 @@ public override void Run(ChangeInfo change, TriggerType trigger)
111113
_processInfo.Enqueue(startInfo);
112114

113115
Logger.WriteLine(
114-
$"Queue command: {startInfo.FileName} {startInfo.Arguments}. Queue Length: {_processInfo.Count}. (Command.Run)",
116+
$"{correlationPrefix}Queue command: {startInfo.FileName} {startInfo.Arguments}. Queue Length: {_processInfo.Count}. (Command.Run)",
115117
LogLevel.DEBUG);
116118

117119
// Execute the next process in the queue
118-
Execute();
120+
if (Change != null)
121+
{
122+
Execute(Change.CorrelationId);
123+
}
119124
}
120125

121126
/// <summary>
@@ -151,9 +156,10 @@ protected virtual void Dispose(bool disposing)
151156
/// <summary>
152157
/// Executes the next command process from the queue.
153158
/// </summary>
154-
private void Execute()
159+
/// <param name="correlationId">The correlation ID for tracking this execution.</param>
160+
private void Execute(Guid correlationId)
155161
{
156-
// If the queue is null or empty, then no command is waiting to nbe
162+
// If the queue is null or empty, then no command is waiting to be
157163
// executed
158164
if (_processInfo == null || _processInfo.IsEmpty)
159165
{
@@ -171,7 +177,7 @@ private void Execute()
171177
using (Process process = new Process())
172178
{
173179
Logger.WriteLine(
174-
$"START: Process {startInfo.FileName} {startInfo.Arguments}.");
180+
$"[{correlationId}] START: Process {startInfo.FileName} {startInfo.Arguments}.");
175181

176182
process.StartInfo = startInfo;
177183
process.StartInfo.CreateNoWindow = true;
@@ -180,20 +186,20 @@ private void Execute()
180186
process.WaitForExit();
181187

182188
Logger.WriteLine(
183-
$"END: Process {process?.StartInfo.FileName} {process?.StartInfo.Arguments} has completed.");
189+
$"[{correlationId}] END: Process {process?.StartInfo.FileName} {process?.StartInfo.Arguments} has completed.");
184190
}
185191
}
186192
catch (Exception ex)
187193
{
188194
Logger.WriteLine(
189-
$"Could not run the command '{startInfo.FileName} {startInfo.Arguments}'. Reason: {ex.Message}",
195+
$"[{correlationId}] Could not run the command '{startInfo.FileName} {startInfo.Arguments}'. Reason: {ex.Message}",
190196
LogLevel.ERROR);
191197
}
192198
}
193199
else
194200
{
195201
Logger.WriteLine(
196-
$"The command '{startInfo.FileName}' was not found. Command was not run.",
202+
$"[{correlationId}] The command '{startInfo.FileName}' was not found. Command was not run.",
197203
LogLevel.ERROR);
198204
}
199205
}

src/Configuration/Notification.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ internal void QueueRequest(TriggerType trigger, ChangeInfo change)
184184
}
185185
}
186186

187-
Logger.WriteLine($"Sending request: {Method} {uri}.");
187+
string correlationIdLog = Change != null ? $"[{Change.CorrelationId}] " : "";
188+
Logger.WriteLine($"{correlationIdLog}Sending request: {Method} {uri}.");
188189
Response response =
189190
await Request.SendAsync(
190191
Method,

src/Configuration/Watch.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,27 +442,33 @@ public void ProcessChange()
442442
private void ProcessSingleChange(ChangeInfo change)
443443
{
444444
Logger.WriteLine(
445-
$"{IdLogString}: Change: {change.FullPath}, {change.Trigger} (Watch.ProcessChange)",
445+
$"[{change.CorrelationId}] {IdLogString}: Change: {change.FullPath}, {change.Trigger} (Watch.ProcessChange)",
446446
LogLevel.DEBUG);
447447

448448
if (!PassesFilters(change))
449449
{
450+
Logger.WriteLine(
451+
$"[{change.CorrelationId}] {IdLogString}: Change filtered out: {change.FullPath}",
452+
LogLevel.DEBUG);
450453
return;
451454
}
452455

453456
if (!PassesExclusions(change))
454457
{
458+
Logger.WriteLine(
459+
$"[{change.CorrelationId}] {IdLogString}: Change excluded: {change.FullPath}",
460+
LogLevel.DEBUG);
455461
return;
456462
}
457463

458464
Logger.WriteLine(
459-
$"{IdLogString}: Started: {change.FullPath}, {change.Trigger} (Watch.ProcessChange)",
465+
$"[{change.CorrelationId}] {IdLogString}: Started: {change.FullPath}, {change.Trigger} (Watch.ProcessChange)",
460466
LogLevel.DEBUG);
461467

462468
ExecuteWorkflows(change);
463469

464470
Logger.WriteLine(
465-
$"{IdLogString}: Completed: {change.FullPath}, {change.Trigger} (Watch.ProcessChange)",
471+
$"[{change.CorrelationId}] {IdLogString}: Completed: {change.FullPath}, {change.Trigger} (Watch.ProcessChange)",
466472
LogLevel.DEBUG);
467473
}
468474

0 commit comments

Comments
 (0)