Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure the .NET Core development log

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to configure the NET Core development log, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Developers who are familiar with ASP.NET must be familiar with web.config files. In an ASP.NET environment, you usually operate in this file if you want to add configuration parameters. Among them, nothing is more commonly used than AppSettings and ConnectionStrings. To get the configuration information in the file in the code, ConfigurationManager is an essential assembly that needs to be introduced.

In the age of ASP.NET Core, however, the way configurations are stored and read has changed.

If you know anything about the ASP.NET Core project, you should have seen the appsettings.json file. Let's start with how the JSON file is configured to explain how configuration information is read in ASP.NET Core.

Suppose you have a preset appsettings.json file:

Reading in code can be done in the following ways:

First, instantiate a ConfigurationBuilder object, and then set the underlying path.

The operation of SetBasePath is actually to set the value of FileProvider in the attribute dictionary of ConfigurationBuilder.

Then add the JSON file.

A JsonConfigurationSource object has been added to ConfigurationBuilder.

Finally, execute the Build method of ConfigurationBuilder, and you can get the Configuration object that holds the configuration information.

To sum up the code in the example, the operation of obtaining configuration information is actually divided into two steps:

Generate Configuration object

Key values get information from the Configuration object

The steps to generate Configuration objects must have at least three basic steps.

Generate ConfigurationBuilder object

Add ConfigurationSource object

Create a Configuration object

If you look at the code that creates the Configuration object, you will find that the internal utilization is actually the ConfigurationProvider object created in ConfigurationSource.

If you look at the IConfiguratonSource interface, there is only one Build method.

The resulting Configuration object, the ConfigurationRoot, contains all the ConfigurationProvider, indicating that the configuration information is provided by these ConfigurationProvider.

Trace to the constructor of type ConfigurationRoot, and sure enough, all ConfigurationProvider is loaded when it generates the object.

For example, in JsonConfigurationProvider:

Through the JSON parser, the configuration information of the JSON file is read into the Data property of ConfigurationProvider. This property is used to save all configuration information.

Once you have the ConfigurationRoot object, getting configuration information is easy. Iterate through each ConfigurationProvider to get the first data that matches the key value.

The ConfigurationProvider object gets the value of the configuration from the Data property.

Public virtual bool TryGet (string key, out string value) = > Data.TryGetValue (key, out value)

You can see the Configuration ["wizards:0:Name"] in the initial example, because in Load files, the way it is stored is to use: as a delimiter to serve as the key value of a nested object.

You can also write in another way, binding the configuration information as an object.

Define the object type first:

Re-bind the object:

Writing has become a common way for objects to invoke properties, but the result is the same.

In addition to using JSON files to store configuration information, ASP.NET Core also supports INI and XML files. Of course, when there are other types of files, you can also load the configuration file by implementing the IConfigurationSource interface and inheriting the ConfigurationProvider class to establish a custom ConfigrationProvider object.

As for ways other than files, ASP.NET Core also provides a lot.

Command line, AddCommandLine

Environment variable, AddEnvironmentVariables

Memory, AddInMemoryCollection

User Secret, AddUserSecrets

AzureKeyVault, AddAzureKeyVault

The method of storing and reading configuration depends on the actual scenario. ASP.NET Core has opened the entry for configuration, and any access method is feasible in theory. In practice, developers need to try and explore constantly.

Thank you for reading this article carefully. I hope the article "how to configure the .NET Core Development Log" shared by the editor will be helpful to you. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report