Skip to content

Commit 9ae8985

Browse files
CopilotJusterZhu
andcommitted
Fix logger calls to use string interpolation instead of Serilog-style placeholders
Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com>
1 parent 2e460ea commit 9ae8985

7 files changed

Lines changed: 52 additions & 54 deletions

File tree

src/c#/GeneralUpdate.Drivelution/Abstractions/Events/DrivelutionLogger.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ public class DrivelutionLogger : IDrivelutionLogger
99
public event EventHandler<LogEventArgs>? LogMessage;
1010

1111
/// <inheritdoc />
12-
public void Debug(string message, params object[] args)
12+
public void Debug(string message, Exception? exception = null, params object[] args)
1313
{
14-
RaiseLogEvent(LogLevel.Debug, message, null, args);
14+
RaiseLogEvent(LogLevel.Debug, message, exception, args);
1515
}
1616

1717
/// <inheritdoc />
18-
public void Information(string message, params object[] args)
18+
public void Information(string message, Exception? exception = null, params object[] args)
1919
{
20-
RaiseLogEvent(LogLevel.Information, message, null, args);
20+
RaiseLogEvent(LogLevel.Information, message, exception, args);
2121
}
2222

2323
/// <inheritdoc />
24-
public void Warning(string message, params object[] args)
24+
public void Warning(string message, Exception? exception = null, params object[] args)
2525
{
26-
RaiseLogEvent(LogLevel.Warning, message, null, args);
26+
RaiseLogEvent(LogLevel.Warning, message, exception, args);
2727
}
2828

2929
/// <inheritdoc />
@@ -42,6 +42,7 @@ private void RaiseLogEvent(LogLevel level, string message, Exception? exception,
4242
{
4343
try
4444
{
45+
// Format message if args provided
4546
var formattedMessage = args.Length > 0 ? string.Format(message, args) : message;
4647

4748
var eventArgs = new LogEventArgs

src/c#/GeneralUpdate.Drivelution/Abstractions/Events/IDrivelutionLogger.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ public interface IDrivelutionLogger
1414
/// <summary>
1515
/// Logs a debug message
1616
/// </summary>
17-
void Debug(string message, params object[] args);
17+
void Debug(string message, Exception? exception = null, params object[] args);
1818

1919
/// <summary>
2020
/// Logs an information message
2121
/// </summary>
22-
void Information(string message, params object[] args);
22+
void Information(string message, Exception? exception = null, params object[] args);
2323

2424
/// <summary>
2525
/// Logs a warning message
2626
/// </summary>
27-
void Warning(string message, params object[] args);
27+
void Warning(string message, Exception? exception = null, params object[] args);
2828

2929
/// <summary>
3030
/// Logs an error message

src/c#/GeneralUpdate.Drivelution/Core/DriverUpdaterFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static IGeneralDrivelution Create(IDrivelutionLogger? logger = null, Driv
5353
else
5454
{
5555
var osDescription = RuntimeInformation.OSDescription;
56-
logger.Error("Unsupported platform detected: {Platform}", null, osDescription);
56+
logger.Error($"Unsupported platform detected: {osDescription}");
5757
throw new PlatformNotSupportedException(
5858
$"Current platform '{osDescription}' is not supported. " +
5959
"Supported platforms: Windows (8+), Linux (Ubuntu 18.04+, CentOS 7+, Debian 10+)");

src/c#/GeneralUpdate.Drivelution/Linux/Implementation/LinuxGeneralDrivelution.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,9 @@ private async Task ParseKernelModuleAsync(string koPath, DriverInfo driverInfo,
501501
driverInfo.Version = "1.0.0";
502502
}
503503
}
504-
catch (Exception)
504+
catch (Exception ex)
505505
{
506-
_logger.Debug($"Could not get module info for: {koPath}");
506+
_logger.Debug($"Could not get module info for: {koPath}", ex);
507507
driverInfo.Version = "1.0.0";
508508
}
509509
}
@@ -540,9 +540,9 @@ private async Task ParseDebPackageAsync(string debPath, DriverInfo driverInfo, C
540540
driverInfo.Version = "1.0.0";
541541
}
542542
}
543-
catch (Exception)
543+
catch (Exception ex)
544544
{
545-
_logger.Debug($"Could not get package info for: {debPath}");
545+
_logger.Debug($"Could not get package info for: {debPath}", ex);
546546
driverInfo.Version = "1.0.0";
547547
}
548548
}
@@ -587,9 +587,9 @@ private async Task ParseRpmPackageAsync(string rpmPath, DriverInfo driverInfo, C
587587
driverInfo.Version = "1.0.0";
588588
}
589589
}
590-
catch (Exception)
590+
catch (Exception ex)
591591
{
592-
_logger.Debug($"Could not get package info for: {rpmPath}");
592+
_logger.Debug($"Could not get package info for: {rpmPath}", ex);
593593
driverInfo.Version = "1.0.0";
594594
}
595595
}

src/c#/GeneralUpdate.Drivelution/Windows/Implementation/WindowsDriverBackup.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async Task<bool> BackupAsync(
2525
string backupPath,
2626
CancellationToken cancellationToken = default)
2727
{
28-
_logger.Information("Backing up driver from {SourcePath} to {BackupPath}", sourcePath, backupPath);
28+
_logger.Information($"Backing up driver from {sourcePath} to {backupPath}");
2929

3030
try
3131
{
@@ -39,7 +39,7 @@ public async Task<bool> BackupAsync(
3939
if (!string.IsNullOrEmpty(backupDir) && !Directory.Exists(backupDir))
4040
{
4141
Directory.CreateDirectory(backupDir);
42-
_logger.Information("Created backup directory: {BackupDir}", backupDir);
42+
_logger.Information($"Created backup directory: {backupDir}");
4343
}
4444

4545
// Add timestamp to backup filename to avoid conflicts
@@ -57,7 +57,7 @@ public async Task<bool> BackupAsync(
5757
await sourceStream.CopyToAsync(destinationStream, cancellationToken);
5858
}
5959

60-
_logger.Information("Driver backup completed successfully: {BackupPath}", backupPathWithTimestamp);
60+
_logger.Information($"Driver backup completed successfully: {backupPathWithTimestamp}");
6161
return true;
6262
}
6363
catch (Exception ex)
@@ -73,7 +73,7 @@ public async Task<bool> RestoreAsync(
7373
string targetPath,
7474
CancellationToken cancellationToken = default)
7575
{
76-
_logger.Information("Restoring driver from {BackupPath} to {TargetPath}", backupPath, targetPath);
76+
_logger.Information($"Restoring driver from {backupPath} to {targetPath}");
7777

7878
try
7979
{
@@ -87,15 +87,15 @@ public async Task<bool> RestoreAsync(
8787
if (!string.IsNullOrEmpty(targetDir) && !Directory.Exists(targetDir))
8888
{
8989
Directory.CreateDirectory(targetDir);
90-
_logger.Information("Created target directory: {TargetDir}", targetDir);
90+
_logger.Information($"Created target directory: {targetDir}");
9191
}
9292

9393
// Backup existing target file if it exists
9494
if (File.Exists(targetPath))
9595
{
9696
var tempBackup = $"{targetPath}.old";
9797
File.Move(targetPath, tempBackup, true);
98-
_logger.Information("Moved existing file to temporary backup: {TempBackup}", tempBackup);
98+
_logger.Information($"Moved existing file to temporary backup: {tempBackup}");
9999
}
100100

101101
// Copy backup file to target location
@@ -120,7 +120,7 @@ public async Task<bool> DeleteBackupAsync(
120120
string backupPath,
121121
CancellationToken cancellationToken = default)
122122
{
123-
_logger.Information("Deleting backup: {BackupPath}", backupPath);
123+
_logger.Information($"Deleting backup: {backupPath}");
124124

125125
return await Task.Run(() =>
126126
{
@@ -134,7 +134,7 @@ public async Task<bool> DeleteBackupAsync(
134134
}
135135
else
136136
{
137-
_logger.Warning("Backup file not found: {BackupPath}", backupPath);
137+
_logger.Warning($"Backup file not found: {backupPath}");
138138
return false;
139139
}
140140
}

src/c#/GeneralUpdate.Drivelution/Windows/Implementation/WindowsDriverValidator.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public async Task<bool> ValidateIntegrityAsync(
2929
string hashAlgorithm = "SHA256",
3030
CancellationToken cancellationToken = default)
3131
{
32-
_logger.Information("Validating file integrity: {FilePath}", filePath);
32+
_logger.Information($"Validating file integrity: {filePath}");
3333

3434
try
3535
{
@@ -62,7 +62,7 @@ public async Task<bool> ValidateSignatureAsync(
6262
IEnumerable<string> trustedPublishers,
6363
CancellationToken cancellationToken = default)
6464
{
65-
_logger.Information("Validating driver signature: {FilePath}", filePath);
65+
_logger.Information($"Validating driver signature: {filePath}");
6666

6767
try
6868
{
@@ -94,7 +94,7 @@ public async Task<bool> ValidateCompatibilityAsync(
9494
DriverInfo driverInfo,
9595
CancellationToken cancellationToken = default)
9696
{
97-
_logger.Information("Validating driver compatibility for: {DriverName}", driverInfo.Name);
97+
_logger.Information($"Validating driver compatibility for: {driverInfo.Name}");
9898

9999
try
100100
{
@@ -108,9 +108,8 @@ public async Task<bool> ValidateCompatibilityAsync(
108108
{
109109
_logger.Warning("Driver compatibility validation failed");
110110
var report = CompatibilityChecker.GetCompatibilityReport(driverInfo);
111-
_logger.Warning("Compatibility report: Current OS={CurrentOS}, Target OS={TargetOS}, " +
112-
"Current Arch={CurrentArch}, Target Arch={TargetArch}",
113-
report.CurrentOS, report.TargetOS, report.CurrentArchitecture, report.TargetArchitecture);
111+
_logger.Warning($"Compatibility report: Current OS={report.CurrentOS}, Target OS={report.TargetOS}, " +
112+
$"Current Arch={report.CurrentArchitecture}, Target Arch={report.TargetArchitecture}");
114113
}
115114

116115
return isCompatible;

src/c#/GeneralUpdate.Drivelution/Windows/Implementation/WindowsGeneralDrivelution.cs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public async Task<UpdateResult> UpdateAsync(
4141

4242
try
4343
{
44-
_logger.Information("Starting driver update for: {DriverName} v{Version}",
45-
driverInfo.Name, driverInfo.Version);
44+
_logger.Information($"Starting driver update for: {driverInfo.Name} v{driverInfo.Version}");
4645

4746
result.StepLogs.Add($"[{DateTime.Now:HH:mm:ss}] Starting driver update");
4847

@@ -78,7 +77,7 @@ public async Task<UpdateResult> UpdateAsync(
7877
if (await BackupAsync(driverInfo, backupPath, cancellationToken))
7978
{
8079
result.BackupPath = backupPath;
81-
_logger.Information("Backup created at: {BackupPath}", backupPath);
80+
_logger.Information($"Backup created at: {backupPath}");
8281
}
8382
}
8483

@@ -161,8 +160,7 @@ public async Task<UpdateResult> UpdateAsync(
161160
finally
162161
{
163162
result.EndTime = DateTime.UtcNow;
164-
_logger.Information("Driver update process ended. Duration: {Duration}ms, Success: {Success}",
165-
result.DurationMs, result.Success);
163+
_logger.Information($"Driver update process ended. Duration: {result.DurationMs}ms, Success: {result.Success}");
166164
}
167165

168166
return result;
@@ -171,7 +169,7 @@ public async Task<UpdateResult> UpdateAsync(
171169
/// <inheritdoc/>
172170
public async Task<bool> ValidateAsync(DriverInfo driverInfo, CancellationToken cancellationToken = default)
173171
{
174-
_logger.Information("Validating driver: {DriverName}", driverInfo.Name);
172+
_logger.Information($"Validating driver: {driverInfo.Name}");
175173

176174
try
177175
{
@@ -241,7 +239,7 @@ public async Task<bool> RollbackAsync(string backupPath, CancellationToken cance
241239
{
242240
try
243241
{
244-
_logger.Information("Rolling back driver from backup: {BackupPath}", backupPath);
242+
_logger.Information($"Rolling back driver from backup: {backupPath}");
245243

246244
// Implement rollback logic
247245
// This involves:
@@ -258,11 +256,11 @@ public async Task<bool> RollbackAsync(string backupPath, CancellationToken cance
258256
var backupFiles = Directory.GetFiles(backupPath, "*.*", SearchOption.AllDirectories);
259257
if (!backupFiles.Any())
260258
{
261-
_logger.Warning("No backup files found in: {BackupPath}", backupPath);
259+
_logger.Warning($"No backup files found in: {backupPath}");
262260
return false;
263261
}
264262

265-
_logger.Information("Found {Count} backup files", backupFiles.Length);
263+
_logger.Information($"Found {backupFiles.Length} backup files");
266264

267265
// For INF-based drivers, try to reinstall the backed up version
268266
var infFiles = backupFiles.Where(f => f.EndsWith(".inf", StringComparison.OrdinalIgnoreCase)).ToArray();
@@ -273,7 +271,7 @@ public async Task<bool> RollbackAsync(string backupPath, CancellationToken cance
273271
{
274272
try
275273
{
276-
_logger.Information("Attempting to restore driver from: {InfFile}", infFile);
274+
_logger.Information($"Attempting to restore driver from: {infFile}");
277275
await InstallDriverUsingPnPUtilAsync(infFile, cancellationToken);
278276
}
279277
catch (Exception ex)
@@ -301,7 +299,7 @@ private async Task ExecuteDriverInstallationAsync(
301299
UpdateStrategy strategy,
302300
CancellationToken cancellationToken)
303301
{
304-
_logger.Information("Executing driver installation: {DriverPath}", driverInfo.FilePath);
302+
_logger.Information($"Executing driver installation: {driverInfo.FilePath}");
305303

306304
try
307305
{
@@ -328,7 +326,7 @@ private async Task ExecuteDriverInstallationAsync(
328326
/// </summary>
329327
private async Task InstallDriverUsingPnPUtilAsync(string driverPath, CancellationToken cancellationToken)
330328
{
331-
_logger.Information("Installing driver using PnPUtil: {DriverPath}", driverPath);
329+
_logger.Information($"Installing driver using PnPUtil: {driverPath}");
332330

333331
var startInfo = new ProcessStartInfo
334332
{
@@ -348,7 +346,7 @@ private async Task InstallDriverUsingPnPUtilAsync(string driverPath, Cancellatio
348346

349347
await process.WaitForExitAsync(cancellationToken);
350348

351-
_logger.Information("PnPUtil output: {Output}", output);
349+
_logger.Information($"PnPUtil output: {output}");
352350

353351
if (process.ExitCode != 0)
354352
{
@@ -366,7 +364,7 @@ private async Task<bool> VerifyDriverInstallationAsync(DriverInfo driverInfo, Ca
366364
{
367365
try
368366
{
369-
_logger.Information("Verifying driver installation for: {DriverPath}", driverInfo.FilePath);
367+
_logger.Information($"Verifying driver installation for: {driverInfo.FilePath}");
370368

371369
// Use PnPUtil to enumerate installed drivers and check if our driver is present
372370
var processStartInfo = new ProcessStartInfo
@@ -394,7 +392,7 @@ private async Task<bool> VerifyDriverInstallationAsync(DriverInfo driverInfo, Ca
394392
bool isInstalled = output.Contains(driverFileName, StringComparison.OrdinalIgnoreCase) ||
395393
output.Contains(Path.GetFileNameWithoutExtension(driverInfo.FilePath), StringComparison.OrdinalIgnoreCase);
396394

397-
_logger.Information("Driver verification result: {IsInstalled}", isInstalled);
395+
_logger.Information($"Driver verification result: {isInstalled}");
398396
return isInstalled;
399397
}
400398
catch (Exception ex)
@@ -467,19 +465,19 @@ public async Task<List<DriverInfo>> GetDriversFromDirectoryAsync(
467465

468466
try
469467
{
470-
_logger.Information("Reading driver information from directory: {DirectoryPath}", directoryPath);
468+
_logger.Information($"Reading driver information from directory: {directoryPath}");
471469

472470
if (!Directory.Exists(directoryPath))
473471
{
474-
_logger.Warning("Directory not found: {DirectoryPath}", directoryPath);
472+
_logger.Warning($"Directory not found: {directoryPath}");
475473
return driverInfoList;
476474
}
477475

478476
// Default to .inf files for Windows
479477
var pattern = searchPattern ?? "*.inf";
480478
var driverFiles = Directory.GetFiles(directoryPath, pattern, SearchOption.AllDirectories);
481479

482-
_logger.Information("Found {Count} driver files matching pattern: {Pattern}", driverFiles.Length, pattern);
480+
_logger.Information($"Found {driverFiles.Length} driver files matching pattern: {pattern}");
483481

484482
foreach (var filePath in driverFiles)
485483
{
@@ -492,7 +490,7 @@ public async Task<List<DriverInfo>> GetDriversFromDirectoryAsync(
492490
if (driverInfo != null)
493491
{
494492
driverInfoList.Add(driverInfo);
495-
_logger.Information("Parsed driver: {DriverName} v{Version}", driverInfo.Name, driverInfo.Version);
493+
_logger.Information($"Parsed driver: {driverInfo.Name} v{driverInfo.Version}");
496494
}
497495
}
498496
catch (Exception ex)
@@ -501,7 +499,7 @@ public async Task<List<DriverInfo>> GetDriversFromDirectoryAsync(
501499
}
502500
}
503501

504-
_logger.Information("Successfully loaded {Count} driver(s) from directory", driverInfoList.Count);
502+
_logger.Information($"Successfully loaded {driverInfoList.Count} driver(s) from directory");
505503
}
506504
catch (Exception ex)
507505
{
@@ -573,9 +571,9 @@ public async Task<List<DriverInfo>> GetDriversFromDirectoryAsync(
573571
}
574572
}
575573
}
576-
catch
574+
catch (Exception ex)
577575
{
578-
_logger.Debug($"Could not get signature for file: {filePath}");
576+
_logger.Debug($"Could not get signature for file: {filePath}", ex);
579577
}
580578

581579
return driverInfo;
@@ -645,9 +643,9 @@ private async Task ParseInfFileAsync(string infPath, DriverInfo driverInfo, Canc
645643
driverInfo.Version = "1.0.0";
646644
}
647645
}
648-
catch
646+
catch (Exception ex)
649647
{
650-
_logger.Debug($"Could not parse INF file: {infPath}");
648+
_logger.Debug($"Could not parse INF file: {infPath}", ex);
651649
}
652650
}
653651
}

0 commit comments

Comments
 (0)