我想将控制台侦听器添加到我的asp.net核心应用程序中。此示例运行良好,但我正在寻找一种在操作员键入命令时如何临时禁用记录器垃圾邮件的想法。在Linux中实现一些tty系统会很好。
[Startup.cs]
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceScopeFactory scopeFac, ILogger<Startup> logger)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
// Console listener to manage migrations and something else
Task.Run(async () => {
while (true)
{
var cmd = Console.ReadLine();
logger.LogInformation(cmd);
if (cmd == "migratedb")
{
using (var scope = scopeFac.CreateScope())
{
var dbContext = scope.ServiceProvider.GetService(typeof(AppDbContext)) as AppDbContext;
await dbContext.Database.MigrateAsync();
}
}
}
});
}