Application of ELK distributed log and NLog in dotNetCore
I. Introduction to ELK
ELK is an acronym for Elasticsearch, Logstash, and Kibana. All three are open source software. The three sets of open source tools combine to form a powerful centralized log management platform.
Elasticsearch is a distributed search and analysis engine with high scalability, high reliability, and easy management. Built on Apache Lucene, it can store, search, and analyze large-capacity data in near real time. With simple configuration, Elasticsearch will help you manage clusters, shards, failovers, master node elections, etc., and also provide a monitoring interface for cluster status.
Logstash is a tool for collecting, parsing, and filtering logs. Supports almost any type of logs, including system logs, business logs, and security logs, and supports multiple receiving methods such as file, syslog, tcp, stdin, redis, and kafka. Support elasticrsearch, email, exec, nagios, tcp, hdfs, etc.
Kibana is an open source analysis and visualization platform for searching, analyzing, and visualizing data stored in Elasticsearch, and then displaying it in the form of charts. It also allows users to customize query, filter and summarize data.
Second, installation (here take windows as an example)
1. Step 1: Download Elasticsearch Download URL:
https://www.elastic.co/cn/downloads/elasticsearch
Select the platform version that suits you, as shown in the figure below:
2. Download Logstash download address:
https://www.elastic.co/cn/downloads/logstash
, choose the platform you are suitable for, as shown in the figure below:
3. Download Kibana , download address:
https://www.elastic.co/cn/downloads/kibana
, choose your suitable platform, as shown below:
Extract the compressed package to a file, as shown in the figure:
Switch to elasticsearch-7.3.2 \ bin , click elasticsearch.bat to complete the installation, open http: // localhost: 9200 / as shown in the figure below and the installation is complete
Install node.js and grunt, download the msi of the corresponding system from the address:
https://nodejs.org/en/download/
, double-click to install
Open a command prompt and enter npm install -g grunt-cli, as shown below, grunt installation is complete
Go to \ elasticsearch-7.3.2 \ config , open elasticsearch.yml , and add the following code at the end of the file:
http.cors.enabled : true
http.cors.allow-origin : “*”
Double-click elasticsearch.bat restart es
Download Head, download address: https://github.com/mobz/elasticsearch-head , after downloading, decompress elasticsearch-head-master to elasticsearch-7.3.2 folder, as shown in the figure:
Modify Gruntfile.js in the elasticsearch -head-master folder and add hostname: ‘*’
Execute npm install in \ elasticsearch-7.3.2 \ elasticsearch-head-master folder
After installation is complete perform the grunt server or npm run start running head plug, HTTP: // access the next browser http: // localhost: 9100 /
logstash installation: start the configuration file after decompression
Kibana installation: \ config kibana-7.3.2 folder modify kibana.yml, cancel server.port , server.host , server.name , elasticsearch.hosts , i18n.locale in front of the # number and i18n.locale value Change to zh-CN , run kibana.bat in the bin folder , open localhost: 5601, as shown in the figure, the installation was successful
Third, .Net Core uses Nlog to call ELK to write logs
1.NuGet installation dependencies
Configure nlog.config
<?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" internalLogToConsole="true"> <extensions> <add assembly="NLog.Targets.ElasticSearch"/> </extensions> <targets async="true"> <!--https://github.com/reactive-markets/NLog.Targets.ElasticSearch/wiki--> <target xsi:type="ElasticSearch" name="ElasticSearch" uri="http://127.0.0.1:9200" index="Web" documentType="logevent" includeAllProperties="false" requireAuth="false"> <field name="host" layout="${machinename}" /> <field name="application" layout="${applicationName}" /> <field name="logged" layout="${date}" /> <field name="level" layout="${level}" /> <field name="message" layout="${message}" /> <field name="logger" layout="${logger}" /> <field name="callSite" layout="${callsite:filename=true}" /> <field name="exception" layout="${exception:tostring}" /> <field name="IP" layout="${aspnet-request-ip}" /> <field name="User" layout="${aspnetcore-request-user}" /> <field name="serverName" layout="${machinename}" /> <field name="url" layout="${aspnetcore-request-url}" /> </target> </targets> <rules> <logger name="*" minlevel="INFO" writeTo="ElasticSearch" /> </rules> </nlog>
View Code
3.Startup.cs configuration
4.Application
If you see the following data, the call is successful
Orignal link:https://www.cnblogs.com/minghon/p/11853157.html