ASP. NET Core 2.0 WebApi global configuration and log instance, corewebapi
Recently, ASP is directly used to convert some original webSerivce into WebApi. net Core 2.0 Framework, in use, found different from the original asp.net, through the search has been slowly resolved, record down the backup.
1. Global Configuration
In asp.net, global configuration change is written in web. config, as shown below:
<?xml version="1.0"?><configuration><connectionStrings> <add name="conn" connectionString="Data Source=localhost;Initial Catalog=helloworld;Integrated Security=True"/> </connectionStrings> <appSettings> <add key="app_key" value="helloworld" /> <add key="app_secret" value="1234567890abcdef" /> </appSettings></configuration>
In ASP. Net Core 2.0 WebApi, the web. config file is no longer available. After checking some information, you can write the global variable configuration in the appsetting. json file, as shown below:
{ "connectionStrings": { "conn": "Data Source=localhost;Initial Catalog=helloworld;Integrated Security=True" } "appSettings": { "app_key": "helloworld", "app_secret": "1234567890abcdef" }}
In this way, you can reference the global variable configuration in the program.
You can set global variables more complex by using deleetting. json. For specific methods, refer to the references below.
Ii. log recording
In the past ASP. NET, logs were recorded using Nlog. Now they are converted to Core 2.0, and they are also ready to continue using Nlog. in use, they are found to be different from the previous one.
First, obtain the NLog. Web. AspNetCore package in Nuget,
Modify the code of the startup. cs file.
Public void Configure (IApplicationBuilder app, IHostingEnvironment env) // change it to public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
Add the following statement to the Configure function:
loggerFactory.AddNLog();app.AddNLogWeb();loggerFactory.ConfigureNLog(“nlog.config”);
Remember to first reference using NLog. Web and using NLog. Extensions. Logging in the file header;
Add a "Web configuration file" named nlog. config with the following content:
<?xml version="1.0" encoding="utf-8"?><nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <targets> <target xsi:type="File" name="logfile" fileName="${basedir}/logs/${shortdate}.log" keepFileOpen="false" layout="${longdate}|${callsite:fileName=True}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="File" name="debugfile" fileName="${basedir}/logs/${shortdate}_debug.log" keepFileOpen="false" layout="${longdate}|${callsite:fileName=True}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="File" name="errfile" fileName="${basedir}/logs/${shortdate}_error.log" keepFileOpen="false" layout="${longdate}|${callsite:fileName=True}|${uppercase:${level}}|${message} ${exception}" /> </targets> <rules> <logger name="*" level="Debug" writeTo="debugfile" /> <logger name="*" level="Error" writeTo="errfile" /> <logger name="*" minlevel="Trace" writeTo="logfile" /> </rules></nlog>
Then you can call the log function in the program.
The DEMO code for the two functions is as follows:
Using System; using System. IO; using Microsoft. extensions. configuration; using NLog. extensions. logging; using NLog. web; public class Program {public static IConfigurationRoot Configuration {get; set;} public static NLog. logger log = NLog. logManager. getCurrentClassLogger (); public static void ConfigAndLog () {var builder = new ConfigurationBuilder (). setBasePath (Directory. getCurrentDirectory ()). addJsonFile ("deleteworkflow. json "); Configuration = builder. build (); string app_key = Configuration ["etettings: app_key"]; string coon = Configuration ["connectionStrings: conn"]; log. debug ("database connection:" + conn); return ;}}
The above ASP. NET Core 2.0 WebApi global configuration and log instance are all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the customer's home.