tomatolog

tomatolog 是一个基于 .netcore 平台的产品。

the tomatolog 是一个中间件,包含客户端、服务端,非常容易使用和部署。

客户端实现了iloggerfactory,使用服务注入成功后即可使用,对业务入侵非常小,也支持通过客户端调用写入日志流。

tomatolog 的客户端和服务端目前都是基于 .netcore 版本,客户端提供了三种日志流传输方式,目前实现了 redis/rabbitmq/kafka 流。如果希望使用非 .netcore 平台的客户端,你可以自己开放其它第三方语言的客户端,通过实现 tomatolog 传输协议,将数据传送到管道(redis/rabbitmq/kafka)中即可。

tomatolog 服务端还提供了三种存储日志的方式,分别是 file、mongodb、elasticsearch,存储方式可以通过配置文件指定。在 tomatolog 服务端,我们还提供了一个web 控制台,通过该控制台,可以对日志进行查询、搜索,对服务过滤器进行配置,警报配置、通知发送等等,其中,可使用的警报通知方式有:sms 和 email 两种方式,但是,sms 其本质是一个 http 请求,通过 sms 的配置,可以实现向所有提供了 http 接口的网关发送通知。

tomatolog 系统架构

get started

使用客户端

选择安装以下客户端中的任意一项

install-package tomatolog.client.redis
install-package tomatolog.client.rabbitmq
install-package tomatolog.client.kafka

tomatolog客户端配置文件 appsetting.json

{
  "tomatolog": {
    "loglevel": "information",
    "projectlabel": "example",
    "projectname": "example",
    "sysoptions": {
      "eventid": true,
      "ip": true,
      "iplist": true,
      "machinename": true,
      "processid": true,
      "processname": true,
      "threadid": true,
      "timestamp": true,
      "username": true
    },
    "tags": null,
    "version": "1.0.0",
    "exchange": "tomatolog-exchange",
    "exchangetype": "direct",
    "host": "127.0.0.1",
    "password": "123456",
    "port": 5672,
    "queuename": "tomatolog-queue",
    "routekey": "all",
    "username": "lgx",
    "vhost": "tomatolog"
  }
}

服务注入

public void configureservices(iservicecollection services)
{
    services.addsingleton<itomatologclient>(factory =>
    {
        var options = this.configuration.getsection("tomatolog").get<eventrabbitmqoptions>();
        var client = new tomatologclientrabbitmq(options);

        return client;
    });
    ...
}

配置启用

public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory factory, itomatologclient logclient)
{
    factory.usetomatologger(logclient);
    ...
}

使用 tomatologclient

[route("api/[controller]")]
[apicontroller]
public class homecontroller : controllerbase
{
    private readonly itomatologclient logclient;
    private readonly ilogger logger;
    public homecontroller(ilogger<homecontroller> logger, itomatologclient logclient)
    {
        this.logger = logger;
        this.logclient = logclient;
    }

    [httpget]
    public async task<actionresult<ienumerable<string>>> get()
    {
        // used by ilogger
        this.logger.logerror("测试出错了");

        // used by itomatologclient
        try
        {
            await this.logclient.writelogasync(1029, loglevel.warning, "warning infomation", "warning content", new { lasttime = datetime.now, tips = "warning" });
            throw new notsupportedexception("notsupported media type");
        }
        catch (exception ex)
        {
            await ex.addtomatologasync();
        }

        return new string[] { "value1", "value2" };
    }
}

部署服务端

首先,下载服务端压缩包文件 版本预览 ,该压缩包仅包含项目运行必需文件,托管该服务端的服务器上必须按照 dotnet core sdk 2.2+

接下来,解压文件,修改 appsetting.environment.json 文件将服务器进行配置,将配置好的服务端部署到你的服务器上,可以为 tomatolog 选择 iis 或者其它托管方式,服务端默认运行端口为:20272.

编辑服务端配置文件

{
  "logging": {
    "includescopes": false,
    "loglevel": {
      "default": "debug",
      "system": "information",
      "microsoft": "information"
    }
  },
  "tomatolog": {
    "cache-redis": null, // 过滤器会使用该分布式缓存进行策略考量,如果有配置
    "config": {
      "sysconfig": "config/sysconfig.json" // 系统配置文件,可通过web控制台进行配置
    },
    "storage": {
      "type": "tofile", //tofile/toes/tomongodb 可以选择的存储方式
      "file": "d:\\tomatolog\\storage", // 如果type选择了 tofile ,则这里必须指定绝对路径
      "es": "http://127.0.0.1:9200/", // 如果type选择了toes,这里必须配置 elasticsearch 服务地址
      "mongodb": "mongodb://root:root@127.0.0.1:27017/admin" //如果type选择了tomongodb,这里必须配置 tomongodb 数据库链接
    },
    "flow": {
      "type": "rabbitmq", // redis/rabbitmq/kafaka 这里指定客户端和服务器的传输管道类型,两端配置必须一致
      "redis": {
        "connection": null,
        "channel": "tomatologchannel"
      },
      "rabbitmq": { // 如果使用了 rabbitmq,则必须配置该节点
        "host": "127.0.0.1",
        "port": 5672,
        "username": "root",
        "password": "123456",
        "vhost": "tomatolog",
        "exchange": "tomatolog-exchange",
        "exchangetype": "direct",
        "queuename": "tomatolog-queue",
        "routekey": "all",
        "channels": 1 // 运行的消息队列实例数量
      },
      "kafka": {
        "group": "tomatologserver",
        "bootstrapservers": "127.0.0.1:9092",
        "topic": "tomatolog"
      }
    }
  }
}

番茄日志服务端控制台长什么样

在浏览器中打开地址:http://localhost:20272/

首页看日志列表

日志详情、弹出查看详情、日志搜索、支持es/mongodb/file搜索

全局日志处理、警报配置

针对单个项目的详细日志处理、警报配置

一次打包,到处运行

不管是从项目结构还是解决方案,我都强调简单就是最美的根本要求,解决方案的内容虽然看起来很多,但是你也只需要按需引用其中一个客户端就可以了,服务端更是如此,全站都打包在一个 .netcore 的应用程序中,程序的警报配置都是存储在配置文件中的,无需数据库支持。