ASP.NET Core 2.0 webapi global configuration and log instance


Recently, some of the original webservces are converted into webapi, which is used directly ASP.Net Core 2.0 framework, in use, found with the original Different places, through the search has been slowly solved, record down for backup.

1、 Global configuration

stay The global change configuration is written in web.config , as shown below

<?xml version="1.0"?>
 <add name="conn" connectionString="Data Source=localhost;Initial Catalog=helloworld;Integrated Security=True"/>
 <add key="app_key" value="helloworld" />
 <add key="app_secret" value="1234567890abcdef" />

stay ASP.Net In core 2.0 webapi, it is no longer available web.config File, check some information, you can write the global variable configuration in the appsetting.json In the document, as follows:

 "connectionStrings": {
 "conn": "Data Source=localhost;Initial Catalog=helloworld;Integrated Security=True"
 "appSettings": {
 "app_key": "helloworld",
 "app_secret": "1234567890abcdef"

In this way, the global variable configuration can be referenced in the program.

use appSetting.json Global variables can be set more complex, specific methods can refer to the references.

2、 Log

before ASP.NET At that time, the logs were all recorded with NLog. Now that we have converted to core 2.0, we are ready to continue to use NLog. In use, we find that there are also differences between them.

First, get it in nuget NLog.Web.AspNetCore Bags,

Then the startup.cs The code of the file is modified

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
//Revised as
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

In the configure function, add the following statement:


Remember to refer to using in the header NLog.Web And using NLog.Extensions.Logging ;

Add a “web configuration file” named nlog.config The contents are as follows:

<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns=""
  <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}" />
 <logger name="*" level="Debug" writeTo="debugfile" />
  <logger name="*" level="Error" writeTo="errfile" />
 <logger name="*" minlevel="Trace" writeTo="logfile" />

Then you can start to call the log function in the program.

The demo codes of the two functions are 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()
  Configuration = builder.Build();
  string app_key = Configuration["appSettings:app_key"];
  string coon = Configuration["connectionStrings:conn"];
  log.Debug ("database connection is: + conn)";

The above article ASP.NET The core 2.0 webapi global configuration and log instance are all the contents shared by Xiaobian. I hope to give you a reference, and also hope that you can support developeppaer more.