弄清楚了 Serilog 的基础知识。现在尝试添加一些丰富器,以便我可以在每个日志行中打印用户名或机器名或类名等。
这是我到目前为止的代码,
using System;
using Serilog;
using Serilog.Sinks.SystemConsole;
using Serilog.Sinks.File;
using Serilog.Enrichers;
using Serilog.Core;
using Serilog.Events;
using System.Threading;
using Serilog.Context;
var outputTemplate = "{MachineName} | {UserName} | {Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u4}] | {ClassName} | {Message:l}{NewLine}{Exception}";
var Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console(outputTemplate: outputTemplate)
.WriteTo.File("logFile-.log",
outputTemplate: outputTemplate)
.Enrich.FromLogContext()
.Enrich.WithEnvironmentUserName()
.Enrich.WithProperty("Version", "1.0.0")
.CreateLogger();
Logger.Information("Hello, Serilog!");
var position = new { Latitude = 25, Longitude = 134 };
var elapsedMs = 35;
for (int i = 0; i < 5; i++)
{
Logger.Information("Processed {@position} in {elapsed} ms.", position, elapsedMs);
Logger.Information("");
}
using (LogContext.PushProperty("SourceContext", "TestClass"))
{
for (int i = 0; i < 5; i++)
{
Logger.Information("Processed {@position} in {elapsed} ms.", position, elapsedMs);
Logger.Information("");
}
}
Run Code Online (Sandbox Code Playgroud)
输出
| | 2018-03-03 19:02:56.247 -05:00 [INFO] | | Hello, Serilog!
| | 2018-03-03 19:02:56.287 -05:00 [INFO] | | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.295 -05:00 [INFO] | |
| | 2018-03-03 19:02:56.295 -05:00 [INFO] | | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.296 -05:00 [INFO] | |
| | 2018-03-03 19:02:56.297 -05:00 [INFO] | | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.298 -05:00 [INFO] | |
| | 2018-03-03 19:02:56.298 -05:00 [INFO] | | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.299 -05:00 [INFO] | |
| | 2018-03-03 19:02:56.300 -05:00 [INFO] | | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.301 -05:00 [INFO] | |
| | 2018-03-03 19:02:56.302 -05:00 [INFO] | TestClass | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.307 -05:00 [INFO] | TestClass |
| | 2018-03-03 19:02:56.308 -05:00 [INFO] | TestClass | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.310 -05:00 [INFO] | TestClass |
| | 2018-03-03 19:02:56.310 -05:00 [INFO] | TestClass | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.311 -05:00 [INFO] | TestClass |
| | 2018-03-03 19:02:56.312 -05:00 [INFO] | TestClass | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.313 -05:00 [INFO] | TestClass |
| | 2018-03-03 19:02:56.316 -05:00 [INFO] | TestClass | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.317 -05:00 [INFO] | TestClass |
Press any key to continue . . .
Run Code Online (Sandbox Code Playgroud)
根据我的理解,我必须在创建对象时手动定义“占位符”(MachineName或UserName或ClassName)。如果我不需要使用它们,我不确定如何使这些占位符可选(即不在日志行中打印空格)。(我不想为示例打印几行的类名)。或者也许我没有正确理解 Enrichers 的概念。outputTemplateLogger
任何帮助是极大的赞赏!!
如果我理解正确的话,你在这里有两个问题:
- 为什么机器名和用户名没有出现在日志中?
机器名称没有出现在日志中,因为您没有添加适当的丰富器 - WithMachineName()。将以下调用添加到配置中:
var Logger = new LoggerConfiguration()
// ...
.Enrich.WithMachineName()
// ...
Run Code Online (Sandbox Code Playgroud)
用户名没有出现在日志中,因为您使用了不正确的占位符,{UserName}而它应该是{EnvironmentUserName}。
这是解决这两个问题的记录器配置:
var outputTemplate = "{MachineName} | {EnvironmentUserName} | {Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u4}] | {ClassNameDelimited} | {Message:l}{NewLine}{Exception}";
var Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console(outputTemplate: outputTemplate)
.WriteTo.File("logFile-.log",
outputTemplate: outputTemplate)
.Enrich.FromLogContext()
.Enrich.WithEnvironmentUserName()
.Enrich.WithMachineName()
.Enrich.WithProperty("Version", "1.0.0")
.CreateLogger();
Run Code Online (Sandbox Code Playgroud)
- 如何使丰富占位符可选?
占位符 like{MachineName}只是替换为属性值。如果缺少值,则将其替换为空字符串。所以实际上占位符是可选的。但是对于这样的输出模板"{MachineName} | Something"将导致" | Something"日志条目。当您定义这样的输出模板时,不会" | "从结果条目中删除常量字符串。
这个问题有一个解决方案,但是它需要一些努力。计划如下:
以下是此类浓缩器的示例:
public class DelimitedEnricher : ILogEventEnricher
{
private readonly ILogEventEnricher innerEnricher;
private readonly string innerPropertyName;
private readonly string delimiter;
public DelimitedEnricher(string innerPropertyName, string delimiter)
{
this.innerPropertyName = innerPropertyName;
this.delimiter = delimiter;
}
public DelimitedEnricher(ILogEventEnricher innerEnricher, string innerPropertyName, string delimiter) : this(innerPropertyName, delimiter)
{
this.innerEnricher = innerEnricher;
}
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
innerEnricher?.Enrich(logEvent, propertyFactory);
LogEventPropertyValue eventPropertyValue;
if (logEvent.Properties.TryGetValue(innerPropertyName, out eventPropertyValue))
{
var value = (eventPropertyValue as ScalarValue)?.Value as string;
if (!String.IsNullOrEmpty(value))
{
logEvent.AddPropertyIfAbsent(new LogEventProperty(innerPropertyName + "Delimited", new ScalarValue(value + delimiter)));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是配置丰富器的扩展方法:
public static class DelimitedEnrichersExtensions
{
public const string Delimiter = " | ";
public static LoggerConfiguration WithEnvironmentUserNameDelimited(this LoggerEnrichmentConfiguration enrichmentConfiguration)
{
return enrichmentConfiguration.With(new DelimitedEnricher(new EnvironmentUserNameEnricher(), EnvironmentUserNameEnricher.EnvironmentUserNamePropertyName, Delimiter));
}
public static LoggerConfiguration WithMachineNameDelimited(this LoggerEnrichmentConfiguration enrichmentConfiguration)
{
return enrichmentConfiguration.With(new DelimitedEnricher(new MachineNameEnricher(), MachineNameEnricher.MachineNamePropertyName, Delimiter));
}
public static LoggerConfiguration WithPropertyDelimited(this LoggerEnrichmentConfiguration enrichmentConfiguration, string propertyName)
{
return enrichmentConfiguration.With(new DelimitedEnricher(propertyName, Delimiter));
}
}
Run Code Online (Sandbox Code Playgroud)
最后,这是一个使用此自定义丰富器的配置:
var outputTemplate = "{MachineNameDelimited}{EnvironmentUserNameDelimited}{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u4}] | {ClassNameDelimited}{Message:l}{NewLine}{Exception}";
var Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console(outputTemplate: outputTemplate)
.WriteTo.File("logFile-.log",
outputTemplate: outputTemplate)
.Enrich.FromLogContext()
.Enrich.WithEnvironmentUserNameDelimited()
.Enrich.WithMachineNameDelimited()
.Enrich.WithPropertyDelimited("ClassName")
.Enrich.WithProperty("Version", "1.0.0")
.CreateLogger();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3676 次 |
| 最近记录: |