zoukankan      html  css  js  c++  java
  • ELK系列~NLog.Targets.Fluentd到达如何通过tcp发到fluentd

    最近火的不能再火的日志框架就是ELK,其中E(Elasticsearch)表示日志存储,L(Logstash)表示日志收集,K(kibana)表示日志的UI界面,用来查询和分析,而其中的L可以使用Fluentd来代替,并且以上架构都可以通过docker来进行快速的部署。

    它们的工作流程

    fluentd系统有输入和输出,输入对应我们的应用程序,输出对应咱们的ES存储系统,在.net平台上,如果希望把日志发到fluentd,有三种方式,下面分别简单说一下:

    1. http方式
    2. tcp方式
    3. 客户端c/s方式

    一 http方式

    构建一个共享的httpclient对象,然后声明为keep-alive,使用keep-alive可以改善这种状态,即在一次TCP连接中可以持续发送多份数据而不会断开连接。通过使用keep-alive机制,可以减少tcp连接建立次数,也意味着可以减少TIME_WAIT状态连接,以此提高性能和提高httpd服务器的吞吐率(更少的tcp连接意味着更少的系统内核调用,socket的accept()和close()调用)。

    fluentd配置:

    <source>
        @type http
        port 24224
        bind 0.0.0.0
      </source>

    C#代码:

                        var json = JsonConvert.SerializeObject(new
                        {
                            target_index = projectName,
                            timestamp = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ"),
                            Level = level.ToString(),
                            Message = message,
                            StackTrace = ex?.StackTrace
                        });
                        json = json.Replace("target_index", "@target_index").Replace("timestamp", "@timestamp");
                        var httpContent = new StringContent(json, Encoding.UTF8);
                        httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                        httpClient.PostAsync(apiLoggerUrl, httpContent).Wait();

    二 tcp方式

    tcp方式比http要复杂一些,在fluentd配置时也需要注册,在性能上优于http,目前NLog.Targets.Fluentd这个框架对它进行了集成,但说明文档不是很清楚,大叔经过测试也已经成功实现了tcp的日志记录,需要注意的地方就是fluentd在接受tcp时,需要有一个结果标示,默认是 ,在客户端发送请求时需要在数据包结尾添加它。

    NLog.Targets.Fluentd在github上的地址:

    https://github.com/fluent/NLog.Targets.Fluentd

    fluentd配置:

      <source>
        @type tcp
        tag pilipa
        format /^(?<field1>d+):(?<field2>w+)$/
        port 24224
        bind 0.0.0.0
      </source>
      <match **>
        @type stdout
      </match>

    客户端调用

              var fluentdTarget = new NLog.Targets.Fluentd()
                {
                    Host = "192.168.200.214",
                    Port = 24224,
                    Tag = "pilipa",
                    LingerEnabled = false,
                    NoDelay = true,
                    EmitStackTraceWhenAvailable = false,
                };
                LoggingConfiguration config = new LoggingConfiguration();
                config.AddRuleForOneLevel(LogLevel.Info, "fluentd");
                LogManager.Configuration = config;
                Logger logger = LogManager.GetLogger("Example");
                logger.Info(json + "
    "); //这是必须的,看到
    表示数据包结束
                Console.Read();

     然后在我们的fluentd上就可以看到日志了。

    三 客户端c/s方式

    目前正在研究,客户端驱动地址:https://docs.fluentd.org/v0.12/articles/windows#set-up-nxlog-on-windows

  • 相关阅读:
    GitLab 介绍
    git 标签
    git 分支
    git 仓库 撤销提交 git reset and 查看本地历史操作 git reflog
    git 仓库 回退功能 git checkout
    python 并发编程 多进程 练习题
    git 命令 查看历史提交 git log
    git 命令 git diff 查看 Git 区域文件的具体改动
    POJ 2608
    POJ 2610
  • 原文地址:https://www.cnblogs.com/lori/p/7716093.html
Copyright © 2011-2022 走看看