開源日志框架Exceptionless使用教程


Exceptionless是一款日志記錄框架,它開源、免費、提供管理界面、易於安裝和使用。ExceptionLess底層采用ElasticSearch作為日志存儲,提供了快速、豐富的查詢API,方便我們進行系統集成。本文將介紹ExceptionLess的常見用法。

安裝ExceptionLess

在ExceptionLess官網提供了基於Docker的私有化部署方式,我們可以按照官網的方式進行測試環境的安裝。

  1. 在官網github中下載最新的release包,地址:https://github.com/exceptionless/Exceptionless/releases
  2. 解壓縮,然后進入解壓縮的目錄,執行 docker-compose up -d命令在后台啟動多個容器,當執行完成后,Exceptionless已經在本地運行起來了。我們可以在Kitematic中查看運行中的容器
  3. 按照官網的說明,5000端口是登陸頁面,但實際情況是5000是API,5100才是登陸頁面,因此我們打開http://localhost:5100進入登陸頁面。注意:此處可能跟版本有關,在使用時查看docker的端口映射。

通過以上步驟,就在本地搭建好了測試環境。我們可以看到啟動的總共啟動了6個容器,分別是redis、elasticsearch、kibana、Exceptionless Job、Exceptionless Api、Excetpionless UI。

快速上手

搭建好測試環境后,首先訪問Exceptionless UI來創建用戶、組織和項目。然后,當項目創建完成之后,Exceptionless 會跳轉到客戶端配置頁面,來指引我們如何使用Exceptionless客戶端。我們可以選擇自己需要用到的客戶端,通過頁面的指引完成客戶端配置。

引導頁面如下:

image

按照這種方式我們可以完成.Net平台項目、JS項目的配置。

客戶端API:https://github.com/exceptionless/Exceptionless.Net/wiki

配置好以后,我們就可以記錄日志了,例如(代碼來源於官網):

// 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中的事件有以下幾種類型:

  • 日志消息:記錄的日志,可以是任何文本內容
  • 特性使用:功能使用量的記錄,例如接口調用情況等
  • 異常情況:記錄異常的信息
  • 失效鏈接:當被訪問的頁面不存在時進行記錄

除了記錄內容外,Exceptionless還支持對事件添加標簽、附加數據、用戶描述等操作,例如(代碼來源於官網):

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集成的支持,只需要添加相應的日志組件的配置文件即可。以Log4net為例:

首先添加程序集的支持:

Install-Package Exceptionless.Log4net

然后在log4net的配置文件中進行配置(代碼來源於官網):

<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,截圖如下:

image

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

參考資料


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM