C # - Serilog - Enrichers leave blank entries

Figured out the basics of Serilog. Now, trying to add some enrichments so that I can print the username or machine name or class name, etc. In each line of the journal.

This is the code that I still have,

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("");
    }
}

Output

 |  | 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 . . .

In my opinion, I have to manually define the "placeholders" ( MachineNameor UserNameor ClassName) in outputTemplatewhen creating the object Logger. I am not sure how to make these placeholders optional (i.e. do not print empty space in the log line) if I do not need to use them. (I would not want to print the class name for multiple lines for examples). Or maybe I misunderstand the concept of Enrichers.

Any help is much appreciated!

+4
source share
1

, 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();
  1. ?

, {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();
+2

Source: https://habr.com/ru/post/1694413/


All Articles