如何将我自己的属性添加到 Serilog 输出模板 [英] How to add my own properties to Serilog output template

查看:51
本文介绍了如何将我自己的属性添加到 Serilog 输出模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小应用程序,它从服务总线接收消息,它可以为不同的用户发送几种不同类型的事件.根据事件的类型,调用不同的函数.我正在记录每个函数中的信息.

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.

我目前有这个:

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");

这很好用,但是由于对于这个应用程序,每个日志都将包含日志中的 UserId 和 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. 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"});

虽然这些都不起作用,但它输出的只是我的消息,它不会通过我传递给它的 UserId 或 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.

我这样做错了吗?或者有没有办法做到这一点?

Am I doing this wrong? Or is there even a way to do it at all?

推荐答案

如果要添加不属于消息模板的属性,则需要丰富日志上下文.这意味着添加 FromLogContext 扩充器,并以稍微不同的方式将您的属性添加到记录的事件中.

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 FromLogContext 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.

现在我不确定 Loggly.我以前从未使用过它.但是,如果您使用的是 Seq(来自 Serilog 的创建者并且最适合它),则您甚至不需要修改输出模板.添加的属性将自动可用于每个事件.

Now I'm not sure about 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). Properties added will automatically be available for each event.

正如 Nicholas Blumhardt 通过评论指出的那样,如果您只需要一次性向单个记录的事件添加一个属性,您也可以这样做.当我有很多事件属性不一定需要显示在消息中时,我有时会这样做,并且只适用于这个单个事件.

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. 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 submitted");

这篇关于如何将我自己的属性添加到 Serilog 输出模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆