简体   繁体   English

向 Serilog 添加自定义属性

[英]Add custom properties to Serilog

I'm using Serilog with an MS SQL Server sink in my application.我在我的应用程序中使用 Serilog 和 MS SQL Server 接收器。 Let's assume I have defined the following class ...让我们假设我已经定义了以下类......

public class Person
{
  public string FirstName { get; set; }
  public string LastName { get; set; }

  public DateTime BirthDate { get; set; }
  // ... more properties
}

... and created an instance: ...并创建了一个实例:

var person = new Person
{
    FirstName = "John",
    LastName = "Doe",
    BirthDate = DateTime.UtcNow.AddYears(-25)
};

I have placed the following log call in my code:我在我的代码中放置了以下日志调用:

Log.Information("New user: {FirstName:l} {LastName:l}",
    person.FirstName, person.LastName);

Is it possible to also log the BirthDate property without adding it to the message template so that it's rendered within the Properties XML column?是否也可以记录BirthDate属性而不将其添加到消息模板中,以便在Properties XML 列中呈现它? I'd like to output it later in a details view of my application's log viewer.我想稍后在我的应用程序日志查看器的详细信息视图中输出它。

I'm basically looking for a behavior similar to the object destructuring, but without printing the flat object as part of the log message.我基本上是在寻找类似于对象解构的行为,但没有将平面对象打印为日志消息的一部分。

This is as simple as:这很简单:

Log.ForContext("BirthDate", person.BirthDate)
   .Information("New user: {FirstName:l} {LastName:l}",
                           person.FirstName, person.LastName);

You can actually do this in a few different ways.您实际上可以通过几种不同的方式来做到这一点。 In your case, the first way is probably the best:在您的情况下,第一种方法可能是最好的:

Log.ForContext("BirthDate", person.BirthDate)
    .Information("New user: {FirstName:l} {LastName:l}",
        person.FirstName, person.LastName);

But you can also use the LogContext in other scenarios:但您也可以在其他场景中使用LogContext

Log.Logger = new LoggerConfiguration()
    // Enrich all log entries with properties from LogContext
    .Enrich.FromLogContext();

using (LogContext.PushProperty("BirthDate", person.BirthDate))
{
    Log.Information("New user: {FirstName:l} {LastName:l}",
        person.FirstName, person.LastName);
}

Or, in the case where you want to log a "constant" property, you can add it like this:或者,如果您想记录“常量”属性,您可以像这样添加它:

Log.Logger = new LoggerConfiguration()
    // Enrich all log entries with property
    .Enrich.WithProperty("Application", "My Application");

See Context and correlation – structured logging concepts in .NET (5) for more information.有关更多信息,请参阅上下文和相关性 - .NET 中的结构化日志概念 (5)

If you're using the generic Microsoft ILogger interface you can use BeginScope;如果您使用的是通用的 Microsoft ILogger 接口,则可以使用 BeginScope;

using (_logger.BeginScope(new Dictionary<string, object> { { "LogEventType", logEventType }, { "UserName",  userName } }))
{
    _logger.LogInformation(message, args);
}

This is discussed here;这在这里讨论; https://blog.rsuter.com/logging-with-ilogger-recommendations-and-best-practices/ https://blog.rsuter.com/logging-with-ilogger-recommendations-and-best-practices/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM