弄清楚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("");
}
}
产量
| | 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 . . .
根据我的理解,我必须在创建MachineName
对象时手动定义UserName
中的“占位符”(ClassName
或outputTemplate
或Logger
)。如果我不需要使用它们,我不知道如何使这些占位符可选(即不在日志行中打印空格)。 (我不想为几行打印类名)。或者我可能不理解Enrichers的概念。
任何帮助是极大的赞赏!!
如果我理解正确,你有两个问题:
- 为什么计算机名称和用户名不会出现在日志中?
机器名称不会出现在日志中,因为您尚未添加适当的richher - WithMachineName()
。添加以下对配置的调用:
var Logger = new LoggerConfiguration()
// ...
.Enrich.WithMachineName()
// ...
用户名不会出现在日志中,因为您使用了错误的占位符{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();
- 如何使丰富的占位符可选?
像{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)));
}
}
}
}
以下是配置richhers的扩展方法:
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));
}
}
最后,这是一个使用此自定义Richher的配置:
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();