serilog-contrib / Serilog.Sinks.Logz.Io

Apache License 2.0
15 stars 11 forks source link

"Environment" and "ServiceName" removed from LogzioOptions in version 7.1.0 #31

Closed markdehaas closed 1 year ago

markdehaas commented 1 year ago

This recent change https://github.com/serilog-contrib/Serilog.Sinks.Logz.Io/commit/384d56ded78c46f20818376011739dc7f8c3e0bb as a fix for #29 and released in version 7.1.0 made some breaking changes to the Serilog.Sinks.Logz.Io.LogzioOptions class. Some properties just got moved to a different (nested) object, but the Environment and ServiceName properties seem to have been completely removed.

Was the removal of these two properties intentional? They're not explicitly mentioned in the commit, issue, or changelog. Can they be restored or do these properties need to be configured in a different way now?

mantasaudickas commented 1 year ago

Yes, removal was intentional as you can achieve the same thing by making serilog enricher. Something like:

public class ServiceNameEnricher : ILogEventEnricher
{
    private readonly string _serviceName;

    public ServiceNameEnricher(string serviceName)
    {
        _serviceName = serviceName;
    }

    public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
    {
        if (!string.IsNullOrWhiteSpace(_serviceName))
        {
            logEvent.AddOrUpdateProperty(new LogEventProperty("service", new ScalarValue(_serviceName)));
        }
    }
}

and then use it when configuring logger like this:

    public static IHostBuilder UseSerilogWithServiceName(this IHostBuilder builder)
    {
        return builder
            .UseSerilog((context, configuration) =>
            {
                configuration
                    .ReadFrom.Configuration(context.Configuration);

                var serviceName = context.Configuration["Settings:ServiceName"].ToLower();
                configuration.Enrich.With(new ServiceNameEnricher(serviceName));
            }
    }
markdehaas commented 1 year ago

Thanks. Together with some PropertyTransformationMap entries we managed to get the exact same haviour. 👍