打开基于ASP的控制台日志.. NET 5服务器类型



如果使用控制台启动应用程序,我想添加控制台日志记录。更多信息,请参阅登录ASP的官方文档。净5。如何判断应用程序是否在控制台下运行?

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
    if (We are running under the console)
    {
        loggerFactory.AddConsole();
    }
    app.Run(async (context) =>
    {
        var logger = loggerFactory.CreateLogger("LoggingSample.Startup");
        logger.LogInformation("Writing output.");
        await context.Response.WriteAsync("Hello World!");
    });
}

如果你想在IIS/IIS Express后面运行Kestrel时排除日志记录,一个选择是使用由HttpPlatformHandler本地模块添加的HTTP_PLATFORM_PORT环境变量:

if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("HTTP_PLATFORM_PORT"))) {
    // Only enable logging when running Kestrel or WebListener
    // without IIS acting as a reverse-proxy.
}

决定你的应用程序是否由WebListener托管,可以使用应用程序构建器的ServerFeatures属性完成:

if (app.ServerFeatures.Any(feature => feature.Key == typeof(WebListener))) {
    // Add server-specific features here.
}

最新更新