[英]Serilog sink wrapper mapping, separate filename from content
Using the Serilog.Sinks.Map library, the example shows使用Serilog.Sinks.Map库,示例显示
Log.Logger = new LoggerConfiguration()
.WriteTo.Map("Name", "Other", (name, wt) => wt.File($"./logs/log-{name}.txt"))
.CreateLogger();
Log.Information("Hello, {Name}!", "Alice");
Log.CloseAndFlush();
An event written to log-Alice.txt
with text Hello, Alice!
写入log-Alice.txt
的事件,带有文本Hello, Alice!
How do I do similar but separate the filename from the content?我如何做类似但将文件名与内容分开? Eg I want to do例如我想做
Log.Information("Hello!", "Alice");
which should write to a file log-Alice.txt
but with text Hello!
这应该写入文件log-Alice.txt
但带有文本Hello!
Essentially my goal is to log to any named log but there could be hundreds of them.本质上,我的目标是记录到任何命名的日志,但可能有数百个。
Serilog.Sinks.Map
, in your example, is using a property called Name
to define the file name. Serilog.Sinks.Map
在您的示例中,使用名为Name
的属性来定义文件名。 If you don't want to define this property in the message template, you can define it via Context
, for example:如果不想在消息模板中定义这个属性,可以通过Context
来定义,例如:
var log = Log.ForContext("Name", "Alice");
log.Information("Hello!");
log.Information("Hello Again!");
Both messages above would be written to the log-Alice.txt
file, because they both carry a property Name = "Alice"
in the log context.上面的两条消息都将写入log-Alice.txt
文件,因为它们都在日志上下文中带有属性Name = "Alice"
。
Similarly, the below will write each message to a separate file:同样,以下内容会将每条消息写入单独的文件:
Log.ForContext("Name", "John").Information("Hello 1!"); // log-John.txt
Log.ForContext("Name", "Peter").Information("Hello 2!"); // log-Peter.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.