[英]Any way to read Serilog's LogContext later in a downstream class?
I'm pushing values to the Serilog LogContext
in a middleware class of a .NET Core web app:-我正在将值推送到 .NET Core
LogContext
应用程序的中间件 class 中的 Serilog LogContext:-
using LogContext.PushProperty("MyAct", "some clever joke") ...
Is it possible to read the value for MyAct
property in a downstream class?是否可以读取下游 class 中
MyAct
属性的值? I don't see anything promising on the LogContext class - seems like a write only entity, but maybe I am missing something?我在 LogContext class 上看不到任何有前途的东西 - 似乎是一个只写实体,但也许我遗漏了什么?
I have a similar problem.我有一个类似的问题。 I want to write a Unit test for my Middleware that pushes context and want to test if I am pushing the right context information.
我想为我的中间件编写一个单元测试来推送上下文,并想测试我是否推送了正确的上下文信息。
I solved it by creating a Logger that writes to an observer.我通过创建一个写入观察者的记录器解决了这个问题。 (Adding the Serilog.Sinks.Observable to my test dependencies)
(将 Serilog.Sinks.Observable 添加到我的测试依赖项中)
private class LogEventStoreObserver : IObserver<LogEvent>
{
public LogEvent LastEvent { get; set; }
public void OnCompleted() { }
public void OnError(Exception error) { }
public void OnNext(LogEvent value)
{
LastEvent = value;
}
}
Log.Logger = new LoggerConfiguration().WriteTo.Observers(events =>
events.Subscribe(logObserver))
.Enrich.FromLogContext()
.CreateLogger();
Now I can read the LogEvent properties which contains the LogContext like this:现在我可以像这样读取包含 LogContext 的 LogEvent 属性:
// Setup
LogEvent testLogEvent = null;
nextMock.Setup(_ => _(It.IsAny<HttpContext>())).Callback<HttpContext>((_) =>
{
Log.Information("Test");
testLogEvent = logObserver.LastEvent;
});
// Act
await middleware.InvokeAsync(httpContext);
// Verify
testLogEvent.Properties.Should().ContainKey("mySpecialContext");
Serilog does not provide for you to Just Read the values from the LogContext
. Serilog 不提供您从
LogContext
中读取值。
Normally the PushProperty
stashes it, and Enrich.FromLogContext
is charged with grabbing the contextual information that will accompany the destructured message and tokens as the logging call is capturing them into the LogEvent
that'll then be passed to the chain of sinks .通常
PushProperty
将其存储起来,并且Enrich.FromLogContext
负责获取将伴随解构消息和令牌的上下文信息,因为日志调用将它们捕获到LogEvent
中,然后将其传递到sinks链。
Really this begs the question: why do you feel you need to do this ?这真的引出了一个问题:你为什么觉得你需要这样做?
Actually answering the question: It is possible the underlying APIs are exposed in a usable fashion though;实际上回答了这个问题:尽管底层 API 可能以可用的方式公开; I'd go to https://github.com/serilog/serilog - the
LogContext
impl and tests are all in that core repo.我将 go 到https://github.com/serilog/serilog -
LogContext
impl 和测试都在该核心仓库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.