Configure Nlog with Elasticsearch in ASP.NET Core


It’s always nice to have logs from our applications. Nlog is a solid open-source library in .NET that supports save the logs. However, when we add elasticsearch to it, searching for specific logs will be simpler. In a short tutorial I will show you how to combine these two tools.


In a first step we must to add the dependencies presented below.

In NLog.config file we need to add extension for elasticsearch.

Then add specific target for Elasticsearch. NLog.conifg should looks like below (with additional comments for save nlog to file).

In appsettings.json assign the appropriate address with port on which elasticsearch works.

At the end we need to register IHttpContextAccessor.

And add code for read configuration and register NLog.

*Note that Nlog.config must have option selected in propertis: Copy to Output Directory to Copy always.

Now it’s enough to inject dependencies through the constructor and start to log „some” information.

In this way we can use the benefits of elasticsearch – typing the appropriate url in the browser like:

For searching logs http://localhost:9200/logstash-„date”/_search?q=error or check aliases on http://localhost:9200/_aliases etc.

API Elasticsearch documentation.

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *