惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
N
News and Events Feed by Topic
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
The Cloudflare Blog
H
Heimdal Security Blog
Schneier on Security
Schneier on Security
Engineering at Meta
Engineering at Meta
Google Online Security Blog
Google Online Security Blog
宝玉的分享
宝玉的分享
AI
AI
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Last Watchdog
The Last Watchdog
T
Troy Hunt's Blog
S
Security @ Cisco Blogs
H
Hacker News: Front Page
F
Fortinet All Blogs
博客园_首页
S
Secure Thoughts
N
News and Events Feed by Topic
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
Spread Privacy
Spread Privacy
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Hugging Face - Blog
Hugging Face - Blog
Hacker News: Ask HN
Hacker News: Ask HN
C
CXSECURITY Database RSS Feed - CXSecurity.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
L
LINUX DO - 最新话题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Schneier on Security
Know Your Adversary
Know Your Adversary
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Scott Helme
Scott Helme
P
Privacy & Cybersecurity Law Blog
S
Securelist
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
O
OpenAI News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
PCI Perspectives
PCI Perspectives
L
LangChain Blog
雷峰网
雷峰网
Security Archives - TechRepublic
Security Archives - TechRepublic
V2EX - 技术
V2EX - 技术

博客园 - Ron.Liang

亿万级分库分表后如何进行跨表分页查询 日计不足涓滴成河-自定义响应结果格式化器 .NET5.0 单文件发布打包操作深度剖析 .NET5.0 Preview 8 开箱教程 算法面试题:一个List<Student>,要求删除里面的男生,不用Linq和Lamda,求各种解,并说明优缺点! .NETCore中实现ObjectId反解 深入剖析.NETCORE中CORS(跨站资源共享) 因为喜欢所以升级,MyStaging-3.0 继续 .NETCore3.1中的Json互操作最全解读-收藏级 鲲鹏来了,在EulerOS试用.NETCore-3.1 超简单让.NET Core开发者快速拥有CI/CD的能力-Docker版本 .NETCore下CI/CD之自动化测试 多场景抢红包业务引发.NETCore下使用适配器模式实现业务接口分离 13张PPT带你了解主动式消息队列处理集群 番茄日志发布1.0.3版本-增加Kafka支持 博客园升级有感一点建议 上车时机已到--.NETCore是适应时代发展的雄鹰利剑 花5分钟时间来了解一下高性能网关Kong会有意外收获 高性能微服务网关.NETCore客户端Kong.Net开源发布
TomatoLog-1.1.0实现ILoggerFactory
Ron.Liang · 2019-08-27 · via 博客园 - Ron.Liang

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 的应用程序中,程序的警报配置都是存储在配置文件中的,无需数据库支持。