serilog下沉xamarin标签在日志中不可见

时间:2017-03-18 15:52:08

标签: xamarin xamarin.android serilog

我正在使用serilog / serilog-sinks-xamarin,如here所述

在.Android项目中,我添加了以下代码:

    Log.Logger = new LoggerConfiguration().WriteTo.AndroidLog().Enrich.WithProperty("Tag", "CustomTag").CreateLogger();

从便携式课程中调用它:

Log.Information("App is Starting");

我可以在设备日志中看到此行,但标记为空白: missing tag

1 个答案:

答案 0 :(得分:0)

Enrich.WithProperty必须设置为Constants.SourceContextPropertyName,其中Serilog是值的常量字符串:SourceContext

LoggerConfiguration中使用“SourceContext”:

Log.Logger = new LoggerConfiguration().WriteTo.AndroidLog().Enrich.WithProperty("SourceContext", "CustomTag").CreateLogger();

serilog-sinks-xamarin中,日志标记通过以下方式分配:

var tag = logEvent.Properties.Where(x => x.Key == Constants.SourceContextPropertyName).Select(x => x.Value.ToString("l", null)).FirstOrDefault() ?? "";

re:https://github.com/serilog/serilog-sinks-xamarin/blob/dec633b488a5c7dd3f3c9a017b972eaf101a177c/src/Serilog.Sinks.Xamarin.Droid/Sinks/Xamarin/AndroidLogSink.cs#L54

re:https://github.com/serilog/serilog/blob/32e0c9578db720add74720bceb62bd46967694c4/src/Serilog/Core/Constants.cs#L27