Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Hosting Lifetime Startup Messages

Rolf Kristensen edited this pageNov 18, 2024 ·14 revisions

Hosting Lifetime Startup Messages

AspNetCore v2 applications will print the following magic messages directly to Console:

Now listening on: https://[::]:443Now listening on: http://[::]:80Application started. Press Ctrl+C to shut down.

AspNetCore v3 applications will print the following magic messages toMicrosoft.Hosting.Lifetime-Logger:

info: Microsoft.Hosting.Lifetime[0]      Now listening on: https://localhost:5001info: Microsoft.Hosting.Lifetime[0]      Now listening on: http://localhost:5000info: 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 thisserverReadyAction 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 theMicrosoft.Hosting.Lifetime-Logger (or changing the expected output format).

Setup Microsoft Extension Logging Filter

The Microsoft Extension Logging is by default very chatty even on Info-Level, so many setup filters to only output warnings (or higher severity) fromMicrosoft.*-Loggers. This will also discard the important startup messages from theMicrosoft.Hosting.Lifetime-Logger.

Example ofappsettings.json with special filter rule forMicrosoft.Hosting.Lifetime:

{"Logging": {"IncludeScopes":false,"LogLevel": {"Default":"Trace","Microsoft":"Warning","Microsoft.Hosting.Lifetime":"Information"    }  },"AllowedHosts":"*"}

Setup NLog Console Logger in AspNetCore v3

Extending theNLog.config from theNLog ASP.NET Core v3 Tutorial, so the NLog Console will output the Lifetime Startup Messages:

<?xml version="1.0" encoding="utf-8" ?><nlogxmlns="http://www.nlog-project.org/schemas/NLog.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><!-- enable asp.net core layout renderers-->  <extensions>    <addassembly="NLog.Web.AspNetCore"/>  </extensions><!-- the targets to write to-->  <targets><!-- write logs to file-->    <targetxsi: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-->    <targetxsi: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}" />    <targetxsi: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-->    <loggername="*"minlevel="Trace"writeTo="allfile" /><!--Output hosting lifetime messages to make Docker / Visual Studio happy-->    <loggername="Microsoft.Hosting.Lifetime"level="Info"writeTo="lifetimeConsole, ownFile-web"final="true" /><!--Skip non-critical Microsoft logs and so log only own logs-->    <loggername="Microsoft.*"maxlevel="Info"final="true" /><!-- BlackHole without writeTo-->    <loggername="*"minlevel="Trace"writeTo="ownFile-web" />  </rules></nlog>

Notice thatNLog.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.

Suppressing Startup Messages in AspNetCore v2.1

AspNetCore v2.1 WebHostBuilder can be configured to disable the direct output to Console:

publicstaticIWebHostBuilderCreateWebHostBuilder(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 alsoSuppressing the startup and shutdown messages in ASP.NET Core

Suppressing Startup Messages in AspNetCore v3

AspNetCore v3 HostBuilder can be configured to disable output toMicrosoft.Hosting.Lifetime-Logger:

publicclassStartup{publicvoidConfigureServices(IServiceCollectionservices){// ... other configurationservices.Configure<ConsoleLifetimeOptions>(opts=>opts.SuppressStatusMessages=true);}}

This will also prevent NLog from receiving the output.

See alsoASP.NET Core 3.0: structured logging for startup messages

For general docs, checkhttps://github.com/NLog/NLog/wiki

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp