Location
- File: src/ModularPipelines/FileSystem/File.cs
- Lines: 36-122 (multiple methods)
Problem
Almost every method starts with the same logging pattern:
public Task<string> ReadAsync(CancellationToken cancellationToken = default)
{
ModuleLogger.Current.LogInformation("Reading File: {Path}", this);
return System.IO.File.ReadAllTextAsync(Path, cancellationToken);
}
public IAsyncEnumerable<string> ReadLinesAsync(CancellationToken cancellationToken = default)
{
ModuleLogger.Current.LogInformation("Reading File: {Path}", this);
return System.IO.File.ReadLinesAsync(Path, cancellationToken);
}
public Task<byte[]> ReadBytesAsync(CancellationToken cancellationToken = default)
{
ModuleLogger.Current.LogInformation("Reading File: {Path}", this);
return System.IO.File.ReadAllBytesAsync(Path, cancellationToken);
}
// And many more with similar pattern...
Why it's a problem
- DRY Violation: The same logging call is repeated in 15+ methods
- Inconsistent Messages: Some say "Reading", some say "Writing to", making it harder to search logs
- Cross-Cutting Concern: Logging is scattered throughout business logic
- Hard to Disable: No way to disable file operation logging without modifying each method
- AOP Candidate: This is a classic case for aspect-oriented programming
Suggested Fix
- Create a helper method or use an interceptor pattern:
private void LogOperation(string operation)
{
ModuleLogger.Current.LogInformation("{Operation} File: {Path}", operation, this);
}
public Task<string> ReadAsync(CancellationToken cancellationToken = default)
{
LogOperation("Reading");
return System.IO.File.ReadAllTextAsync(Path, cancellationToken);
}
- Or use a logging wrapper:
private async Task<T> WithLogging<T>(string operation, Func<Task<T>> action)
{
ModuleLogger.Current.LogInformation("{Operation} File: {Path}", operation, this);
return await action().ConfigureAwait(false);
}
- Consider making logging optional via configuration
Category
Location
Problem
Almost every method starts with the same logging pattern:
Why it's a problem
Suggested Fix
Category