, 2 :
, enricher - WithMachineName().
:
var Logger = new LoggerConfiguration()
.Enrich.WithMachineName()
, placeholder {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". , string " | ".
, . :
:
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)));
}
}
}
}
:
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));
}
}
, , :
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();