我在哪里可以记录ASP.NET Core应用程序的启动/停止/错误事件?

时间:2017-01-16 11:39:22

标签: c# asp.net asp.net-core

在旧的ASP.NET中,在Global.asax.cs类中,我会在应用程序启动时停止,停止并抛出未处理的异常:

  • Application_Start()
  • Application_End()
  • Application_Error()

如何在ASP.NET Core中执行相同的操作?它有一个Startup类,但它用于配置。

我在哪里挂钩应用的开始/停止/错误事件?

4 个答案:

答案 0 :(得分:18)

您需要使用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; }

IApplicationLifetime的实例可以用Configure方法获得。还可以在这里添加ILoggerFactory:

public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime, ILoggerFactory loggerFactory)
{
    // use applicationLifetime
}

拥有ILoggerFactory,您可以create ILogger的实例:

var logger = loggerFactory.CreateLogger("StartupLogger"); 

所以你只需要在Startup类中创建一个属性来持久保存ILogger(或ILoggerFactory的实例,如果你想为不同的事件创建不同的ligger实例)。总结一下:

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;
    }
}

答案 1 :(得分:5)

请参阅CaptureStartupErrors以及可帮助您查找问题的方法#wrapper { width: 100%; height: 100%; overflow:hidden; display: flex; flex-direction: row; } #sidebar { flex-basis: 250px; } #content { overflow: hidden; flex: 1; } #sidecontent { flex-basis: 250px; }

当在localhost上运行完美但在Azure中失败时,这尤其方便。

以下是我对NetCore Web Apps的常用配置:

.CaptureStartupErrors(true)

在Azure App Service中,您可以在Kudu Tools public static IWebHost BuildWebHost(string[] args) => WebHost .CreateDefaultBuilder(args) .CaptureStartupErrors(true) .UseKestrel() .UseIISIntegration() .UseStartup<Startup>() .UseAzureAppServices() .Build();

中的日志流中查找日志

答案 2 :(得分:1)

现在不再使用在最高答案中建议的Microsoft.AspNetCore.Hosting.IApplicationLifetime

[Obsolete("This type is obsolete and will be removed in a future version. The recommended alternative is Microsoft.Extensions.Hosting.IHostApplicationLifetime.", false)]
public interface IApplicationLifetime

在应用程序关闭时,使用IHostApplicationLifetime触发回调。

在某处添加:

public static async Task WaitForShutdownAsync(this IHost host)
{
    // Get the lifetime object from the DI container
    var applicationLifetime = host.Services.GetService<IHostApplicationLifetime>();

    // Create a new TaskCompletionSource called waitForStop
    var waitForStop = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);

    // Register a callback with the ApplicationStopping cancellation token
    applicationLifetime.ApplicationStopping.Register(obj =>
    {
        var tcs = (TaskCompletionSource<object>)obj;

        //PUT YOUR CODE HERE 

        // When the application stopping event is fired, set 
        // the result for the waitForStop task, completing it
        tcs.TrySetResult(null);
    }, waitForStop);

    // Await the Task. This will block until ApplicationStopping is triggered,
    // and TrySetResult(null) is called
    await waitForStop.Task;

    // We're shutting down, so call StopAsync on IHost
    await host.StopAsync();
}

然后我在Program.cs中使用它:

var host = CreateHostBuilder(args).Build();
host.WaitForShutdownAsync();

其他回调相同。 您可以找到here

的更多信息

如果我错过了什么,请告诉我

答案 3 :(得分:1)

我不喜欢@neustart47的答案,因为它不必要地复杂,但是他认为IApplicationLifetime已经过时了。

取自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
    }
}

完成!