開源日志框架Exceptionless使用教程
Exceptionless是一款日志記錄框架,它開源、免費、提供管理界面、易于安裝和使用。ExceptionLess底層采用ElasticSearch作為日志存儲,提供了快速、豐富的查詢API,方便我們進行系統(tǒng)集成。本文將介紹ExceptionLess的常見用法。
安裝ExceptionLess
在ExceptionLess官網(wǎng)提供了基于Docker的私有化部署方式,我們可以按照官網(wǎng)的方式進行測試環(huán)境的安裝。
- 在官網(wǎng)github中下載最新的release包,地址:https://github.com/exceptionless/Exceptionless/releases
- 解壓縮,然后進入解壓縮的目錄,執(zhí)行
docker-compose up -d命令在后臺啟動多個容器,當執(zhí)行完成后,Exceptionless已經(jīng)在本地運行起來了。我們可以在Kitematic中查看運行中的容器 - 按照官網(wǎng)的說明,5000端口是登陸頁面,但實際情況是5000是API,5100才是登陸頁面,因此我們打開http://localhost:5100進入登陸頁面。注意:此處可能跟版本有關(guān),在使用時查看docker的端口映射。
通過以上步驟,就在本地搭建好了測試環(huán)境。我們可以看到啟動的總共啟動了6個容器,分別是redis、elasticsearch、kibana、Exceptionless Job、Exceptionless Api、Excetpionless UI。
快速上手
搭建好測試環(huán)境后,首先訪問Exceptionless UI來創(chuàng)建用戶、組織和項目。然后,當項目創(chuàng)建完成之后,Exceptionless 會跳轉(zhuǎn)到客戶端配置頁面,來指引我們?nèi)绾问褂肊xceptionless客戶端。我們可以選擇自己需要用到的客戶端,通過頁面的指引完成客戶端配置。
引導(dǎo)頁面如下:

按照這種方式我們可以完成.Net平臺項目、JS項目的配置。
客戶端API:https://github.com/exceptionless/Exceptionless.Net/wiki
配置好以后,我們就可以記錄日志了,例如(代碼來源于官網(wǎng)):
// Import the exceptionless namespace.
using Exceptionless;
// Submit logs
ExceptionlessClient.Default.SubmitLog("Logging made easy");
// You can also specify the log source and log level.
// We recommend specifying one of the following log levels: Trace, Debug, Info, Warn, Error
ExceptionlessClient.Default.SubmitLog(typeof(Program).FullName, "This is so easy", "Info");
ExceptionlessClient.Default.CreateLog(typeof(Program).FullName, "This is so easy", "Info").AddTags("Exceptionless").Submit();
// Submit feature usages
ExceptionlessClient.Default.SubmitFeatureUsage("MyFeature");
ExceptionlessClient.Default.CreateFeatureUsage("MyFeature").AddTags("Exceptionless").Submit();
// Submit a 404
ExceptionlessClient.Default.SubmitNotFound("/somepage");
ExceptionlessClient.Default.CreateNotFound("/somepage").AddTags("Exceptionless").Submit();
// Submit a custom event type
ExceptionlessClient.Default.SubmitEvent(new Event { Message = "Low Fuel", Type = "racecar", Source = "Fuel System" });
功能介紹
Exceptionless中的事件有以下幾種類型:
- 日志消息:記錄的日志,可以是任何文本內(nèi)容
- 特性使用:功能使用量的記錄,例如接口調(diào)用情況等
- 異常情況:記錄異常的信息
- 失效鏈接:當被訪問的頁面不存在時進行記錄
除了記錄內(nèi)容外,Exceptionless還支持對事件添加標簽、附加數(shù)據(jù)、用戶描述等操作,例如(代碼來源于官網(wǎng)):
try {
throw new ApplicationException("Unable to create order from quote.");
} catch (Exception ex) {
ex.ToExceptionless()
// Set the reference id of the event so we can search for it later (reference:id).
// This will automatically be populated if you call ExceptionlessClient.Default.Configuration.UseReferenceIds();
.SetReferenceId(Guid.NewGuid().ToString("N"))
// Add the order object but exclude the credit number property.
.AddObject(order, "Order", excludedPropertyNames: new [] { "CreditCardNumber" }, maxDepth: 2)
// Set the quote number.
.SetProperty("Quote", 123)
// Add an order tag.
.AddTags("Order")
// Mark critical.
.MarkAsCritical()
// Set the coordinates of the end user.
.SetGeo(43.595089, -88.444602)
// Set the user id that is in our system and provide a friendly name.
.SetUserIdentity(user.Id, user.FullName)
// Set the users description of the error.
.SetUserDescription(user.EmailAddress, "I tried creating an order from my saved quote.")
// Submit the event.
.Submit();
}
NLog、Log4net集成
官方支持NLog、Log4net集成的支持,只需要添加相應(yīng)的日志組件的配置文件即可。以Log4net為例:
首先添加程序集的支持:
Install-Package Exceptionless.Log4net
然后在log4net的配置文件中進行配置(代碼來源于官網(wǎng)):
<log4net>
<appender name="exceptionless" type="Exceptionless.Log4net.ExceptionlessAppender,Exceptionless.Log4net">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="exceptionless"/>
</root>
</log4net>
API接口
除了豐富的客戶端功能外,Exceptionless還提供了大量API的支持,這些API可以在5000端口訪問到。地址為:http://localhost:5000/docs/index.html,截圖如下:

通過這些接口,我們可以實現(xiàn)更多自定義的操作,例如用戶授權(quán)、項目管理、日志查詢等操作。

浙公網(wǎng)安備 33010602011771號