其他分享
首页 > 其他分享> > .NET下使用ELK日志中心

.NET下使用ELK日志中心

作者:互联网

ELK原理与介绍:https://www.cnblogs.com/aresxin/p/8035137.html

 

一、Elasticsearch

7.x版本已经集成jdk了,下载地址 https://www.elastic.co/cn/downloads/elasticsearch

运行bin目录下的”elasticsearch.bat“文件,等待一段时间运行完后,浏览器打开http://localhost:9200,出现如下图即为成功

如需更改端口:打开config目录下的”elasticsearch.yml“文件,修改“http.port”选项

注:这个比较吃内存,如果内存较小会运行失败。我跑完都快占用4G了 

 

二、Logstash

下载地址:https://www.elastic.co/cn/downloads/logstash

输入配置:https://www.elastic.co/guide/en/logstash/current/input-plugins.html

输出配置:https://www.elastic.co/guide/en/logstash/current/output-plugins.html

1.在config目录下,新建“logstash-demo.conf”文件,如果要设置账号密码,则去掉#注释

input {
  tcp {
    port => 5044
    type => "service1-log"
  }
}

output {
  elasticsearch {
    hosts => ["http://localhost:9200"]
    index => "service1-%{+YYYY.MM.dd}"
    #user => "elastic"
    #password => "changeme"
  }
}

多个服务时的配置:

input {
  tcp {
    port => 5044
    type => "service1-log"
  }
  tcp {
    port => 5045
    type => "service2-log"
  }
}

output {
  if [type] == "service1-log" {
    elasticsearch {
      hosts => ["http://localhost:9200"]
      index => "service1-%{+YYYY.MM.dd}"
      #user => "elastic"
      #password => "changeme"
    }
  }
  if [type] == "service2-log" {
    elasticsearch {
      hosts => ["http://localhost:9200"]
      index => "service2-%{+YYYY.MM.dd}"
      #user => "elastic"
      #password => "changeme"
    }
  }
}

2.进入bin目录,打开CMD执行命令:

logstash.bat -f ../config/logstash-demo.conf

出现如下图信息则运行成功:

 

三、Kibana

下载地址:https://www.elastic.co/cn/downloads/kibana

运行bin目录下的”kibana.bat“文件,出现如下信息后,就可以用浏览器能打开http://localhost:5601了

如需更改端口:打开config目录下的”kibana.yml“文件,修改“server.port”选项

如需启用汉化:打开config目录下的”kibana.yml“文件,修改“i18n.locale”选项,值为 zh-CN

 

四、.NET5控制台

1.Nuget安装“NLog”

2.创建“NLog.config”配置文件,并设置属性“始终复制”

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets async="true">
    <target name="logstash" xsi:type="Network" address="tcp://127.0.0.1:5044" keepConnection="false"
            layout="[${uppercase:${level}}] [${longdate}] [${logger}] | ${message} ${exception}" />
  </targets>
  <rules>
    <logger name="*" level="Info,Error" writeTo="logstash" />
  </rules>
</nlog>

3.Main函数代码

using NLog;
using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            var logger = LogManager.LoadConfiguration("NLog.config").GetCurrentClassLogger();
            logger.Info($"当前GUID:{Guid.NewGuid()}");
            try
            {
                int n1 = 1;
                int n2 = 0;
                int n3 = n1 / n2;
            }
            catch (Exception e)
            {
                logger.Error(e);
            }
            Console.WriteLine("Hello World!");
        }
    }
}

 代码至少执行一次,再进行下面的步骤,不然截图会有差异

 

五、Kibana后台设置

1、浏览器打开http://localhost:5601

2、

 

3、

 4、输入名称 service1-* 后,下一步按钮才呈可点击状态

 5、

6、找到messge字段,点击修改

7、把包含警告信息的文本标为黄色,把包含错误信息的文本标为红色

8、查看日志的位置

 

 

附录、搭配RabbitMQ使用

过程草图:

 1、“logstash-demo.conf”改造为:

input {
  rabbitmq {
    type => "service1-log"
   exchange => "service1-log" #订阅请求失败后重试之前等待的时间(以秒为单位),必填项 subscription_retry_interval_seconds => 3 #是否持久化,默认false durable => true #rabbitmq的地址,必填项 host => "localhost"
   port => 5672 user => "guest" password => "guest" } rabbitmq { type => "service2-log"
   exchange => "service2-log" subscription_retry_interval_seconds => 3 durable => true host => "localhost"
port => 5672 user => "guest" password => "guest" } } output { if [type] == "service1-log" { elasticsearch { hosts => ["http://localhost:9200"] index => "service1-%{+YYYY.MM.dd}" #user => "elastic" #password => "changeme" } } if [type] == "service2-log" { elasticsearch { hosts => ["http://localhost:9200"] index => "service2-%{+YYYY.MM.dd}" #user => "elastic" #password => "changeme" } } }

2、控制台安装“Nlog.RabbitMQ.Target”Nuget包,“NLog.config”的改为:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:haf="https://github.com/haf/NLog.RabbitMQ/raw/master/src/schemas/NLog.RabbitMQ.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    internalLogToConsole="true">

  <extensions>
    <add assembly="NLog.Targets.RabbitMQ" />
  </extensions>

  <targets async="true">
    <!-- when http://nlog.codeplex.com/workitem/6491 is fixed, then xsi:type="haf:RabbitMQ" instead;
       these are the defaults (except 'topic', 'appid', and 'useJSON'): 
    -->
    <target name="RabbitMQTarget"
        xsi:type="RabbitMQ"
        username="guest"
        password="guest"
        hostname="localhost"
        exchange="service1-log"
        port="5672"
        topic="DemoApp.Logging.{0}"
        vhost="/"
        durable="true"
        appid="NLog.RabbitMQ.DemoApp"
        maxBuffer="10240"
        heartBeatSeconds="3"
        useJSON="true"
        layout="${message}"
        compression="none"
        />
  </targets>

  <rules>
    <logger name="*" minlevel="Info,Warn,Error" writeTo="RabbitMQTarget"/>
  </rules>

</nlog>

 参考:https://github.com/dtretyakov/NLog.RabbitMQ

 

标签:ELK,http,log,elastic,service1,NET,type,日志,localhost
来源: https://www.cnblogs.com/shousiji/p/15222276.html