TomatoLog-1.1.0實現ILoggerFactory

  • 2019 年 10 月 3 日
  • 筆記

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:[email protected]: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 的應用程式中,程式的警報配置都是存儲在配置文件中的,無需資料庫支援。