简体   繁体   English

如何将自己的属性添加到Serilog输出模板

[英]How to add my own properties to Serilog output template

I have a small application that is receiving messages from a service bus, which can send through several different types of events for different users. 我有一个小应用程序正在从服务总线接收消息,该服务总线可以为不同的用户发送几种不同类型的事件。 Based on the type of event, a different function is called. 根据事件的类型,调用不同的函数。 I'm logging information in each of these functions. 我正在记录这些功能中的每一个。

I currently have this: 我目前有这个:

var logger = new LoggerConfiguration()
    .MinimumLevel.Information()
    .WriteTo.Console(outputTemplate:
        "[{Timestamp:HH:mm:ss} {Level:u3}] {Message}{NewLine}{Exception}")
    .WriteTo.Loggly()
    .CreateLogger();

...

// logging an event
Log.Information("{UserId} {Event} - Here is my message", "123", "Test Event");

That works fine, but since for this application, every single log will contain both the UserId and Event data in the logs, I figured I could add them to my output template to make my logging code a little cleaner. 这很好,但是因为对于这个应用程序,每个日志都会在日志中包含UserId和Event数据,我想我可以将它们添加到我的输出模板中,以使我的日志代码更加清晰。 So I've tried this: 所以我试过这个:

var logger = new LoggerConfiguration()
    .MinimumLevel.Information()
    .WriteTo.Console(outputTemplate:
      "[{Timestamp:HH:mm:ss} {Level:u3}] {UserId} {Event} - {Message}{NewLine}{Exception}")
    .WriteTo.Loggly()
    .CreateLogger();

...

// logging an event
Log.Information("Here is my message", "123", "Test Event");
Log.Information("Here is my message", new { UserId = "123", Event = "Test Event"});

Neither of those work though, all it outputs is my message, it doesn't pass through the UserId or Event that I passed into it. 这些都不起作用,它输出的只是我的消息,它不会通过我传入其中的UserId或Event。

Am I doing this wrong? 我做错了吗? Or is there even a way to do it at all? 或者甚至有办法做到这一点?

If you want to add properties that aren't part of the message template, then you need to enrich the log context. 如果要添加不属于消息模板的属性,则需要丰富日志上下文。 That means adding the from log context enricher, and adding your properties to your logged event a bit differently. 这意味着添加日志上下文更丰富,并将您的属性添加到您记录的事件有点不同。

Log.Logger = new LoggerConfiguration()
    .Enrich.FromLogContext()
    .MinimumLevel.Information()
    .WriteTo.Console(outputTemplate:
    "[{Timestamp:HH:mm:ss} {Level:u3}] {UserId} {Event} - {Message}{NewLine}{Exception}")
    .CreateLogger();

using (LogContext.PushProperty("UserId", "123"))
using (LogContext.PushProperty("Event", "Test Event"))
{
    Log.Information("Here is my message about order {OrderNumber}", 567);
    Log.Information("Here is my message about product {ProductId}", "SomeProduct");
}

You can learn more about enrichment in the documentation . 您可以在文档中了解有关丰富的更多信息。

Now I'm not sure about Loggly. 现在我不确定Loggly。 I've never used it before. 我以前从未使用过它。 But you don't even need to modify the output template if you're using Seq (which is from the creators of Serilog and works best with it). 但是如果你使用的是Seq(它来自Serilog的创建者并且最适合它),你甚至不需要修改输出模板。 Properties added will automatically be available for each event. 添加的属性将自动用于每个事件。

As Nicholas Blumhardt pointed out via comment, if you just need to one-off add a property to a single logged event, you can do that as well. 正如Nicholas Blumhardt通过评论指出的那样,如果你只需要一次性地为一个记录的事件添加一个属性,你也可以这样做。 I sometimes do this when I have a lot of properties for events that don't necessarily need to show up in the message, and only apply to this single event. 当有很多属性用于不一定需要在消息中显示的事件时,我有时会这样做,并且仅适用于此单个事件。

Log
   .ForContext("OrderNumber", orderNumber)
   .ForContext("UserId", user.Id)
   .Information("Order {OrderNumber} submitted", order.OrderNumber);

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

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