gpt4 book ai didi

serilog - 如何将 Serilog 事件写入 .NET 集合?

转载 作者:行者123 更新时间:2023-12-03 20:34:06 25 4
gpt4 key购买 nike

我要写 Serilog Warning .NET 集合中的事件,以便它们可以包含在给用户的报告中。我该怎么做 configure the static Log.Logger 写信给类似 static List<string<> 的东西?

我看到了 Rx Observer in the list of provided sinks ,这是唯一一个似乎很容易使 .NET 对象可用的方法,还是有我错过的明显替代方法?

或者,这是一种愚蠢的方法吗 - 有没有更好的方法来收集 Warning将事件添加到面向用户的报告中?

最佳答案

class CollectionSink : ILogEventSink {
public ICollection<LogEvent> Events { get; } = new ConcurrentBag<LogEvent>();

public void Emit(LogEvent le) {
Events.Add(le);
}
}

var col = new CollectionSink();
Log.Logger = new LoggerConfiguration()
.WriteTo.Sink(col, LogEventLevel.Warning)
.CreateLogger();

// read col.Events....

不确定这个会不会打字,但这基本上是我做过几次的方式。

关于serilog - 如何将 Serilog 事件写入 .NET 集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36327580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com