ASP.NET Core 2.0 IHostEnvironment和IApplicationLifetime介紹
阿新 • • 發佈:2018-05-29
pat onstop cat clas alt 監控 gis 開發 class
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"); }); }
ASP.NET Core 2.0 IHostEnvironment和IApplicationLifetime介紹