Popular blog tags

How to Prevent Kestrel from logging to Console in ASP.NET Core 2.2


I recently had the need to start a web server from the command line. Authentication, you see, is a complicated process and sometimes requires you to open a browser to complete that process.

Active Directory, requires you to have a Return Url which isn’t really possible from a command line. Or is it?

With .NET Core, you can easily setup a web server that listens to localhost. Active Directory, can be configured to redirect to localhost. Problem solved, right?

Not exactly. I’m partial to not outputing anything to useful when creating a CLI tool. I want kestrel to not output anything in the console. We have other ways to make an EXE talk, you see.

So how do you get Kestrel to go silent?

The first solution led me to change how I built my WebHost instance by adding .ConfigureLogging(...) and using Microsoft.Extensions.Logging. It is the perfect solution when you want to tell kestrel to not output anything from individual requests.

However, Kestrel will still output that it started a web server and on which ports it’s listening to. Let’s remove that too, shall we?


.SuppressStatusMessages(true) // <== this removes the "web server started on port XXXX" message
.ConfigureLogging((context, logging) =>
// this removes the logging from all providers (mostly console)
//snip: providers where I want the logging to happen
.UseKestrel(options =>
options.ListenLocalhost(Common.Port); // port that is hardcoded somewhere 🤷‍♂️