1
votes

I have installed Elasticsearch locally on my Windows 7 machine. It is working properly and I can query it using postman. Using Postman, I have also created an index called cmstest. This has a table called zdsf. If I get the mapping (http://localhost:9200/cmstest/_mapping) for this index I get the following:

Mapping

I can post entries to this table as well using Postman.

Now, I am trying to use Serilog from my .NET 4.5 application to log to this localhost instance of Elasticsearch.But nothing seems to be working. Here is my code:

var elasticUri = new Uri("http://localhost:9200/cmstest/zdsf");

var loggerConfig = new LoggerConfiguration()
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(elasticUri)
                {
                    AutoRegisterTemplate = true
                });

zdsf z = new zdsf();

z.filedate = DateTime.Now;
z.filename = "File2";

var logger = loggerConfig.CreateLogger();

var jsonString = new JavaScriptSerializer().Serialize(z);

logger.Error(jsonString);

I have tried using the following URLs:

http://localhost:9200/

http://localhost:9200/cmstest

http://localhost:9200/cmstest/zdsf

I have tried setting AutoRegisterTemplate to true as well as false. But Serilog doesn't seem to be writing anything to elasticsearch. It doesn't even give an exception. It just finishes without logging anything.

Another interesting thing is that fiddler doesn't show any traffic to http://localhost:9200 when serilog is supposed to be doing the logging. But it shows traffic when I make requests through Postman.

What am I doing wrong? How can I get Serilog to work?

UPDATE 1: Updated ElasticSearch.Net nuget package

I discovered that the Sink was using an old version of ElasticSearch.Net. So, I deleted the ElasticSearch.Net nuget package and downloaded the latest. This also added the needed BindingRedirects in my app.config. But logging to ElasticSearch still doesn't work.

2
You'll want to take a look at the IndexFormat parameter on ElasticsearchSinkOptions. The Url won't include the index, instead it will be the url for the Elasticsearch instance.Daniel Leach

2 Answers

2
votes

There are multiple things that were going wrong in my case:

  1. When I was trying with a console app, the app was being terminated before the log was being written. This can be fixed by either having a Console.Readline() or a Log.CloseAndFlush() statement before the app terminates.
  2. Next, the Serilog.Sinks.Elasticsearch package relies on an older version of the Elasticsearch.net nuget package. For hitting Elastic 5.X, you need a later version of the Elasticsearch.net package. So, I manually deleted the package and got the latest version.
  3. Now even though I had the latest Elasticsearch.net package, some code somewhere was looking for the older package. Adding an assembly redirect fixed this.
  4. Next, I learnt that Serilog.Sinks.Elasticsearch has a minimum logging level. I set it to Verbose. But that didn't work. The problem was I was setting it in the Sink and not at the Serilog level. After this I was able to get my local app working.
  5. I think this will still not work in Azure Function (my desired app in this case) as Azure Function doesn't seem to have assembly redirect. For this, I have downloaded the Serilog.Sinks.Elasticsearch source code, deleted the reference to the older version of Elasticsearch.net and pointed it to the latest version. I plan to use this code instead of the nuget package.
0
votes

Have you read Lifecycle of Loggers? You must either call Log.CloseAndFlush() (if you're logging via the global Log object), or dispose local Logger objects. Otherwise, your log events are never written.