Tip: 此篇已加入.NET Core微服務基礎系列文章索引
一、Exceptionless極簡介紹
Exceptionless 是一個開源的實時的日志收集框架,它可以應用在基於 ASP.NET,ASP.NET Core,Web API,Web Forms,WPF,Console,ASP.NET MVC 等技術開發的應用程序中,並且提供了REST接口可以應用在 Javascript,Node.js 中。它將日志收集變得簡單易用並且不需要了解太多的相關技術細節及配置,對於微服務架構的應用程序來說,統一的日志收集系統的建立更是有必要。
二、Quick Start
2.1 官方創建一個賬號
2.2 創建項目
2.3 得到ApiKey
2.4 安裝Exceptionless.AspNetCore並進行配置
NuGet>Install-Package Exceptionless.AspNetCore
*.目前最新版本是4.3.2004
在你要進行Logging的項目(MVC,WebAPI等)中注冊APIKey,這里以ASP.NET Core WebAPI項目為例:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime) { ...... app.UseMvc(); // exceptionless app.UseExceptionless(Configuration["Exceptionless:ApiKey"]); // swagger ...... }
這里我將ApiKey配置到了json配置文件中:
"Exceptionless": { "ApiKey": "Your Api Key from Exceptionless server" }
2.5 簡單地封裝一個ExceptionlessLogger
(1)自定義一個ILogger接口
public interface ILogger { void Trace(string message, params string[] args); void Debug(string message, params string[] args); void Info(string message, params string[] args); void Warn(string message, params string[] args); void Error(string message, params string[] args); }
(2)實現ILogger接口:ExceptionlessLogger
public class ExceptionLessLogger : ILogger { /// <summary> /// Trace /// </summary> public void Trace(string message, params string[] tags) { ExceptionlessClient.Default.CreateLog(message, LogLevel.Trace).AddTags(tags).Submit(); } /// <summary> /// Debug /// </summary> public void Debug(string message, params string[] tags) { ExceptionlessClient.Default.CreateLog(message, LogLevel.Debug).AddTags(tags).Submit(); } /// <summary> /// Info /// </summary> public void Info(string message, params string[] tags) { ExceptionlessClient.Default.CreateLog(message, LogLevel.Info).AddTags(tags).Submit(); } /// <summary> /// Warn /// </summary> public void Warn(string message, params string[] tags) { ExceptionlessClient.Default.CreateLog(message, LogLevel.Warn).AddTags(tags).Submit(); } /// <summary> /// Error /// </summary> public void Error(string message, params string[] tags) { ExceptionlessClient.Default.CreateLog(message, LogLevel.Error).AddTags(tags).Submit(); } }
2.6 注入ExceptionlessLogger
public IServiceProvider ConfigureServices(IServiceCollection services) { // IoC - Logger services.AddSingleton<ILogger, ExceptionLessLogger>(); ...... }
2.7 在你想要Logging的地方調用
比如我們要記錄一個User登錄的日志:
public class LoginController : Controller { public ILogger Logger { get; } public LoginController(ILogger logger) { Logger = logger; } [HttpGet("{id}")] public string Get(int id) { Logger.Info($"User {id} Login Successfully. Time:{DateTime.Now.ToString()}", "Tag1", "Tag2"); return "Login Success."; } }
測試結果:
2.8 記錄你程序中的各種Exception
這里模擬一個空指針的異常,這里借助Exceptionless針對Exception類的擴展方法去進行寫異常信息。
[HttpGet] public string Get() { try { string str = null; str.ToString(); } catch (Exception ex) { ex.ToExceptionless().Submit(); } return "Unknown Error!"; }
測試結果:
2.9 Check你的日志與異常記錄
(1)Check 日志
在Log Messages 或 AllEvents菜單中選擇Dashboard,即可看到當前項目所有的Log Message了。(如果選擇的是AllEvents,可能還會包含其他類型的信息,比如Exception)
在最近的Log中可以看到我們剛剛的測試中記錄的一跳日志:
點擊超鏈接,即可進入詳細頁面:
Overview:可以看到一些項目和日志的基本信息,比如Event Type,Level以及標簽Tags
Environment:可以看到記錄日志所在的項目所處的一些軟硬件環境信息
下面是一些額外的信息,比如Framework Version以及Runtime Framework
通過對這些日志的查看和分析,我們可以方便地在一個地方對所有服務中的日志進行查看和分析。But,在線版本對項目和日志數量有限制,建議在生產環境使用本地部署版本,它是開源的。
(2)Check 異常
在Exception菜單下選擇Dashboard:
在最近的異常信息中找到剛剛記錄的:
同樣,通過超鏈接查看詳細信息:
Overview:可以看到這個異常的基本信息,比如Error Type以及Stack Trace,這些都是可以幫准我們快速定位錯誤的信息
Exception:如果基本信息不夠,那就查看詳情,你可能需要看看加載了哪些Modules
最后是Environment,跟Log的Environment差不多,這里就不再貼圖了。
三、本地部署
我們說到Exceptionless是一款強大的開源框架,那么我們可以下載下來根據需要進行獨立部署,可以不受一些用戶、項目、Event數量的限制。這里我暫時不會去做其獨立部署的實踐,但是園子里已經有很多的獨立部署實踐的分享了,有興趣的朋友可以看看下面幾篇:
依樂祝,《ASP.NET Core免費開源分布式日志收集框架Exceptionless安裝配置以及簡單實用》
花兒笑彎了腰,《Self Host 使用Exceptionless實時監控程序運行日志服務》
平凡網客,《Exceptionless 本地部署》
當然,官方的獨立部署Wiki也是一個絕佳的參考資料:https://github.com/exceptionless/Exceptionless/wiki/Self-Hosting
以下是測試環境的要求:
以下是Production環境的要求,我們可以看到在Production環境中,強烈推薦使用ELK的ElasticSearch,如果有不知道ELK的朋友也可以百度/Google一下,ELK也是我后續的學習計划。
四、小結
本篇主要簡單的介紹了一下開源的分布式日志框架Exceptionless,並通過兩個小例子介紹了如何快速的在ASP.NET Core中進行使用,最后通過在Exceptionless平台中Check我們在程序中記錄的日志/異常信息了解Exceptionless的強大。此外,通過引入園友和官方的Wiki文檔介紹了Exceptionless的另一種使用模式:本地部署,有興趣的朋友可以去看看,這里我就不再去實踐了(對我現階段而言,去做獨立部署的優先級不高)。本篇沒有過多的語言介紹,更多的是貼code以及貼圖片,因此不能算是很好的介紹文章,不過結合列出的參考資料應該可以對Exceptionless做個快速入門。
一些朋友問我后續的分享計划,這里小小透漏一下:Ocelot+IdentityServer的結合做統一驗證和授權,Ocelot+Butterfly的結合(目前Ocelot已集成Butterfly)做分布式追蹤,基於AppMetrics+InfluxDB+Grafana的性能監控,數據一致性(可能會使用幾個EventBus框架)初探,基於Apollo做配置中心,ASP.NET Core on Docker與K8S結合等等。等到POC初步研究后,可能還會去看看微軟的微服務架構官方高級版大Demo-eShopOnContainers(微軟有一本pdf大家可以去下載,點我下載)。這些計划可能需要花費我很多時間,不過我相信這樣的學習和實踐是值得的,也是值得分享的,如果你也有這樣的計划,那就一起加油吧!
參考資料
Exceptionless Github:https://github.com/exceptionless/Exceptionless
savorboard(楊曉東),《免費開源分布式系統日志收集框架 Exceptionless》
編程夢,《ExceptionLess新玩法 — 記日志》
編程夢,《ExceptionLess新玩法 -- 審計日志》
花兒笑彎了腰,《Self Host 使用Exceptionless實時監控程序運行日志服務》
平凡網客,《Exceptionless 本地部署》
依樂祝,《ASP.NET Core免費開源分布式日志收集框架Exceptionless安裝配置以及簡單實用》