Default configuration and logging are now set up in the CreateDefaultBuilder method, instead of added into the Configure method of the Startup class. This video introduces the changes and shows how to add additional configuration or logging providers with
- As you just saw Configuration is now built into the CreateDefaultBuilder method, and in our change is that an Iconfiguration object is injected into Startup.cs through the DI container. This allows you to use an Iconfiguration object as an injectable instance into anything within your program. And you can also add additional configuration using the WebHostBuilder. So let's look at all this in code.
So here in my main method I am creating the base builder, and that's just a helper method I have down here that calls CreateDefaultBuilder and UseStartup, but doesn't call build yet. And then if I want to add in another configuration, for example, my key vault, I have an extension method here that's called UseMyKeyVault and UseMyKeyVault calls the configure app configuration method on the builder object, which exposes the context and the config.
The context allows me to get things like the run-time environment, so context.hostingenvironment. is development, so I can change how things work based on the environment and just like we saw in 1.1 where we used a key vault we have to build a configuration so we can access the settings in the JSON file, so that's where we call config.build. Right here in ConfigureAppConfiguration and then we can configure the azure key vault.
Let's take a look at the startup class, and this is where the configuration would have been created in 1.0 and 1.1 asp.netcoreprojects, but now IConfiguration is just injected in and then setting the configuration property which is an IConfiguration interface as opposed to an IConfiguration root which is was in prior versions, but it greatly simplifies the ability to access the configuration from anywhere in your application.
So you can see that the new paradigm greatly simplifies not only accessing, but also configuring your application. Logging configuration is also greatly simplified through the CreateDefaultBuilder, but there's a couple new items. First and foremost, that you can now configure log filters through the app settings .JSON, or through code, and additional logging providers just like Configuration can be added using the WebHostBuilder.
So we go back into our code, we can see that the standard logging on 100 and 101 for console and debug are added in, so these were removed from the configure method, and that we're adding the configuration by getting the logging section of the app settings file. So if we look at the appsettings.Development we see here's the logging section, and then each provider gets its' own section, and that configures the filtering for us.
And then there's a default log level as well, in case you add a provider that isn't configured with its' own section. I go back into my extension methods, and if we want to enable as your app service logging as we did in 1.1, this is also very simple. We just call builder.UseAzureAppServices and then ConfigureLogging adding in the azure web app diagnostics.
All the packages needed for this are included in the .allmeta package, so there's no additional packages to add in to get the azure app service logging feature turned on. Like the configuration, the new CreatesDefaultBuilder and the new builder options make it much simpler and cleaner to configure logging in your application.
- Running and debugging ASP.NET Core applications
- Pros and cons of migrating existing applications to ASP.NET Core.
- Built-in dependency injection
- Environment awareness and app configuration
- Web host configuration and SSL
- View components invoked as tag helpers
- Configuration and logging
- Using Razor Pages