TomatoLog-1.1.0實現ILoggerFactory

TomatoLog

TomatoLog 是一個基於 .NETCore 平臺的產品。git

The TomatoLog 是一箇中間件,包含客戶端、服務端,很是容易使用和部署。github

客戶端實現了ILoggerFactory,使用服務注入成功後便可使用,對業務入侵很是小,也支持經過客戶端調用寫入日誌流。mongodb

TomatoLog 的客戶端和服務端目前都是基於 .NETCore 版本,客戶端提供了三種日誌流傳輸方式,目前實現了 Redis/RabbitMQ/Kafka 流。若是但願使用非 .NETCore 平臺的客戶端,你能夠本身開放其它第三方語言的客戶端,經過實現 TomatoLog 傳輸協議,將數據傳送到管道(Redis/RabbitMQ/Kafka)中便可。數據庫

TomatoLog 服務端還提供了三種存儲日誌的方式,分別是 File、MongoDB、Elasticsearch,存儲方式能夠經過配置文件指定。在 TomatoLog 服務端,咱們還提供了一個Web 控制檯,經過該控制檯,能夠對日誌進行查詢、搜索,對服務過濾器進行配置,警報配置、通知發送等等,其中,可以使用的警報通知方式有:SMS 和 Email 兩種方式,可是,SMS 其本質是一個 Http 請求,經過 SMS 的配置,能夠實現向全部提供了 Http 接口的網關發送通知。json

TomatoLog 系統架構

Get Started

使用客戶端

選擇安裝如下客戶端中的任意一項c#

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+api

接下來,解壓文件,修改 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 的應用程序中,程序的警報配置都是存儲在配置文件中的,無需數據庫支持。服務器

相關文章
相關標籤/搜索