gpt4 book ai didi

c# - OpenTracing 不使用 Serilog 发送日志

转载 作者:可可西里 更新时间:2023-11-01 08:12:52 27 4
gpt4 key购买 nike

我正在尝试使用 OpenTracing.Contrib.NetCore与 Serilog。我需要将我的自定义日志发送给 Jaeger。现在,它仅在我使用默认记录器工厂 Microsoft.Extensions.Logging.ILoggerFactory

时有效

我的创业公司:

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

services.AddSingleton<ITracer>(sp =>
{
var loggerFactory = sp.GetRequiredService<ILoggerFactory>();
string serviceName = sp.GetRequiredService<IHostingEnvironment>().ApplicationName;

var samplerConfiguration = new Configuration.SamplerConfiguration(loggerFactory)
.WithType(ConstSampler.Type)
.WithParam(1);

var senderConfiguration = new Configuration.SenderConfiguration(loggerFactory)
.WithAgentHost("localhost")
.WithAgentPort(6831);

var reporterConfiguration = new Configuration.ReporterConfiguration(loggerFactory)
.WithLogSpans(true)
.WithSender(senderConfiguration);

var tracer = (Tracer)new Configuration(serviceName, loggerFactory)
.WithSampler(samplerConfiguration)
.WithReporter(reporterConfiguration)
.GetTracer();

//GlobalTracer.Register(tracer);
return tracer;
});
services.AddOpenTracing();
}

在 Controller 的某处:

[Route("api/[controller]")]
public class ValuesController : ControllerBase
{
private readonly ILogger<ValuesController> _logger;

public ValuesController(ILogger<ValuesController> logger)
{
_logger = logger;
}

[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
_logger.LogWarning("Get values by id: {valueId}", id);
return "value";
}
}

结果我将能够在 Jaeger UI 中看到该日志 enter image description here

但是当我使用 Serilog 时,没有任何自定义日志。我已将 UseSerilog() 添加到 WebHostBuilder,并且我可以在控制台中看到所有自定义日志,但在 Jaeger 中看不到。github 中有 Unresolved 问题.您能否建议我如何将 Serilog 与 OpenTracing 结合使用?

最佳答案

这是 Serilog 记录器工厂实现中的一个限制;特别是,Serilog 目前会忽略添加的提供程序,并假定 Serilog Sinks 将取而代之。

因此,解决方案是实现一个简单的 WriteTo.OpenTracing() 方法,将 Serilog 直接连接到 OpenTracing

public class OpenTracingSink : ILogEventSink
{
private readonly ITracer _tracer;
private readonly IFormatProvider _formatProvider;

public OpenTracingSink(ITracer tracer, IFormatProvider formatProvider)
{
_tracer = tracer;
_formatProvider = formatProvider;
}

public void Emit(LogEvent logEvent)
{
ISpan span = _tracer.ActiveSpan;

if (span == null)
{
// Creating a new span for a log message seems brutal so we ignore messages if we can't attach it to an active span.
return;
}

var fields = new Dictionary<string, object>
{
{ "component", logEvent.Properties["SourceContext"] },
{ "level", logEvent.Level.ToString() }
};

fields[LogFields.Event] = "log";

try
{
fields[LogFields.Message] = logEvent.RenderMessage(_formatProvider);
fields["message.template"] = logEvent.MessageTemplate.Text;

if (logEvent.Exception != null)
{
fields[LogFields.ErrorKind] = logEvent.Exception.GetType().FullName;
fields[LogFields.ErrorObject] = logEvent.Exception;
}

if (logEvent.Properties != null)
{
foreach (var property in logEvent.Properties)
{
fields[property.Key] = property.Value;
}
}
}
catch (Exception logException)
{
fields["mbv.common.logging.error"] = logException.ToString();
}

span.Log(fields);
}
}

public static class OpenTracingSinkExtensions
{
public static LoggerConfiguration OpenTracing(
this LoggerSinkConfiguration loggerConfiguration,
IFormatProvider formatProvider = null)
{
return loggerConfiguration.Sink(new OpenTracingSink(GlobalTracer.Instance, formatProvider));
}
}

关于c# - OpenTracing 不使用 Serilog 发送日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56156809/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com