ASP.NET Core 2.0 IHostEnvironment和IApplicationLifetime介绍

IHostEnvironment获取程序信息

public void Configure(IApplicationBuilder app, IHostingEnvironment env){ app.Run(async (context) => { await context.Response.WriteAsync($"ApplicationName:{env.ApplicationName}"); await context.Response.WriteAsync($"ContentRootPath:{env.ContentRootPath}"); await context.Response.WriteAsync($"WebRootPath:{env.WebRootPath}"); await context.Response.WriteAsync($"是否开发环境:{env.IsDevelopment()}"); });}

技术分享图片

IApplicationLifetime站点启动或关闭时的监控

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

 

相关文章