国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET Core 2.0 WebApi全局配置及日志實例

2024-07-10 13:32:49
字體:
來源:轉載
供稿:網友

最新在將原來寫的一些webSerivce轉換為WebApi,直接就用了ASP.Net Core 2.0的框架,在使用中,發現的與原有的asp.net不同的地方,通過搜索已經慢慢解決,記錄下來備用。

一、全局配置

在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中,已經沒有了web.config文件,查了一些資料,可以把全局變量配置寫在appsetting.json文件中,如下所示:

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

這樣一來,在程序中就可以對全局變量配置進行引用了。

使用appSetting.json,全局變量可以設置的更為復雜,具體的方法可以參考文后的參考文獻。

二、記錄日志

以前ASP.NET的時候,日志都是用Nlog進行記錄,現在轉換到了Core 2.0,也準備繼續使用Nlog,在使用中,發現和以前的有也所不同。

首先,在Nuget中獲取NLog.Web.AspNetCore包,

然后將startup.cs文件的代碼進行修改

public void Configure(IApplicationBuilder app, IHostingEnvironment env)//修改為public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

并在Configure函數中,加上以下語句:

loggerFactory.AddNLog();app.AddNLogWeb();loggerFactory.ConfigureNLog(“nlog.config”);

記得要在文件頭先引用using NLog.Web和using NLog.Extensions.Logging;

增加一個"Web配置文件",文件名為nlog.config,內容如下:

<?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>

然后在程序中就可以開始調用日志功能了。

二個功能的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("數據庫連接為:" + conn);  return; }}

以上這篇ASP.NET Core 2.0 WebApi全局配置及日志實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荥经县| 府谷县| 开化县| 长垣县| 龙泉市| 东光县| 和硕县| 新乐市| 泽库县| 霞浦县| 乐业县| 奇台县| 兰州市| 竹北市| 稷山县| 苍南县| 凤台县| 云和县| 丁青县| 蕲春县| 梧州市| 上饶市| 体育| 马鞍山市| 天津市| 淮阳县| 峨眉山市| 松潘县| 临西县| 牙克石市| 仙游县| 女性| 观塘区| 山东| 乌兰察布市| 黑龙江省| 阿图什市| 诸城市| 那坡县| 金堂县| 沿河|