c# - 更改 elasticsearch serilog 接收器 http 内容 header

标签 c# elasticsearch kibana serilog

我正在尝试使用 serilog elasticsearch 接收器将一些日志写入 kibana。 根据规范 here 内容应设置为 application/x-ndjson 并且由于没有用于设置内容类型的特定选项我已经尝试使用 GlobalHeaders 但它没有效果而且它设置为 json。 我已将示例应用程序更改为以下内容:

Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.Console(theme: SystemConsoleTheme.Literate)
            .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://elastic:changeme@localhost:9200")) // for the docker-compose implementation
            {
                ModifyConnectionSettings =
                    x => x.GlobalHeaders(new NameValueCollection { { "Content-Type", "application/x-ndjson" } }),
                BufferBaseFilename = "Text.txt",
                Period = TimeSpan.FromSeconds(10),
                AutoRegisterTemplate = true,
                //BufferBaseFilename = "./buffer",
                RegisterTemplateFailure = RegisterTemplateRecovery.IndexAnyway,
                FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
                EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog |
                                   EmitEventFailureHandling.WriteToFailureSink |
                                   EmitEventFailureHandling.RaiseCallback,
                FailureSink = new FileSink("./failures.txt", new JsonFormatter(), null)
            })
            .CreateLogger();

            // Enable the selflog output
            SelfLog.Enable(Console.Error);

            Log.Information("Hello, world!");

            int a = 10, b = 0;
            try {
                Log.Debug("Dividing {A} by {B}", a, b);
                Console.WriteLine(a / b);
            } catch (Exception ex) {
                Log.Error(ex, "Something went wrong");
            }

            for (int i = 0; i < 10; i++) {
                Log.Error(new Exception($"Test error {i}"), "Test error {Error}", "TEst message");
            }

api 调用未返回错误,但 kibana 中不存在日志。 我是做错了什么,还是无法配置,或者我需要更改服务器中的某些内容?

最佳答案

我希望下面的代码有助于确定问题所在

ModifyConnectionSettings = x => x.OnRequestCompleted(ELKReqCompleted);

 void ELKReqCompleted(IApiCallDetails apiCallDetails)
        {
            //apiCallDetails.AuditTrail
            //apiCallDetails.DebugInformation

        }

关于c# - 更改 elasticsearch serilog 接收器 http 内容 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51825629/

相关文章:

javascript - Cordova WP8 - 推送通知回调

elasticsearch - Elasticsearch-head集群运行状况:未连接

elasticsearch - 更新在Elasticsearch中如何工作?

elasticsearch - 在 Elasticsearch 中如何进行词条查询?

elasticsearch - ELK和LDAP之间的集成

c# - 属性引用不在主键上的一对多映射

c# - 无法从 Docker 中的 ASP.NET Core 3.1 连接到本地 SQL Server

python - Elasticsearch 没有给出准确的结果python

c# - 如何从 XML 文档读取值以构建组合框?

elasticsearch - 无法使用Logstash(7.6.2)和xml过滤器插件设置解析和设置时间戳