serilog-sinks-graylog icon indicating copy to clipboard operation
serilog-sinks-graylog copied to clipboard

Make DefaultSerializerSettings non-readonly

Open antgraf opened this issue 1 year ago • 4 comments

As I understand the idea of DefaultSerializerSettings is to override JSON settings. But in the current implementation it's impossible to assign it. Changing the pre-created object is also not possible because JsonSerializerOptions uses caching and immutable state after creation (at least in .NET 7, see ConfigurationList<TItem>.IsImmutable & JsonSerializerOptions.IsImmutable). Proposal: remove "readonly" modifier, so the field could be re-assigned before a logger creation. Alternately, JsonSerializerOptions may be added as a parameter to configuration.

antgraf avatar Oct 11 '23 19:10 antgraf

@antgraf you should not change DefaultSerializerSettings. I'm not sure that the configuration system will be able to correctly read the settings for JsonSerializer. If you want to change json serializer settings then you should make GraylogSinkOptions like this

            var loggerConfig = new LoggerConfiguration();
            loggerConfig.WriteTo.Graylog(new GraylogSinkOptions
            {
                ShortMessageMaxLength = 50,
                MinimumLogEventLevel = LogEventLevel.Information,
                Facility = "GELF",
                HostnameOrAddress = "localhost",
                Port = 12209,
                UseGzip = false,
                JsonSerializerOptions = new JsonSerializerOptions
                {
                    WriteIndented = true,
                    Encoder = new JavaScriptTestEncoder()
                },
                TransportType = TransportType.Udp
            });

whir1 avatar Oct 16 '23 16:10 whir1

@whir1 Is it possible with configuration file?

antgraf avatar Oct 17 '23 00:10 antgraf

no it's not possible with config files. But i think you can use combination with configuration and startup code to make SinkOptions

whir1 avatar Oct 17 '23 07:10 whir1

@whir1 is there an example of mixing config and runtime options? I have an impression that after instantiating it with a config there is no way to apply more changes programmatically. Thanks.

antgraf avatar Oct 18 '23 21:10 antgraf