From 18cb4c09c55bb349fb80576c20de27e1d7ea6469 Mon Sep 17 00:00:00 2001 From: John Crnjanin Date: Sun, 17 Jan 2016 16:18:02 +1000 Subject: [PATCH 1/2] Refactored how directives are applied to loggerConfiguration --- .../KeyValuePairs/KeyValuePairSettings.cs | 84 +++++++++---------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs b/src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs index 8612f313c..e347d0658 100644 --- a/src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs +++ b/src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs @@ -90,11 +90,11 @@ public void Configure(LoggerConfiguration loggerConfiguration) var sinkDirectives = (from wt in directives where splitWriteTo.IsMatch(wt.Key) let match = splitWriteTo.Match(wt.Key) - let call = new + let call = new MethodArgumentValue { Method = match.Groups["method"].Value, Argument = match.Groups["argument"].Value, - wt.Value + Value = wt.Value } group call by call.Method).ToList(); @@ -102,61 +102,48 @@ where splitWriteTo.IsMatch(wt.Key) { var configurationAssemblies = LoadConfigurationAssemblies(directives); - if (eventEnricherDirectives.Any()) - { - var eventEnricherTypes = FindEventEnricherConfigurationMethods(configurationAssemblies); + ApplyDirectives(eventEnricherDirectives, () => FindEventEnricherConfigurationMethods(configurationAssemblies), loggerConfiguration.Enrich); + ApplyDirectives(sinkDirectives, () => FindSinkConfigurationMethods(configurationAssemblies), loggerConfiguration.WriteTo); + } + } - foreach(var eventEnricherDirective in eventEnricherDirectives) - { - var target = eventEnricherTypes - .Where(e => e.Name == eventEnricherDirective && - e.GetParameters().Skip(1).All(p => -#if NET40 - (p.Attributes & ParameterAttributes.HasDefault) != ParameterAttributes.None -#else - p.HasDefaultValue -#endif - )) - .OrderByDescending(m => m.GetParameters().Length) - .FirstOrDefault(); - - if (target != null) - { - target.Invoke(null, new object[] { loggerConfiguration.Enrich }); - } - } - } + private static void ApplyDirectives(IEnumerable directives, Func> getConfigurationMethods, object loggerConfigMethod) + { + var directivesGrouped = directives.Select(d => new MethodArgumentValue { Method = d }).GroupBy(m => m.Method).ToList(); + ApplyDirectives(directivesGrouped, getConfigurationMethods, loggerConfigMethod); + } - if (sinkDirectives.Any()) - { - var sinkConfigurationMethods = FindSinkConfigurationMethods(configurationAssemblies); - foreach (var sinkDirective in sinkDirectives) - { - var target = sinkConfigurationMethods - .Where(m => m.Name == sinkDirective.Key && - m.GetParameters().Skip(1).All(p => + private static void ApplyDirectives(List> directives, Func> getConfigurationMethods, object loggerConfigMethod) + { + if (directives.Any()) + { + var configurationMethods = getConfigurationMethods(); + + foreach (var directiveInfo in directives) + { + var target = configurationMethods + .Where(m => m.Name == directiveInfo.Key && + m.GetParameters().Skip(1).All(p => #if NET40 (p.Attributes & ParameterAttributes.HasDefault) != ParameterAttributes.None #else p.HasDefaultValue #endif - || sinkDirective.Any(s => s.Argument == p.Name))) - .OrderByDescending(m => m.GetParameters().Length) - .FirstOrDefault(); + || directiveInfo.Any(s => s.Argument == p.Name))) + .OrderByDescending(m => m.GetParameters().Length) + .FirstOrDefault(); - if (target != null) - { - var config = loggerConfiguration.WriteTo; + if (target != null) + { - var call = (from p in target.GetParameters().Skip(1) - let directive = sinkDirective.FirstOrDefault(s => s.Argument == p.Name) - select directive == null ? p.DefaultValue : ConvertToType(directive.Value, p.ParameterType)).ToList(); + var call = (from p in target.GetParameters().Skip(1) + let directive = directiveInfo.FirstOrDefault(s => s.Argument == p.Name) + select directive == null ? p.DefaultValue : ConvertToType(directive.Value, p.ParameterType)).ToList(); - call.Insert(0, config); + call.Insert(0, loggerConfigMethod); - target.Invoke(null, call.ToArray()); - } + target.Invoke(null, call.ToArray()); } } } @@ -230,5 +217,12 @@ internal static IList FindConfigurationMethods(IEnumerable .Where(m => m.GetParameters()[0].ParameterType == configType) .ToList(); } + + private class MethodArgumentValue + { + public string Method { get; set; } + public string Argument { get; set; } + public string Value { get; set; } + } } } From 54fde6c39c08401a3b4ce9e45cb06d5f3eb80bf5 Mon Sep 17 00:00:00 2001 From: John Crnjanin Date: Sun, 17 Jan 2016 21:57:26 +1000 Subject: [PATCH 2/2] Event enrichers from config refactoring --- .../KeyValuePairs/KeyValuePairSettings.cs | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs b/src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs index e347d0658..3c6ad9cc4 100644 --- a/src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs +++ b/src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs @@ -81,10 +81,6 @@ public void Configure(LoggerConfiguration loggerConfiguration) loggerConfiguration.Enrich.WithProperty(name, enrichProperyDirective.Value); } - var eventEnricherDirectives = directives.Where(dir => - dir.Key.StartsWith(EnrichWithEventEnricherPrefix) && !dir.Key.StartsWith(EnrichWithPropertyDirectivePrefix) && dir.Key.Length > EnrichWithEventEnricherPrefix.Length) - .Select(dir => dir.Key.Substring(EnrichWithEventEnricherPrefix.Length)); - var splitWriteTo = new Regex(WriteToDirectiveRegex); var sinkDirectives = (from wt in directives @@ -98,53 +94,57 @@ where splitWriteTo.IsMatch(wt.Key) } group call by call.Method).ToList(); + var eventEnricherDirectives = (from er in directives + where er.Key.StartsWith(EnrichWithEventEnricherPrefix) && !er.Key.StartsWith(EnrichWithPropertyDirectivePrefix) && er.Key.Length > EnrichWithEventEnricherPrefix.Length + let match = er.Key.Substring(EnrichWithEventEnricherPrefix.Length) + let call = new MethodArgumentValue + { + Method = match + } + group call by call.Method).ToList(); + if (sinkDirectives.Any() || eventEnricherDirectives.Any()) { var configurationAssemblies = LoadConfigurationAssemblies(directives); - ApplyDirectives(eventEnricherDirectives, () => FindEventEnricherConfigurationMethods(configurationAssemblies), loggerConfiguration.Enrich); - ApplyDirectives(sinkDirectives, () => FindSinkConfigurationMethods(configurationAssemblies), loggerConfiguration.WriteTo); - } - } + if (sinkDirectives.Any()) + { + ApplyDirectives(sinkDirectives, FindSinkConfigurationMethods(configurationAssemblies), loggerConfiguration.WriteTo); + } - private static void ApplyDirectives(IEnumerable directives, Func> getConfigurationMethods, object loggerConfigMethod) - { - var directivesGrouped = directives.Select(d => new MethodArgumentValue { Method = d }).GroupBy(m => m.Method).ToList(); - ApplyDirectives(directivesGrouped, getConfigurationMethods, loggerConfigMethod); + if (eventEnricherDirectives.Any()) + { + ApplyDirectives(eventEnricherDirectives, FindEventEnricherConfigurationMethods(configurationAssemblies), loggerConfiguration.Enrich); + } + } } - - private static void ApplyDirectives(List> directives, Func> getConfigurationMethods, object loggerConfigMethod) + private static void ApplyDirectives(List> directives, IList configurationMethods, object loggerConfigMethod) { - if (directives.Any()) + foreach (var directiveInfo in directives) { - var configurationMethods = getConfigurationMethods(); - - foreach (var directiveInfo in directives) - { - var target = configurationMethods - .Where(m => m.Name == directiveInfo.Key && - m.GetParameters().Skip(1).All(p => + var target = configurationMethods + .Where(m => m.Name == directiveInfo.Key && + m.GetParameters().Skip(1).All(p => #if NET40 - (p.Attributes & ParameterAttributes.HasDefault) != ParameterAttributes.None + (p.Attributes & ParameterAttributes.HasDefault) != ParameterAttributes.None #else - p.HasDefaultValue + p.HasDefaultValue #endif - || directiveInfo.Any(s => s.Argument == p.Name))) - .OrderByDescending(m => m.GetParameters().Length) - .FirstOrDefault(); + || directiveInfo.Any(s => s.Argument == p.Name))) + .OrderByDescending(m => m.GetParameters().Length) + .FirstOrDefault(); - if (target != null) - { + if (target != null) + { - var call = (from p in target.GetParameters().Skip(1) - let directive = directiveInfo.FirstOrDefault(s => s.Argument == p.Name) - select directive == null ? p.DefaultValue : ConvertToType(directive.Value, p.ParameterType)).ToList(); + var call = (from p in target.GetParameters().Skip(1) + let directive = directiveInfo.FirstOrDefault(s => s.Argument == p.Name) + select directive == null ? p.DefaultValue : ConvertToType(directive.Value, p.ParameterType)).ToList(); - call.Insert(0, loggerConfigMethod); + call.Insert(0, loggerConfigMethod); - target.Invoke(null, call.ToArray()); - } + target.Invoke(null, call.ToArray()); } } }