我的Metrolog输出看起来像是以GMT登录。我的时区是GMT +5.30。设备上的时间设置正确。如何让Metrolog登录当地时间?
示例日志:4|2017-05-15T05:05:36.6887812+00:00|TRACE|3|App|Analytics configured
答案 0 :(得分:4)
您可以通过创建一个扩展 MetroLog.Layouts.Layout 的类来更改跟踪布局,您可以根据您的时区显示 DateTime :
public class CustomLayout : MetroLog.Layouts.Layout
{
/// <summary>
/// Create a formatted string based on given informations
/// </summary>
/// <param name="context"><see cref="LogWriteContext"/></param>
/// <param name="info"><see cref="LogEventInfo"/></param>
/// <returns>Formatted string to log</returns>
public override string GetFormattedString(LogWriteContext context, LogEventInfo info)
{
return $"{info.SequenceID}|{info.TimeStamp.LocalDateTime}|{info.Level}|{info.Logger}|{info.Message}|{info.Exception}";
}
}
初始化目标时不要忘记使用此自定义布局。例如,如果您使用 DebugTarget ,则应执行以下操作:
var loggingConfiguration = new LoggingConfiguration { IsEnabled = true };
loggingConfiguration.AddTarget(LogLevel.Trace, LogLevel.Fatal, new DebugTarget(new CustomLayout()));
然后创建您的日志管理器:
var logManager = LogManagerFactory.CreateLogManager(loggingConfiguration);