[英]OpenTracing doesn't send logs with Serilog
I'm trying to use OpenTracing.Contrib.NetCore with Serilog. 我正在尝试使用Serilog的OpenTracing.Contrib.NetCore 。 I need to send to Jaeger my custom logs.
我需要向Jaeger发送我的自定义日志。 Now, it works only when I use default logger factory
Microsoft.Extensions.Logging.ILoggerFactory
现在,它仅在我使用默认记录器工厂
Microsoft.Extensions.Logging.ILoggerFactory
时才有效
My Startup: 我的创业公司:
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();
}
and somewhere in 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";
}
}
in a result I will able to see that log in Jaeger UI 在结果中,我将能够在Jaeger UI中看到该日志
But when I use Serilog, there are no any custom logs. 但是当我使用Serilog时,没有任何自定义日志。 I've added
UseSerilog()
to WebHostBuilder
, and all custom logs I can see in console but not in Jaeger. 我已经将
UseSerilog()
添加到WebHostBuilder
,以及我可以在控制台中看到的所有自定义日志,但不能在Jaeger中看到。 There is open issue in github . github中存在未解决的问题。 Could you please suggest how I can use Serilog with OpenTracing?
你能否建议我如何使用Serilog和OpenTracing?
This is a limitation in the Serilog logger factory implementation; 这是Serilog记录仪工厂实施的一个限制; in particular, Serilog currently ignores added providers and assumes that Serilog Sinks will replace them instead.
特别是,Serilog目前忽略了添加的提供商,并假设Serilog Sinks将取而代之。
So, the solutions is implementaion a simple WriteTo.OpenTracing()
method to connect Serilog directly to OpenTracing
因此,解决方案是实现一个简单的
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));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.