Skip to content

Commit 4e8caea

Browse files
committed
Log stop
1 parent 7df9ce6 commit 4e8caea

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The following improvements are included in this library:
1010

1111
* `OnStart()` can fail because of invalid service configuration, quit the application when that happens (by [kmcclellan](https://github.com/dotnet/runtime/issues/50019#issuecomment-678658133)).
1212
* On consumer OS Windows 10+, shutting down the computer will actually hibernate the OS. Services won't get another `OnStart()` call when the computer starts again, nor will your background services be notified. Now they will.
13-
* This library makes an `IHostedService` running inside a .NET Windows Service that aware of the user logging in and out, and the computer shutting down and starting up.
13+
* It also can notify your BackgroundServices about user session changes, i.e. logon, logoff and others.
1414
* When an exception occurs during your BackgroundService's lifetime, .NET Platform Extensions < 6 didn't stop the application host. Now it does, but it doesn't report an error to the Service Control Manager. With this extension, it does, as well as setting a process exit code: 13 in both cases ("invalid data").
1515

1616
## Installation

src/CodeCaster.WindowsServiceExtensions/Lifetime/ExtendedWindowsServiceLifetime.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public ExtendedWindowsServiceLifetime(IServiceProvider serviceProvider, IHostEnv
2424
: base(serviceProvider, environment, applicationLifetime, loggerFactory, optionsAccessor)
2525
{
2626
// Should not happen, here to keep the code analysis happy and the intention explicit.
27+
#pragma warning disable CA1416 // Validate platform compatibility
2728
if (!OperatingSystem.IsWindows() || !WindowsServiceHelpers.IsWindowsService())
2829
{
2930
const string methodName = nameof(WindowsServiceLifetimeHostBuilderExtensionsAdapter.UseWindowsServiceExtensions);
@@ -33,13 +34,17 @@ public ExtendedWindowsServiceLifetime(IServiceProvider serviceProvider, IHostEnv
3334

3435
// Explicitly stop the service instead of just exiting the process.
3536
// But this this will kill the application quickly, do other BackgroundServices get the time to exit nicely?
36-
applicationLifetime.ApplicationStopping.Register(Stop);
37+
applicationLifetime.ApplicationStopping.Register(() =>
38+
{
39+
Logger.LogInformation("Application stopping, stopping service, exit code: {exitCode}", ExitCode);
40+
41+
Stop();
42+
});
3743

3844
CanHandlePowerEvent = true;
3945
CanHandleSessionChangeEvent = true;
4046
}
4147

42-
#pragma warning disable CA1416 // Validate platform compatibility - constructor handles that
4348
/// <summary>
4449
/// Store a copy of all registered <see cref="IHostedService"/>s that implement our interface
4550
/// </summary>

0 commit comments

Comments
 (0)