Is it possible to get Timestamp in output template as DateTimeKind.Utc?

前端 未结 1 1644
被撕碎了的回忆
被撕碎了的回忆 2021-01-17 15:12

Currently when I use {Timestamp} in an outputTemplate it appears to have been generated by DateTime.Now and therefore being of DateTimeKind.L

1条回答
  •  清歌不尽
    2021-01-17 15:46

    It appears the limitation in DateTimeOffset formatting is going to thwart this.

    An alternative (so long as the additional property doesn't bloat output somewhere else) is to add a Serilog ILogEventEnricher to the pipeline:

    class UtcTimestampEnricher : ILogEventEnricher {
      public void Enrich(LogEvent logEvent, ILogEventPropertyFactory lepf) {
        logEvent.AddPropertyIfAbsent(
          lepf.CreateProperty("UtcTimestamp", logEvent.Timestamp.UtcDateTime));
      }
    }
    

    You can then use {UtcTimestamp:o} in your output template to get the value you need.

    0 讨论(0)
提交回复
热议问题