asp.net core系列 73 Exceptionless+Nlog以及Apollo介紹
一. 介紹
在一上篇中介紹了Exceptionless的基本使用,這篇主要講Exceptionless結合Nlog的實現雙重日誌記錄,包括Exceptionles的UI視覺化日誌以及Nlog的txt檔案日誌。再是從Apollo配置中心讀取配置檔案,當系統越龐大越多時,需要配置的引數也越來越多,可以通過使用Apollo配置中心來統一管理,例如:配置資料庫連線地址、Exceptionless的對應專案的apikey值,redis連線地址等等所有可配置的引數。
1.1 asp.net core中Apollo配置
開啟asp.net core 專案,刪除appsettings.json檔案預設內容,在新增配置如下所示:
{ "apollo": { "AppId": "100001", "MetaServer": "http://192.168.0.100:8080/", "Env": "Dev", "Meta": { "DEV": "http://192.168.0.100:8080/", "FAT": "http://192.168.0.100:8080/", "UAT": "http://192.168.0.100:8080/", "PRO": "http://192.168.0.100:8080/" } } }
appsettings.json配置對應的Apollo客戶端配置中心如下,這裡埠8070是Apollo客戶端配置介面。埠8080是.net core程式讀取Apollo配置地址。Apollo配置中引數都是以key-value的形式儲存。
下面是讀取Apollo配置檔案的關鍵程式碼:
安裝包如下:
Install-Package Microsoft.Extensions.Configuration -Version 2.2.0 Install-Package Com.Ctrip.Framework.Apollo.Configuration -Version 2.0.3
private static IConfigurationRoot _root = null; /// <summary> /// 獲取Apollo的config /// </summary> /// <returns></returns> public static IConfigurationRoot GetRoot() { if (_root != null) { return _root; } //先獲取appsettings.json的配置 var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); //連線Apollo string appId = config.GetSection("apollo").GetSection("AppId").Value; string metaServer = config.GetSection("apollo").GetSection("MetaServer").Value; var configuration = new ConfigurationBuilder() .AddApollo(appId, metaServer) // .AddDefault(ConfigFileFormat.Xml) // .AddDefault(ConfigFileFormat.Json) // .AddDefault(ConfigFileFormat.Yml) // .AddDefault(ConfigFileFormat.Yaml) .AddDefault().AddNamespace("application") .Build(); _root = configuration; return _root; }
注意:如果變數configuration 中沒有讀取到Apollo引數值,可以從configuration 物件的引數中查詢返回的異常資訊。如果讀取成功會快取一份檔案到本地,如下所示:
//下面是從Apollo(AppId:100001)的配置中心獲取Key為“ApiKey”的value值: string apiKey = GetRoot().GetSection("ApiKey").Value;
關於Apollo更多資料,包括Apollo服務端部署,參考官方文件:https://github.com/ctripcorp/apollo
1.2 Nlog結合Exceptionles
安裝包如下:
Install-Package Exceptionless.NLog Install-Package NLog.Web.AspNetCore
在Nlog的基礎上,結合Exceptionles,關鍵程式碼如下
(也可嘗試通過配置檔案實現 https://github.com/exceptionless/Exceptionless.Net/tree/master/src/Platforms/Exceptionless.NLog):
/// <summary> /// 返回Nlog.Logger /// </summary> /// <returns></returns> public Logger GetExceptionlessLogger() { var config = new LoggingConfiguration(); var exceptionlessTarget = new ExceptionlessTarget(); //讀取Apploo的Exceptionless配置引數 string apiKey = ConfigHelper.GetRoot().GetSection("ApiKey").Value; string serverUrl = ConfigHelper.GetRoot().GetSection("ServerUrl")?.Value; exceptionlessTarget.ApiKey = apiKey; exceptionlessTarget.ServerUrl = serverUrl; exceptionlessTarget.Layout = "${longdate} | ${callsite} | ${level} | ${message}"; exceptionlessTarget.Name = "exceptionless"; //新增exceptionless的Target物件 config.AddTarget("exceptionless", exceptionlessTarget); config.LoggingRules.Add(new LoggingRule("*", global::NLog.LogLevel.Error, exceptionlessTarget)); LogManager.Configuration = config; return LogManager.GetCurrentClassLogger(); } /// <summary> /// Nlog.Logger物件 /// </summary> private Logger _logger { get { return N.NLogBuilder.ConfigureNLog("Config\\NLog.config").GetCurrentClassLogger(); } } /// <summary> /// 帶有Exceptionless的Nlog.Logger物件 /// </summary> private Logger _exceptionlessLogger { get { ExceptionlessHelper helper = new ExceptionlessHelper(); return helper.GetExceptionlessLogger(); } }
//記錄錯誤日誌 public void Error<T>(IFormatProvider formatProvider, T value) { _logger.Error(formatProvider, value); _exceptionlessLogger.Error(formatProvider, value); }
&n