我正在 .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作为参考
Elastic APM 服务器使用承载身份验证,因此您的代码应更新为在使用秘密令牌时发送以下标头:
["Authorization"] = "Bearer xyz"
其中 xyz 是从 APM 服务器检索到的秘密令牌。