Skip to content

Commit 0276bc7

Browse files
author
Per Kops
committed
fix: update logging in unhandled exception scenarios
1 parent 6ffb131 commit 0276bc7

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

src/Atc.Hosting/BackgroundScheduleServiceBase.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,11 @@ public abstract Task DoWorkAsync(
163163
protected virtual Task OnExceptionAsync(
164164
Exception exception,
165165
CancellationToken stoppingToken)
166-
=> Task.CompletedTask;
166+
{
167+
logger.LogBackgroundServiceUnhandledException(exception, ServiceName);
168+
169+
return Task.CompletedTask;
170+
}
167171

168172
/// <inheritdoc />
169173
[SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "OK - by design.")]
@@ -207,8 +211,7 @@ protected override async Task ExecuteAsync(
207211

208212
logger.LogBackgroundServiceRetrying(
209213
ServiceName,
210-
ServiceOptions.CronExpression,
211-
ex);
214+
ServiceOptions.CronExpression);
212215
}
213216

214217
nextOccurrence = cronExpression.GetNextOccurrence(DateTime.UtcNow);
@@ -225,7 +228,7 @@ protected override async Task ExecuteAsync(
225228
}
226229
catch (Exception ex)
227230
{
228-
logger.LogBackgroundServiceUnhandledException(ServiceName, ex);
231+
logger.LogBackgroundServiceUnhandledException(ex, ServiceName);
229232
}
230233
finally
231234
{

src/Atc.Hosting/BackgroundServiceBase.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ public abstract Task DoWorkAsync(
168168
protected virtual Task OnExceptionAsync(
169169
Exception exception,
170170
CancellationToken stoppingToken)
171-
=> Task.CompletedTask;
171+
{
172+
logger.LogBackgroundServiceUnhandledException(exception, ServiceName);
173+
174+
return Task.CompletedTask;
175+
}
172176

173177
/// <inheritdoc />
174178
[SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "OK - by design.")]
@@ -204,8 +208,7 @@ await Task
204208

205209
logger.LogBackgroundServiceRetrying(
206210
ServiceName,
207-
ServiceOptions.RepeatIntervalSeconds,
208-
ex);
211+
ServiceOptions.RepeatIntervalSeconds);
209212
}
210213

211214
await Task
@@ -219,7 +222,7 @@ await Task
219222
}
220223
catch (Exception ex)
221224
{
222-
logger.LogBackgroundServiceUnhandledException(ServiceName, ex);
225+
logger.LogBackgroundServiceUnhandledException(ex, ServiceName);
223226
}
224227
finally
225228
{

src/Atc.Hosting/Extensions/LoggerExtensions.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal static partial class LoggerExtensions
99
[LoggerMessage(
1010
EventId = LoggingEventIdConstants.BackgroundService.Started,
1111
Level = LogLevel.Information,
12-
Message = "Started worker {serviceName}. Worker will run with {repeatIntervalSeconds} seconds interval",
12+
Message = "Started worker {ServiceName}. Worker will run with {RepeatIntervalSeconds} seconds interval",
1313
SkipEnabledCheck = false)]
1414
internal static partial void LogBackgroundServiceStarted(
1515
this ILogger logger,
@@ -19,7 +19,7 @@ internal static partial void LogBackgroundServiceStarted(
1919
[LoggerMessage(
2020
EventId = LoggingEventIdConstants.BackgroundService.Started,
2121
Level = LogLevel.Information,
22-
Message = "Started worker {serviceName}. Worker will run with cron expression {cronExpression}",
22+
Message = "Started worker {ServiceName}. Worker will run with cron expression {CronExpression}",
2323
SkipEnabledCheck = false)]
2424
internal static partial void LogBackgroundServiceStarted(
2525
this ILogger logger,
@@ -29,7 +29,7 @@ internal static partial void LogBackgroundServiceStarted(
2929
[LoggerMessage(
3030
EventId = LoggingEventIdConstants.BackgroundService.Stopped,
3131
Level = LogLevel.Information,
32-
Message = "Stopped worker {serviceName}",
32+
Message = "Stopped worker {ServiceName}",
3333
SkipEnabledCheck = false)]
3434
internal static partial void LogBackgroundServiceStopped(
3535
this ILogger logger,
@@ -38,41 +38,39 @@ internal static partial void LogBackgroundServiceStopped(
3838
[LoggerMessage(
3939
EventId = LoggingEventIdConstants.BackgroundService.Cancelled,
4040
Level = LogLevel.Warning,
41-
Message = "Cancellation invoked for worker {serviceName}",
41+
Message = "Cancellation invoked for worker {ServiceName}",
4242
SkipEnabledCheck = false)]
4343
internal static partial void LogBackgroundServiceCancelled(
4444
this ILogger logger,
4545
string serviceName);
4646

4747
[LoggerMessage(
4848
EventId = LoggingEventIdConstants.BackgroundService.Retrying,
49-
Level = LogLevel.Warning,
50-
Message = "Unhandled exception occurred in worker {serviceName}. Worker will retry after {repeatIntervalSeconds} seconds",
49+
Level = LogLevel.Information,
50+
Message = "Worker {ServiceName} will retry after {RepeatIntervalSeconds} seconds",
5151
SkipEnabledCheck = false)]
5252
internal static partial void LogBackgroundServiceRetrying(
5353
this ILogger logger,
5454
string serviceName,
55-
int repeatIntervalSeconds,
56-
Exception exception);
55+
int repeatIntervalSeconds);
5756

5857
[LoggerMessage(
5958
EventId = LoggingEventIdConstants.BackgroundService.Retrying,
60-
Level = LogLevel.Warning,
61-
Message = "Unhandled exception occurred in worker {serviceName}. Worker will retry on next defined in cron expression {cronExpression}",
59+
Level = LogLevel.Information,
60+
Message = "Worker {ServiceName} will retry on next occurence pr the cron expression {CronExpression}",
6261
SkipEnabledCheck = false)]
6362
internal static partial void LogBackgroundServiceRetrying(
6463
this ILogger logger,
6564
string serviceName,
66-
string cronExpression,
67-
Exception exception);
65+
string cronExpression);
6866

6967
[LoggerMessage(
7068
EventId = LoggingEventIdConstants.BackgroundService.UnhandledException,
7169
Level = LogLevel.Error,
72-
Message = "Unhandled exception occurred in worker {serviceName}",
70+
Message = "Unhandled exception occurred in worker {ServiceName}",
7371
SkipEnabledCheck = false)]
7472
internal static partial void LogBackgroundServiceUnhandledException(
7573
this ILogger logger,
76-
string serviceName,
77-
Exception exception);
74+
Exception exception,
75+
string serviceName);
7876
}

test/Atc.Hosting.Tests/BackgroundServiceBaseTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,8 @@ public async Task Logging_Start_Retry_Cancel_Stop()
128128

129129
logger
130130
.Log(
131-
LogLevel.Warning,
132-
$"Unhandled exception occurred in worker {serviceName}. Worker will retry after {options.RepeatIntervalSeconds} seconds",
133-
exception);
131+
LogLevel.Information,
132+
$"Worker {serviceName} will retry after {options.RepeatIntervalSeconds} seconds");
134133

135134
logger
136135
.Log(

0 commit comments

Comments
 (0)