我正在尝试使用 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/