Introduction
Earlier in the series, we started putting together an implementation of log messages. We are using ASP.NET Core Web Application and Serilog library. We defined various types of logs and then put together small codebase which hooks up in ASP.NET Core pipeline easily and leverages its infrastructure as well. In previous post of this series, we completed our journey of persisting our logs to file storage. In this post, I will show you one more destination for this persistence i.e. ElasticSearch.
Elasticsearch itself is a big topic and there is a lot you can do with it. Its powerful, fast and tested analytics solution. You can bring in Kibana if you want to build dashboard or other tools from ElasticStack for your solution. I will not dig deeper of its working, but I already wrote few articles about it and you can read those, if you are interested.
Persisting to Elastic Search using Serilog Sink
First thing first, lets add the nuget package for elastic-search sink. This package will take care of all the underlying work needed to communicate and persist data to elastic-search.
After adding this package, we can now configure the AppLogger to write to ElasticSearch as shown below:
.WriteTo.Elasticsearch method is available due to the nuget package we added earlier. Now, we are writing to this sink as well as to file. There are a lot of other sinks available via nuget which work with serilog. You can add more if you needed.
You also, need to install Elasticsearch service on your system or you can use a docker image. You can find the instructions on the official website of elasticsearch.
Another route you can take is to persist this information with some other way e.g. NEST library. In that case you wont need serilog and you need to send your log data via NEST mechanism. However, we will not do that in this demo. If you want to learn more about NEST or elasticsearch, you can refer to this article for details where I covered that functionality.
ElasticSearch Results
I ran the .NET Core Web Application and made various REST calls using Postman tool. Now ElasticSearch itself expose a REST API for various operations. Now to see log data in the elastic-search indices, we can issue the following REST call via web-browser and the following result shows that indices are created for various logs as we configured those earlier.
Now, lets see what is inside in on of these indices. We can use browser and ElasticSearch REST API for this as shown below:
We can see our log messages are being persisted and All it took was a nuget package and a method call to elasticsearch.
Now you can repeat this pattern for various serilog sinks. Also visit the serilog website if you want to see more details information about available sinks and their configurations.
Summary
Well, this was the final part of this series. I tried to keep it simple and yet the components are easy to extend. Its not a lot of code base and may be you can refactor it a little bit more. All the code for these demos is available on this git repo. Feel free to change it as u you. Let me know if you have some comments or questions and Till next time, happy coding.
Discover more from Hex Quote
Subscribe to get the latest posts sent to your email.