Using DI, Logging, and Configuration in a .NET Core Console App

Yesterday set about building a small, Core Console Application that does some data access. A very early challenge for me was how to store my connection string. I’m used to ASP.NET Core work, where the configuration is set up in the Startup class, but a plain old console app starts out none of that sweet infrastructure.

I set about frenzied Googling for anything, and it was a good day for me because I found Chad Ramos‘ article titled:

Dependency Injection, Logging and Configuration In A .NET Core Console Application

It is a very simple, three-step tutorial, very readable and with a single, code example used across all three steps. Chad includes a link to a very readable and neat example if you want more than his code excerpts in the article. It may be easier to just use his code as a startup template for such projects.

It has one small problem in that you can’t e.g. reference a class and call a method from another project because the startup code won’t have run, but this is very rare with console apps.

I am so impressed I’m going to make a project template for Core console apps ready to go, with DI, Configuration, and Logging.