首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >开源日志框架Exceptionless使用教程

开源日志框架Exceptionless使用教程

作者头像
拓荒者-NET
发布2019-09-23 15:45:50
1.2K0
发布2019-09-23 15:45:50
举报

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

通过这些接口,我们可以实现更多自定义的操作,例如用户授权、项目管理、日志查询等操作。

参考资料

我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3mb9ch11jjy8o

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装ExceptionLess
  • 快速上手
  • 功能介绍
  • NLog、Log4net集成
  • API接口
  • 参考资料
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档