Skip to content

Commit ff13314

Browse files
fix: warnings on logs
1 parent 80fe4ed commit ff13314

File tree

12 files changed

+56
-14
lines changed

12 files changed

+56
-14
lines changed

src/app/ApplicationTemplate.Access/PlatformServices/Email/MockedEmailService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ public MockedEmailService(ILogger<MockedEmailService> logger)
1515

1616
public Task Compose(CancellationToken ct, Email email)
1717
{
18-
_logger.LogInformation("Email composed: {Email}", email);
18+
if (_logger.IsEnabled(LogLevel.Information))
19+
{
20+
_logger.LogInformation("Email composed: {Email}", email);
21+
}
1922
return Task.CompletedTask;
2023
}
2124
}

src/app/ApplicationTemplate.Business/KillSwitch/KillSwitchService.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,11 @@ public KillSwitchService(IKillSwitchDataSource killSwitchDataSource, ILogger<Kil
2626

2727
/// <inheritdoc/>
2828
public IObservable<bool> ObserveKillSwitchActivation() => _killSwitchDataSource.ObserveKillSwitchActivation()
29-
.Do(isActive => _logger.LogInformation("Kill switch is now {IsActive}.", isActive));
29+
.Do(isActive =>
30+
{
31+
if (_logger.IsEnabled(LogLevel.Information))
32+
{
33+
_logger.LogInformation("Kill switch is now {IsActive}.", isActive);
34+
}
35+
});
3036
}

src/app/ApplicationTemplate.Presentation/Configuration/ErrorConfiguration.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ public static void OnUnhandledException(Exception exception, bool isTerminating,
4141
}
4242

4343
var logger = services.GetRequiredService<ILogger<CoreStartup>>();
44-
logger.LogError(exception, "An unhandled exception occurred. StackTrace: {StackTrace}", exception.StackTrace);
44+
45+
if (logger.IsEnabled(LogLevel.Error))
46+
{
47+
logger.LogError(exception, "An unhandled exception occurred. StackTrace: {StackTrace}", exception.StackTrace);
48+
}
4549
}
4650

4751
private static async Task HandleCommandException(CancellationToken ct, IDynamicCommand command, Exception exception, IServiceProvider services)

src/app/ApplicationTemplate.Presentation/CoreStartup.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,10 @@ private void SuscribeToKillSwitch(IServiceProvider serviceProvider)
229229
killSwitchService.ObserveKillSwitchActivation()
230230
.SelectManyDisposePrevious(async (activated, ct) =>
231231
{
232-
Logger.LogTrace("Kill switch activation changed to {Activated}.", activated);
232+
if (Logger.IsEnabled(LogLevel.Trace))
233+
{
234+
Logger.LogTrace("Kill switch activation changed to {Activated}.", activated);
235+
}
233236

234237
if (activated)
235238
{

src/app/ApplicationTemplate.Presentation/Framework/DataLoader/LogErrorDataLoaderStrategy.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ public override async Task<object> Load(CancellationToken ct, IDataLoaderRequest
3131
}
3232
catch (Exception error)
3333
{
34-
_logger.LogError(error, "Failed to load request '{RequestSequenceId}' in DataLoader '{DataLoaderName}'.", request.SequenceId, request.Context.GetDataLoaderName());
34+
if (_logger.IsEnabled(LogLevel.Error))
35+
{
36+
_logger.LogError(error, "Failed to load request '{RequestSequenceId}' in DataLoader '{DataLoaderName}'.", request.SequenceId, request.Context.GetDataLoaderName());
37+
}
3538

3639
throw;
3740
}

src/app/ApplicationTemplate.Presentation/Framework/Startup/CoreStartupBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ public async Task Start()
150150

151151
var isFirstStart = !State.IsStarted;
152152

153-
Logger.LogDebug("Starting services (isFirstStart: {IsFirstStart}).", isFirstStart);
153+
if (Logger.IsEnabled(LogLevel.Debug))
154+
{
155+
Logger.LogDebug("Starting services (isFirstStart: {IsFirstStart}).", isFirstStart);
156+
}
154157

155158
await StartServices(ServiceProvider, isFirstStart);
156159

src/app/ApplicationTemplate.Presentation/Framework/Startup/StartupBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ public async Task Start()
152152

153153
async Task StartViewServicesWithLogs(IServiceProvider services, bool isFirstStart)
154154
{
155-
Logger.LogDebug("Starting view services (isFirstStart: {IsFirstStart}).", isFirstStart);
155+
if (Logger.IsEnabled(LogLevel.Debug))
156+
{
157+
Logger.LogDebug("Starting view services (isFirstStart: {IsFirstStart}).", isFirstStart);
158+
}
156159

157160
await StartViewServices(services, isFirstStart);
158161

src/app/ApplicationTemplate.Presentation/Framework/Startup/WritableJsonConfigurationProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ public override void Set(string key, string value)
5050
}
5151
}
5252

53-
_logger.LogDebug("Serialized ­­­­{PairCount} key-value-pairs in {ElapsedMilliseconds}ms.", Data.Count, stopwatch.ElapsedMilliseconds);
54-
53+
if (_logger.IsEnabled(LogLevel.Debug))
54+
{
55+
_logger.LogDebug("Serialized ­­­­{PairCount} key-value-pairs in {ElapsedMilliseconds}ms.", Data.Count, stopwatch.ElapsedMilliseconds);
56+
}
5557
OnReload();
5658
}
5759
}

src/app/ApplicationTemplate.Presentation/Framework/ViewModels/IViewModel.Extensions.Options.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,12 @@ string GetValueAsString(TValue value)
126126
{
127127
if (value is not string && value is not bool)
128128
{
129-
viewModel.GetService<ILogger<TOptions>>().LogWarning("Serialization of type {TypeName} may be wrong. Consider using the valueToString parameter from the GetFromOptionsMonitor method.", typeof(TValue).Name);
129+
var logger = viewModel.GetService<ILogger>();
130+
131+
if (logger.IsEnabled(LogLevel.Warning))
132+
{
133+
logger.LogWarning("Serialization of type {TypeName} may be wrong. Consider using the valueToString parameter from the GetFromOptionsMonitor method.", typeof(TValue).Name);
134+
}
130135
}
131136

132137
return value.ToString();

src/app/ApplicationTemplate.Presentation/ViewModels/Diagnostics/LoggersDiagnosticsViewModel.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ await this.GetService<IMessageDialogService>().ShowMessage(ct, mb => mb
8383

8484
private async Task OnConsoleLoggingChanged(CancellationToken ct, bool isEnabled)
8585
{
86-
this.GetService<ILogger<LoggersDiagnosticsViewModel>>().LogInformation("{IsEnabled} console logging.", isEnabled ? "Enabling" : "Disabling");
86+
var logger = this.GetService<ILogger<LoggersDiagnosticsViewModel>>();
87+
88+
if (logger.IsEnabled(LogLevel.Information))
89+
{
90+
logger.LogInformation("{IsEnabled} console logging.", isEnabled ? "Enabling" : "Disabling");
91+
}
8792

8893
await this.GetService<IMessageDialogService>().ShowMessage(ct, mb => mb
8994
.Title("Diagnostics")
@@ -94,7 +99,12 @@ await this.GetService<IMessageDialogService>().ShowMessage(ct, mb => mb
9499

95100
private async Task OnFileLoggingChanged(CancellationToken ct, bool isEnabled)
96101
{
97-
this.GetService<ILogger<LoggersDiagnosticsViewModel>>().LogInformation("{IsEnabled} file logging.", isEnabled ? "Enabling" : "Disabling");
102+
var logger = this.GetService<ILogger<LoggersDiagnosticsViewModel>>();
103+
104+
if (logger.IsEnabled(LogLevel.Information))
105+
{
106+
logger.LogInformation("{IsEnabled} file logging.", isEnabled ? "Enabling" : "Disabling");
107+
}
98108

99109
await this.GetService<IMessageDialogService>().ShowMessage(ct, mb => mb
100110
.Title("Diagnostics")

0 commit comments

Comments
 (0)