Esempio n. 1
0
        // Capture Serilog configuration provided programmatically using reflection
        public void SetSerilogOptions(Serilog.LoggerConfiguration loggerConfiguration)
        {
            var minLevelProperty = loggerConfiguration.GetType().GetField("_minimumLevel", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            var minimumLevel     = (LogEventLevel)minLevelProperty.GetValue(loggerConfiguration);

            var overridesProperty = loggerConfiguration.GetType().GetField("_overrides", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            var overrideSwitches  = (Dictionary <string, Serilog.Core.LoggingLevelSwitch>)overridesProperty.GetValue(loggerConfiguration);

            Dictionary <string, LogEventLevel> overrideLevels = new ();

            foreach (var overrideSwitch in overrideSwitches)
            {
                overrideLevels.Add(overrideSwitch.Key, overrideSwitch.Value.MinimumLevel);
            }

            MinimumLevel = new MinimumLevel()
            {
                Default  = minimumLevel,
                Override = overrideLevels ?? new Dictionary <string, LogEventLevel>()
            };

            _serilogConfiguration = loggerConfiguration.ClearLevels(MinimumLevel);
        }