diff --git a/AspNetCoreTestProject/CriticalBackgroundServices.cs b/AspNetCoreTestProject/CriticalBackgroundServices.cs index b56f957..d5e92bd 100644 --- a/AspNetCoreTestProject/CriticalBackgroundServices.cs +++ b/AspNetCoreTestProject/CriticalBackgroundServices.cs @@ -4,13 +4,14 @@ namespace AspNetCoreTestProject using System.Threading; using System.Threading.Tasks; using BetterHostedServices; + using Microsoft.Extensions.Logging; public class ImmediatelyCrashingCriticalBackgroundService : CriticalBackgroundService { protected override Task ExecuteAsync(CancellationToken stoppingToken) => throw new Exception("Crash right away"); - public ImmediatelyCrashingCriticalBackgroundService(IApplicationEnder applicationEnder) : base(applicationEnder) + public ImmediatelyCrashingCriticalBackgroundService(IApplicationEnder applicationEnder, ILogger criticalLogger) : base(applicationEnder, criticalLogger) { } } @@ -23,7 +24,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) throw new Exception("Crash after yielding"); } - public YieldingAndThenCrashingCriticalBackgroundService(IApplicationEnder applicationEnder) : base(applicationEnder) + public YieldingAndThenCrashingCriticalBackgroundService(IApplicationEnder applicationEnder, ILogger criticalLogger) : base(applicationEnder, criticalLogger) { } } @@ -38,7 +39,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) this.Activated = true; } - public StubCriticalBackgroundService(IApplicationEnder applicationEnder) : base(applicationEnder) + public StubCriticalBackgroundService(IApplicationEnder applicationEnder, ILogger criticalLogger) : base(applicationEnder, criticalLogger) { } } diff --git a/README.md b/README.md index a553d65..b2f9a4d 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ public class YieldingAndThenCrashingBackgroundService: CriticalBackgroundService throw new Exception("Oh no something really bad happened"); } - public YieldingAndThenCrashingBackgroundService(IApplicationEnder applicationEnder) : base(applicationEnder) { } + public YieldingAndThenCrashingBackgroundService(IApplicationEnder applicationEnder, ILogger logger) : base(applicationEnder, logger) { } } ``` And then you can use it like any other IHostedService. E.g. inside `ConfigureServices` you add the following: @@ -76,7 +76,7 @@ public class YieldingAndThenCrashingCriticalBackgroundService : CriticalBackgrou this._applicationEnder.ShutDownApplication(); // or simply call base.OnError } - public YieldingAndThenCrashingCriticalBackgroundService(IApplicationEnder applicationEnder) : base(applicationEnder) + public YieldingAndThenCrashingCriticalBackgroundService(IApplicationEnder applicationEnder, ILogger logger) : base(applicationEnder, logger) { } } diff --git a/src/CriticalBackgroundService.cs b/src/CriticalBackgroundService.cs index 7be2c41..6e5468e 100644 --- a/src/CriticalBackgroundService.cs +++ b/src/CriticalBackgroundService.cs @@ -2,6 +2,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; namespace BetterHostedServices { @@ -20,12 +21,14 @@ public abstract class CriticalBackgroundService : IHostedService, IDisposable /// Use this if you want to shutdown the application. /// protected IApplicationEnder _applicationEnder; + private readonly ILogger _logger; /// /// - protected CriticalBackgroundService(IApplicationEnder applicationEnder) + protected CriticalBackgroundService(IApplicationEnder applicationEnder, ILogger logger) { this._applicationEnder = applicationEnder; + this._logger = logger; } /// @@ -45,7 +48,8 @@ protected CriticalBackgroundService(IApplicationEnder applicationEnder) protected virtual void OnError(Exception exceptionFromExecuteAsync) { Console.Error.WriteLine($"Error happened while executing CriticalBackgroundTask {this.GetType().FullName}. Shutting down."); - Console.Error.WriteLine(exceptionFromExecuteAsync.ToString()); + this._logger.LogError(exceptionFromExecuteAsync, $"Error happened while executing CriticalBackgroundTask {this.GetType().FullName}. Shutting down."); + this._applicationEnder.ShutDownApplication(); } diff --git a/src/IServiceCollectionExtensions.cs b/src/IServiceCollectionExtensions.cs index c16b2aa..eee551b 100644 --- a/src/IServiceCollectionExtensions.cs +++ b/src/IServiceCollectionExtensions.cs @@ -66,9 +66,12 @@ public static void AddPeriodicTask(this IServiceCollection servic services.AddHostedService>((services) => { + var logger = services.GetRequiredService>>(); + return new PeriodicTaskRunnerBackgroundService( applicationEnder: services.GetRequiredService(), - logger: services.GetRequiredService>>(), + logger: logger, + criticalLogger: logger, serviceProvider: services.GetRequiredService(), periodicTaskFailureMode: failureMode, timeBetweenTasks: timeBetweenTasks diff --git a/src/PeriodicTaskRunnerBackgroundService.cs b/src/PeriodicTaskRunnerBackgroundService.cs index f036e6f..3b76436 100644 --- a/src/PeriodicTaskRunnerBackgroundService.cs +++ b/src/PeriodicTaskRunnerBackgroundService.cs @@ -24,15 +24,17 @@ public class PeriodicTaskRunnerBackgroundService : CriticalBackgr /// /// /// + /// /// /// /// public PeriodicTaskRunnerBackgroundService( IApplicationEnder applicationEnder, ILogger> logger, + ILogger criticalLogger, IServiceProvider serviceProvider, PeriodicTaskFailureMode periodicTaskFailureMode, - TimeSpan timeBetweenTasks) : base(applicationEnder) + TimeSpan timeBetweenTasks) : base(applicationEnder, criticalLogger) { this.logger = logger; this.serviceProvider = serviceProvider; diff --git a/tests/HostedServices/CriticalBackgroundServices.cs b/tests/HostedServices/CriticalBackgroundServices.cs index d945fb3..03c4952 100644 --- a/tests/HostedServices/CriticalBackgroundServices.cs +++ b/tests/HostedServices/CriticalBackgroundServices.cs @@ -4,13 +4,14 @@ namespace BetterHostedServices.Test.HostedServices using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; + using Microsoft.Extensions.Logging; - public class ImmediatelyCrashingCriticalBackgroundService: CriticalBackgroundService + public class ImmediatelyCrashingCriticalBackgroundService: CriticalBackgroundService { protected override Task ExecuteAsync(CancellationToken stoppingToken) => throw new Exception("Crash right away"); - public ImmediatelyCrashingCriticalBackgroundService(IApplicationEnder lifeTime) : base(lifeTime) + public ImmediatelyCrashingCriticalBackgroundService(IApplicationEnder lifeTime, ILogger criticalLogger) : base(lifeTime, criticalLogger) { } } @@ -23,7 +24,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) throw new Exception("Crash after yielding"); } - public YieldingAndThenCrashingCriticalBackgroundService(IApplicationEnder lifeTime) : base(lifeTime) + public YieldingAndThenCrashingCriticalBackgroundService(IApplicationEnder lifeTime, ILogger criticalLogger) : base(lifeTime, criticalLogger) { } diff --git a/tests/IntegrationUtils/AddHostedServiceAsSingletonTestClasses.cs b/tests/IntegrationUtils/AddHostedServiceAsSingletonTestClasses.cs index c8c34b7..65c70e6 100644 --- a/tests/IntegrationUtils/AddHostedServiceAsSingletonTestClasses.cs +++ b/tests/IntegrationUtils/AddHostedServiceAsSingletonTestClasses.cs @@ -3,6 +3,7 @@ namespace BetterHostedServices.Test.IntegrationUtils using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; + using Microsoft.Extensions.Logging; public interface ISomeBackgroundService @@ -12,7 +13,7 @@ public interface ISomeBackgroundService public class SomeBackgroundService : CriticalBackgroundService, ISomeBackgroundService { - public SomeBackgroundService(IApplicationEnder applicationEnder) : base(applicationEnder) + public SomeBackgroundService(IApplicationEnder applicationEnder, ILogger logger) : base(applicationEnder, logger) { }