【发布时间】:2017-08-23 13:40:49
【问题描述】:
我已经在我的 Windows 7 机器上本地安装了 Elasticsearch。它工作正常,我可以使用邮递员查询它。使用 Postman,我还创建了一个名为 cmstest 的索引。这有一个名为 zdsf 的表。如果我得到这个索引的映射 (http://localhost:9200/cmstest/_mapping),我会得到以下信息:
我也可以使用 Postman 将条目发布到此表。
现在,我正在尝试使用我的 .NET 4.5 应用程序中的 Serilog 来登录到 Elasticsearch 的这个 localhost 实例。但似乎没有任何效果。这是我的代码:
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);
我已尝试使用以下网址:
http://localhost:9200/cmstest/zdsf
我尝试将 AutoRegisterTemplate 设置为 true 和 false。但是 Serilog 似乎没有向 elasticsearch 写入任何内容。它甚至没有给出例外。它只是完成而没有记录任何内容。
另一个有趣的事情是,当 serilog 应该进行日志记录时,fiddler 没有显示到 http://localhost:9200 的任何流量。但是当我通过邮递员发出请求时,它会显示流量。
我做错了什么?如何让 Serilog 工作?
更新 1:更新了 ElasticSearch.Net nuget 包
我发现 Sink 使用的是旧版本的 ElasticSearch.Net。所以,我删除了 ElasticSearch.Net nuget 包并下载了最新的。这还在我的 app.config 中添加了所需的 BindingRedirects。但是登录到 ElasticSearch 仍然不起作用。
【问题讨论】:
-
您需要查看 ElasticsearchSinkOptions 上的 IndexFormat 参数。 Url 不会包含索引,而是 Elasticsearch 实例的 url。
标签: .net elasticsearch logging elastic-stack serilog