我正在使用.Net Framework 4.8中的Serilog将日志发送到OpenTelemetry。我使用Elastic作为OpenTelemetry后端。我可以发送跟踪,但不能发送日志。请找到我下面的代码,它将日志写入文件,但不将日志发送到OpenTelemetry。有人能帮忙吗?
// TRACES ----- WORKING
_tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetInstrumentation()
.AddHttpClientInstrumentation()
.AddOtlpExporter(config =>
{
config.Endpoint = new Uri("https://abcd.es.io:443");
config.Headers = "Authorization=ApiKey xyz";
})
.AddSource("SK")
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService(serviceName: "NLogger", serviceVersion: "1.0.0")
.AddAttributes(resourceAttributes))
.Build();
var endpoint = "https://abcd.es.io:443/v1/logs";
var protocol = OtlpProtocol.HttpProtobuf;
另一方面,跟踪不起作用:
// LOGGING --- NOT WORKING
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Error()
.WriteTo.Console()
.WriteTo.File(@"c:\tt\1.txt")
.WriteTo.OpenTelemetry(
options => {
options.Endpoint = endpoint;
options.Protocol = protocol;
options.IncludedData =
IncludedData.SpanIdField
| IncludedData.TraceIdField
| IncludedData.MessageTemplateTextAttribute
| IncludedData.MessageTemplateMD5HashAttribute;
options.ResourceAttributes = new Dictionary<string, object>
{
["service.name"] = "NLogger",
["index"] = 10,
["flag"] = true,
["pi"] = 3.14
};
options.Headers = new Dictionary<string, string>
{
["Authorization"] = "Basic xyz", // user:abc123
};
options.BatchingOptions.BatchSizeLimit = 2;
options.BatchingOptions.Period = TimeSpan.FromSeconds(2);
options.BatchingOptions.QueueLimit = 10;
})
.CreateLogger();
我尝试在授权头中使用“ApiKey”而不是“Basic”,但仍然不起作用。它成功地将日志写入文本文件。请帮帮我
关注此页https://github.com/serilog/serilog-sinks-opentelemetry/blob/dev/example/Example/Program.cs作为参考
2条答案
按热度按时间pkbketx91#
Elastic APM Server使用承载身份验证,因此您的代码应更新为在使用秘密令牌时发送以下标头:
其中xyz是从APM服务器检索的秘密令牌。
请访问https://www.elastic.co/guide/en/apm/get-started/7.15/open-telemetry-elastic.html#instrument-apps-apm-server
brccelvz2#
ApiKey终于成功了。发送/查看日志也存在访问问题。发送/查看日志需要特殊权限。