ASP.NET Core uses NLog to push logs to the ELK system, collecting and analyzing logs uniformly.
First, let's take a look at the renderings, as follows:
Create a new ASP.NET Core 3.1 website project that references NLog.Web.AspNetCore as follows:
Create a new nlog.config configuration file and configure it as follows:
The program file is modified as follows:
We will write data to a file, as well asWriting to Elasticsearch via logstash, we send logging in UDP, why write to a file? Because UDP is unreliable and cannot 100% guarantee that all log data will be written to the ELK system normally, saving it to the local disk is equivalent to a backup.
Local logs are stored for up to 30 days, and logs older than 30 days are automatically cleaned up, which can be set through a profile.
Logstash creates a new test.conf configuration file as follows:
Start Logstash with the following command:
In the HomeController controller, call the log framework, access the Index page through the browser, and write 10,000 logs to the ELK system, the code is as follows:
Click Stack Management in the Kibana interface, select the index mode, click Create Index Mode in the upper right corner, and fill in itsvse-logs-*, as shown in the figure below:
After that, you can see the logs we recorded in Kibana's Discover interface.
Finally, attach the source code:
Tourists, if you want to see the hidden content of this post, please Reply
|