.NETCore IIS应用程序池事件监听
在 Startup.cs 中 Configure 方法,
增加一个参数:IHostApplicationLifetime hostApplicationLifetime
C# 全选
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime hostApplicationLifetime)
C# 全选
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime hostApplicationLifetime)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
hostApplicationLifetime.ApplicationStarted.Register(() => { Logs.Log.Info("应用程序启动后"); });
hostApplicationLifetime.ApplicationStopping.Register(() => { Logs.Log.Info("应用程序退出前"); });
hostApplicationLifetime.ApplicationStopped.Register(() =>
{
Logs.Log.Info("应用程序退出后");
});
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
// 配置404
app.UseStatusCodePagesWithReExecute("/error/{0}");
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
});
}
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
post 管理员