-
Notifications
You must be signed in to change notification settings - Fork 165
Hosting Lifetime Startup Messages
AspNetCore v2 applications will print the following magic messages directly to Console:
Now listening on: https://[::]:443
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
AspNetCore v3 applications will print the following magic messages to Microsoft.Hosting.Lifetime
-Logger:
info: Microsoft.Hosting.Lifetime[0]
Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
The hosting application (Visual Studio / IIS / Docker / Azure Container) will hook into the console output, and will fail to recognize the application as running if this output is not generated (Long delays or nothing happening). Ex. Visual Studio Code uses this serverReadyAction
filter by default:
"serverReadyAction": {
"action": "openExternally",
"pattern": "^\\s*Now listening on:\\s+(https?://\\S+)"
}
Because ASP.NET Core 3 now uses the MEL-ILogger to write these messages, then it is important to NOT discard the output from the Microsoft.Hosting.Lifetime
-Logger (or changing the expected output format).
The Microsoft Extension Logging is by default very chatty even on Info-Level, so many setup filters to only output warnings (or higher severity) from Microsoft.*
-Loggers. This will also discard the important startup messages from the Microsoft.Hosting.Lifetime
-Logger.
Example of appsettings.json
with special filter rule for Microsoft.Hosting.Lifetime
:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Extending the NLog.config
from the NLog ASP.NET Core v3 Tutorial, so the NLog Console will output the Lifetime Startup Messages:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
<target xsi:type="Console" name="lifetimeConsole"
layout="${level:truncate=4:lowercase=true}: ${logger}[0]${newline} ${message}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Output hosting lifetime messages to make Docker / Visual Studio happy -->
<logger name="Microsoft.Hosting.Lifetime" level="Info" writeTo="lifetimeConsole, ownFile-web" final="true" />
<!--Skip non-critical Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" maxlevel="Info" final="true" /> <!-- BlackHole without writeTo -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
Notice that NLog.Extensions.Logging v1.7.5 introduces the layout-renderer ${MicrosoftConsoleLayout}. It can be used like this: <target xsi:type="Console" name="lifetimeConsole" layout="${MicrosoftConsoleLayout}" />
See also: NLog Console vs. AddConsole.
AspNetCore v2.1 WebHostBuilder can be configured to disable the direct output to Console:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseSetting(WebHostDefaults.SuppressStatusMessagesKey, "True") // add this line
.UseStartup<Startup>();
This is similar to adding this environment variable: ASPNETCORE_SUPPRESSSTATUSMESSAGES = TRUE
See also Suppressing the startup and shutdown messages in ASP.NET Core
AspNetCore v3 HostBuilder can be configured to disable output to Microsoft.Hosting.Lifetime
-Logger:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// ... other configuration
services.Configure<ConsoleLifetimeOptions>(opts => opts.SuppressStatusMessages = true);
}
}
This will also prevent NLog from receiving the output.
See also ASP.NET Core 3.0: structured logging for startup messages
For general docs, check https://github.com/NLog/NLog/wiki