[英]Serilog not surfacing my custom context properties
I am using Serilog with WebApi. 我在WebApi中使用Serilog。 I would like to add a Username and class name to every error message.
我想为每个错误消息添加一个用户名和类名。
Here's my code as it stands: 这是我的代码:
public partial class Repo : BaseRepo<db>
{
public Repo(ILogger logger) : base(logger)
{
/// Not working
// Logger.ForContext("UserId", currentuser).ForContext<Repo>();
logger.ForContext("UserId", currentuser).Information("message")
}
The message is rendering as: 该消息呈现为:
INF message
How do I get userid: <currentUser>
to show? 如何获取
userid: <currentUser>
显示?
The message that is displayed for you depends on the output template of the sink you're using (if supported). 为您显示的消息取决于您正在使用的接收器的输出模板 (如果支持)。 If you are using the Console Sink , for example, then you need to add
{Properties:j}
to the outputTemplate
parameter when configuring the sink: 例如,如果您使用的是控制台接收器 ,则在配置接收器时需要将
{Properties:j}
添加到outputTemplate
参数:
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(LogEventLevel.Debug,
outputTemplate: "[{Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}")
.CreateLogger();
Also, if you want to display custom properties that you'll add via LogContext
, then you need to Enrich.FromLogContext()
too: 另外,如果要显示要通过
LogContext
添加的自定义属性,则也需要Enrich.FromLogContext()
:
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(LogEventLevel.Debug,
outputTemplate: "[{Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}")
.Enrich.FromLogContext()
.CreateLogger();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.