With ASP.NET Core 3.0, the ability to inject an ILogger or ILoggerFactory into the Startup class has been removed, so we can no longer do this:
I understand why this has been done, because creating a temporary DI container just for the startup process adds a lot of complexity and potential for errors.
ConfigureServices method (and the new
ConfigureContainer method added in 3.0) does quite a bit of work, including loading extension assemblies; I want to be able to log during that time.
I also want to make sure I only pass around the
ILogger from the
Microsoft.Extensions.Logging namespace to other objects used at startup.
I use NLog for my logging (https://nlog-project.org/). In my Program’s
Main method I configure it like so:
You can grab details on how to create an nlog.config file from the NLog docs, I won’t go into it here.
Then, in my Startup class, I can create the
NLogLoggerProvider class (this is sort of a factory for creating the Microsoft ILogger instances), and from that I can get my logger instance:
Hey presto, logging in the Startup class. Note that we are obviously outside the entire DI system for this logging (which is sort of the point).