ELK+Nlog+.net 搭建一个完善的日志分析平台

我们先在Windows下面搭建elk日志平台,等测试通过在把elk移植到linux平台下。

防止掉坑,一定要版本统一,还有就是java的运行环境是1.8.* 不要最新版本,最新版本部署会报错。

所有资料文章最后会统一给出。

首先安装 elasticsearch:

通过cmd执行下面这个文件 elasticsearch.bat

访问http://localhost:9200/ 出现下面信息则安装成功

安装插件 head

收件安装node环境,执行node安装报,无脑安装执行下一步

通过命令 node -v 查看

执行命令 npm install –g grunt–cli

通过命令到head目录,执行npm install

编辑elasticsearch-5.6.4/config/elasticsearch.yml,加入以下内容:
 

http.cors.enabled: true
http.cors.allow-origin: "*"

打开head/Gruntfile.js,找到下面connect属性,新增 
hostname: ‘0.0.0.0’:

connect: {
        server: {
            options: {
                hostname: '0.0.0.0',
                port: 9100,
                base: '.',
                keepalive: true
            }
        }
    }   

在head/目录下,运行启动命令:

grunt server

然后访问 http://localhost:9100/ 出现下面这个页面,表示安装成功

用head连接elasticsearch发现未连接,那是因为没有配置config下面的elasticsearch.yml

取消注释,修改为:network.host:0.0.0.0

然后重新运行bin目录下的elasticsearch.bat

安装logstash:

在D:\elk\logstash-5.6.4\bin目录下配置文件:test.conf

文件内容

通过控制台查看是否安装成功

input,filter,output,input指的是数据源来自什么地方,stdin{}就是从屏幕是读取数据,filter是根据你的需要来修改一些数据的格式,outout是要把这些数据送到哪个地方,stdout{}就是在屏幕上输出数据,由于我这里不需要filter,所以就没有写,有需要可以参考官方文档https://www.elastic.co/guide/en/logstash/current/filter-plugins.html,这些配置好之后,打开cmd,进入logstash的文件夹,运行logstash -f ***.conf,-f后面是刚刚配置文件所在的位置,我是和logstash.bat放在同一个文件夹下的,所以可以直接运行

然后输入信息

表示安装成功,有些时候会遇到启动错误,一般情况都是*.conf自己写的内容有问题,要自己手写不要直接从网页复制,或者是文件类型编码utf-8这些问题。

控制台能正确输入输出信息,接下来我们修改配置文件把信息发送到elasticsearch,通过tcp监听接口消息、同时监控 控制台输入信息

input:接受日志来源,这里只配置了两个stdin(控制台)、tcp

filter:暂时没有用,处理日志

output:输出日志stdout(控制台),elasticsearch,file这三个

安装kibana

配置好config下面的kibana.yml

然后启动 D:\elk\kibana-5.6.4\bin下面的kibana.bat

访问http://localhost:5601  得到下面这个页面

有日志了页面变成这样

注意:配置文件总结

首先是:lasticsearch config下面的elasticsearch.yml文件,一个是

network.host:0.0.0.0 

http.cors.enabled:trur

http.cors.allow-origin:"*"

lasticsearch下面head文件里的

Gruntfile.js,找到下面connect属性,新增 
hostname: ‘0.0.0.0’,

connect: {
        server: {
            options: {
                hostname: '0.0.0.0',
                port: 9100,
                base: '.',
                keepalive: true
            }
        }
    }   

kibana

kibana下面的config文件kibana.yml配置如下:

server.port: 5601

server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://192.168.10.122:9200"

最重要的就是logstash,前面两个都能通过浏览器查看是否安装成功

logstash只能通过控制台进行查看,首先通过控制台进行操作

配置D:\elk\logstash-5.6.4\config下面的jvm.options文件

增加一句-XX:+UseParNewGC

然后增加配置文件 *.conf

通过logstash -f  *.conf 运行

其它主机想通过tcp向装有logstash的服务器传输日志,记得查看logstash服务器端口这些是否打开。

Nlog的配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <variable name="myvar" value="myvalue"/>

  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
  <targets>

    <!--
    add your targets here
    See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    <target xsi:type ="File"
             name="file"
             fileName="${basedir}/App_Data/Logs/operation.log"
             layout="${longdate} - ${level:uppercase=true}:${message} ${callsite:fileName=true} ${exception:format=Type,Message,Method,StackTrace:maxInnerExceptionLevel=5:innerFormat=ShortType,Message,Method,StackTrace}"
             keepFileOpen="false"
             encoding="utf-8"
             archiveFileName="${basedir}/App_Data/Logs/Backup_${shortdate}.{##}.log"
             archiveNumbering="Sequence"
             archiveEvery="Day"
             maxArchiveFiles="31">
    </target>
    -->
    <target name="network" xsi:type="Network" address="tcp://192.168.10.122:8100" layout="${level} ${logger} ${message}${newline}" />
  </targets>
  <rules>
    <!-- add your logging rules here -->

    <!--
    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
    <logger name="*" minlevel="Debug" writeTo="f" />
    -->
    <logger name="*" minlevel="Debug" writeTo="network" />
  </rules>
</nlog>

然后通过代码请求

 Task.Factory.StartNew(() => LogManager.GetCurrentClassLogger().Log(LogLevel.Info, message));

参考文档:

Nlog向logstash输送日志

https://www.cnblogs.com/cheesebar/p/9146298.html   nlog logstash

https://www.cnblogs.com/never-give-up-1015/p/5720488.html nlog logstash

https://nlog-project.org/config/?tab=layout-renderers nlog

https://github.com/NLog/NLog/wiki/Tutorial#configure-nlog-targets-for-output nlog

https://github.com/jkowalski/NLog/tree/master/examples/targets/Configuration%20File nlog 配置模板

https://www.elastic.co/   elk官网

elk日志环境所需资料

链接:https://pan.baidu.com/s/1myqOTpCd4lYUN_NYHjlLGA密码:gj7v  所需文件资料

所需启动的服务

D:\elk\elasticsearch-5.6.4\bin>elasticsearch.bat

D:\elk\elasticsearch-5.6.4\head>grunt server

D:\elk\kibana-5.6.4\bin>kibana.bat

D:\elk\logstash-5.6.4\bin>logstash -f test.conf


版权声明:本文为w1109390306原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。