ASP.NET Core 2.0 WebApi全局配置及日志實(shí)例
最新在將原來寫的一些webSerivce轉(zhuǎn)換為WebApi,直接就用了ASP.Net Core 2.0的框架,在使用中,發(fā)現(xiàn)的與原有的asp.net不同的地方,通過搜索已經(jīng)慢慢解決,記錄下來備用。
一、全局配置
在asp.net中,全局變更配置寫在web.config中,如下所示
<?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>
在ASP.Net Core 2.0 WebApi中,已經(jīng)沒有了web.config文件,查了一些資料,可以把全局變量配置寫在appsetting.json文件中,如下所示:
{ "connectionStrings": { "conn": "Data Source=localhost;Initial Catalog=helloworld;Integrated Security=True" } "appSettings": { "app_key": "helloworld", "app_secret": "1234567890abcdef" } }
這樣一來,在程序中就可以對(duì)全局變量配置進(jìn)行引用了。
使用appSetting.json,全局變量可以設(shè)置的更為復(fù)雜,具體的方法可以參考文后的參考文獻(xiàn)。
二、記錄日志
以前ASP.NET的時(shí)候,日志都是用Nlog進(jìn)行記錄,現(xiàn)在轉(zhuǎn)換到了Core 2.0,也準(zhǔn)備繼續(xù)使用Nlog,在使用中,發(fā)現(xiàn)和以前的有也所不同。
首先,在Nuget中獲取NLog.Web.AspNetCore包,
然后將startup.cs文件的代碼進(jìn)行修改
public void Configure(IApplicationBuilder app, IHostingEnvironment env) //修改為 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
并在Configure函數(shù)中,加上以下語句:
loggerFactory.AddNLog(); app.AddNLogWeb(); loggerFactory.ConfigureNLog(“nlog.config”);
記得要在文件頭先引用using NLog.Web和using NLog.Extensions.Logging;
增加一個(gè)"Web配置文件",文件名為nlog.config,內(nèi)容如下:
<?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>
然后在程序中就可以開始調(diào)用日志功能了。
二個(gè)功能的DEMO代碼如下:
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("appsettings.json"); Configuration = builder.Build(); string app_key = Configuration["appSettings:app_key"]; string coon = Configuration["connectionStrings:conn"]; log.Debug("數(shù)據(jù)庫連接為:" + conn); return; } }
以上這篇ASP.NET Core 2.0 WebApi全局配置及日志實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
mysql安裝后.net程序運(yùn)行出錯(cuò)的解決方法
這篇文章主要給大家介紹了關(guān)于mysql安裝后.net程序運(yùn)行出錯(cuò)的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02使用?HttpReports?監(jiān)控?.NET?Core?應(yīng)用程序的方法
這篇文章主要介紹了使用?HttpReports?監(jiān)控?.NET?Core?應(yīng)用程序的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03asp.net(c#)動(dòng)態(tài)修改webservice的地址和端口(動(dòng)態(tài)修改配置文件)
這個(gè)問題其實(shí)并沒有我想像的那個(gè)復(fù)雜,我們都知道怎么直接修改吧,那就是修改WebConfig文件的配置節(jié)2012-12-12asp.net Accee數(shù)據(jù)庫連接不穩(wěn)定解決方案
我最最進(jìn)做了個(gè)網(wǎng)站,我是用VS2005 + Access 數(shù)據(jù)庫連接,可以我在連接的時(shí)候總是出現(xiàn)連接不穩(wěn)定的情況,同樣的代碼,一個(gè)字也沒改,就是連接不上,但是有的又連接的上。真是郁悶,真是弄死人。2009-07-07asp.net下判斷用戶什么時(shí)候離開,以什么方式離開
asp.net下判斷用戶什么時(shí)候離開,以什么方式離開...2007-03-03