Where can I log an ASP.NET Core app's start/stop/error events?
You need to use Microsoft.AspNetCore.Hosting.IApplicationLifetime
/// <summary>
/// Triggered when the application host has fully started and is about to wait
/// for a graceful shutdown.
/// </summary>
CancellationToken ApplicationStarted { get; }
/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// Requests may still be in flight. Shutdown will block until this event completes.
/// </summary>
CancellationToken ApplicationStopping { get; }
/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// All requests should be complete at this point. Shutdown will block
/// until this event completes.
/// </summary>
CancellationToken ApplicationStopped { get; }
Instance of IApplicationLifetime could be obtained in Configure
method. Also add ILoggerFactory here:
public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime, ILoggerFactory loggerFactory)
{
// use applicationLifetime
}
Having ILoggerFactory
, you can create instance of ILogger
:
var logger = loggerFactory.CreateLogger("StartupLogger");
So you just need to create a property in the Startup class to persist the instance of ILogger
(or ILoggerFactory
, if you would like to create different ligger instance for different events). To summarize:
public class Startup
{
private ILogger _logger;
public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime, ILoggerFactory loggerFactory)
{
applicationLifetime.ApplicationStopping.Register(OnShutdown);
...
// add logger providers
// loggerFactory.AddConsole()
...
_logger = loggerFactory.CreateLogger("StartupLogger");
}
private void OnShutdown()
{
// use _logger here;
}
}
Please see CaptureStartupErrors and the method .CaptureStartupErrors(true)
that will help you find issues.
This is especially handy when something runs perfect on localhost but fails in Azure.
Here is my usual config for NetCore Web Apps:
public static IWebHost BuildWebHost(string[] args) => WebHost
.CreateDefaultBuilder(args)
.CaptureStartupErrors(true)
.UseKestrel()
.UseIISIntegration()
.UseStartup<Startup>()
.UseAzureAppServices()
.Build();
In Azure App Service you can then find the logs in the log stream in Kudu Tools https://<appname>.scm.azurewebsites.net/api/logstream
I didn't like @neustart47 answer as it was unnecessarily complex but he is right that IApplicationLifetime
is obsolete.
Taken from the Microsoft Docs
// 1. Add the interface `IHostedService` to the class you would like
// to be called during an application event.
internal class LifetimeEventsHostedService : IHostedService
{
private readonly ILogger _logger;
private readonly IHostApplicationLifetime _appLifetime;
// 2. Inject `IHostApplicationLifetime` through dependency injection in the constructor.
public LifetimeEventsHostedService(
ILogger<LifetimeEventsHostedService> logger,
IHostApplicationLifetime appLifetime)
{
_logger = logger;
_appLifetime = appLifetime;
}
// 3. Implemented by `IHostedService`, setup here your event registration.
public Task StartAsync(CancellationToken cancellationToken)
{
_appLifetime.ApplicationStarted.Register(OnStarted);
_appLifetime.ApplicationStopping.Register(OnStopping);
_appLifetime.ApplicationStopped.Register(OnStopped);
return Task.CompletedTask;
}
// 4. Implemented by `IHostedService`, setup here your shutdown registration.
// If you have nothing to stop, then just return `Task.CompletedTask`
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
private void OnStarted()
{
_logger.LogInformation("OnStarted has been called.");
// Perform post-startup activities here
}
private void OnStopping()
{
_logger.LogInformation("OnStopping has been called.");
// Perform on-stopping activities here
}
private void OnStopped()
{
_logger.LogInformation("OnStopped has been called.");
// Perform post-stopped activities here
}
}
Done!