.netcore监控应用开始结束

.netcore中ApplicationLifetime可以监控应用开始结束

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,IApplicationLifetime appLife)
{
            appLife.ApplicationStarted.Register(()=>
            {
                Console.WriteLine("start");
            });
            appLife.ApplicationStopping.Register(()=>
            {
                Console.WriteLine("stopping");
            });
            appLife.ApplicationStopped.Register(() =>
            {
                Console.WriteLine("stopped");
            });
}

有了这个方法之后就可以在进程结束时进行一些操作,比如下线eureka中注册的服务

appLife.ApplicationStopped.Register(()=> {
                DiscoveryManager.Instance.Client.ShutdownAsync();
            });

你可能感兴趣的:(.netcore)