【问题标题】:ServiceStack structured loggingServiceStack 结构化日志记录
【发布时间】:2020-06-07 09:23:11
【问题描述】:

如何在使用时获得结构化日志记录,例如Serilog 与 Servicestack?

Serilog 和 NLog 中的示例都有 Log.Information("Hello World from {FirstName}", "Thomas"); 的形式,我在 ServiceStack 中找不到匹配的方法签名。

【问题讨论】:

  • DebugFormatInfoFormatWarnFormatErrorFormat
  • @RolfKristensen 他们是如何工作的? log.PushProperty("FirstName", "Thomas") 然后log.InfoFormat("Hello World from {FirstName}"); 会起作用吗?所以我需要写两行?
  • @speciman 您要求的内容与Log.Information 匹配。如果您想要与Log.PushProperty 匹配的内容,那么您应该更新您的问题。
  • @RolfKristensen - 我没有在文档中看到 xxxFormat 方法,这让我很困惑。它们正是我想要的。

标签: servicestack


【解决方案1】:

请参阅 enhanced Serilog Logging APIs 的日志记录文档,而此 existing StackOverflow answer 显示了 Serilog Enrichers 示例。

SerilogLoggerTests.cs 展示了使用 Serilog 的不同示例:

var log = new SerilogLogger(GetType());

const string message = "Error Message";
const string messageFormat = "Message Format: message: {0}, exception: {1}";

var ex = new Exception();
log.Info(message);
log.Info(message, ex);
log.InfoFormat(messageFormat, message, ex.Message);
log.Info(ex, messageFormat, messageFormat, ex);

还有一个带有自定义属性的日志上下文示例:

var log = new SerilogLogger(new LoggerConfiguration()
    .WriteTo.Sink(sink).CreateLogger());

var messageTemplate = "Testing adding {prop2} props";
log.ForContext("prop", "value").InfoFormat(messageTemplate, "awesome");

还有用于分配自定义属性的 PushProperty() API,有关示例,请参见 Serilog's Enrichment docs

【讨论】:

  • 已经看过文档,但不明白。似乎他们都需要一个例外才能工作,例如ILog.Info(Exception ex, string messageTemplate, params object[] propertyValues)。我没有例外,只是想记录模板+参数。我可以将null 作为第一个参数传递吗?
  • @specimen 您可以使用不同的重载,请参阅更新的答案。
  • 我没有在文档中看到 xxxFormat 方法,这让我很困惑。它们正是我想要的。
猜你喜欢
  • 2018-03-24
  • 2012-09-03
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2019-09-12
  • 1970-01-01
相关资源
最近更新 更多