簡體   English   中英

OpenTracing不會使用Serilog發送日志

[英]OpenTracing doesn't send logs with Serilog

我正在嘗試使用Serilog的OpenTracing.Contrib.NetCore 我需要向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();
}

在控制器的某個地方:

[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中看到該日志 在此輸入圖像描述

但是當我使用Serilog時,沒有任何自定義日志。 我已經將UseSerilog()添加到WebHostBuilder ,以及我可以在控制台中看到的所有自定義日志,但不能在Jaeger中看到。 github中存在未解決的問題。 你能否建議我如何使用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));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM