Serilog无法与Elasticsearch一起使用 [英] Serilog not working with Elasticsearch

查看:198
本文介绍了Serilog无法与Elasticsearch一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Windows 7计算机上本地安装了Elasticsearch.它工作正常,我可以使用邮递员查询它.使用邮递员,我还创建了一个名为cmstest的索引.这有一个称为zdsf的表.如果我获得此索引的映射( http://localhost:9200/cmstest/_mapping )得到以下信息:

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:

我也可以使用Postman将条目发布到此表中.

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

现在,我正在尝试使用.NET 4.5应用程序中的Serilog登录到Elasticsearch的该localhost实例.但是似乎没有任何效果.这是我的代码:

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);

我尝试使用以下URL:

I have tried using the following URLs:

http://localhost:9200/

http://localhost:9200/cmstest

http://localhost:9200/cmstest/zdsf

我尝试将AutoRegisterTemplate设置为true和false.但是Serilog似乎并没有为Elasticsearch写任何东西.它甚至都没有例外.它只是完成而没有记录任何内容.

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.

另一个有趣的事情是,当serilog是serilog时,提琴手不会显示对 http://localhost:9200 的访问.应该在做日志记录.但是当我通过邮递员发出请求时,它显示了访问量.

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.

我做错了什么?我如何使Serilog正常工作?

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

更新1:更新了ElasticSearch.Net nuget程序包

我发现接收器正在使用旧版本的ElasticSearch.Net.因此,我删除了ElasticSearch.Net nuget软件包并下载了最新的软件包.这也在我的app.config中添加了所需的BindingRedirects.但是登录到ElasticSearch仍然行不通.

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.

推荐答案

在我的情况下,有很多事情出了问题:

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

  1. 当我尝试使用控制台应用程序时,该应用程序在写入日志之前被终止.可以通过在应用终止之前使用Console.Readline()或Log.CloseAndFlush()语句来解决此问题.
  2. 接下来,Serilog.Sinks.Elasticsearch软件包依赖于Elasticsearch.net nuget软件包的旧版本.要使用Elastic 5.X,您需要更高版本的Elasticsearch.net软件包.因此,我手动删除了该软件包并获得了最新版本.
  3. 现在,即使我拥有最新的Elasticsearch.net软件包,某些地方的代码仍在寻找较旧的软件包.添加程序集重定向可解决此问题.
  4. 接下来,我了解到Serilog.Sinks.Elasticsearch具有最低日志记录级别.我将其设置为详细".但这是行不通的.问题是我将其设置在接收器中,而不是在Serilog级别上.之后,我可以使我的本地应用正常工作.
  5. 我认为这在Azure Function(在这种情况下是我想要的应用程序)中仍将不起作用,因为Azure Function似乎没有程序集重定向.为此,我下载了Serilog.Sinks.Elasticsearch源代码,删除了对旧版本Elasticsearch.net的引用,并将其指向最新版本.我计划使用此代码代替nuget包.

这篇关于Serilog无法与Elasticsearch一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆