From e35d82b62fc01c9dd1a215eee5832608f63b56f1 Mon Sep 17 00:00:00 2001 From: John Crnjanin Date: Sun, 17 Jan 2016 09:44:42 +1000 Subject: [PATCH] Event enrichers by settings uses extension methods Keeps syntax in settings the same as if using config object and uses established extension methods as entry points rather than directly activating the enrichers. --- .../KeyValuePairs/KeyValuePairSettings.cs | 54 +++++++++---------- .../Settings/AppSettingsTests.cs | 10 ++-- .../Settings/KeyValuePairSettingsTests.cs | 12 ++--- test/Serilog.Tests/app.config | 10 ++-- 4 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs b/src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs index b74e9f770..8612f313c 100644 --- a/src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs +++ b/src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs @@ -32,11 +32,11 @@ class KeyValuePairSettings : ILoggerSettings const string UsingDirective = "using"; const string WriteToDirective = "write-to"; const string MinimumLevelDirective = "minimum-level"; - const string EnrichWithDirective = "enrich:with"; + const string EnrichWithDirective = "enrich"; const string EnrichWithPropertyDirective = "enrich:with-property"; const string UsingDirectiveFullFormPrefix = "using:"; - const string EnrichWithEventEnricherPrefix = "enrich:with:"; + const string EnrichWithEventEnricherPrefix = "enrich:"; const string EnrichWithPropertyDirectivePrefix = "enrich:with-property:"; const string WriteToDirectiveRegex = @"^write-to:(?[A-Za-z0-9]*)(\.(?[A-Za-z0-9]*)){0,1}$"; @@ -82,11 +82,8 @@ public void Configure(LoggerConfiguration loggerConfiguration) } var eventEnricherDirectives = directives.Where(dir => - dir.Key.StartsWith(EnrichWithEventEnricherPrefix) && dir.Key.Length > EnrichWithEventEnricherPrefix.Length) - .Select(dir => { - var enricherName = dir.Key.Substring(EnrichWithEventEnricherPrefix.Length); - return !enricherName.EndsWith("Enricher") ? String.Format("{0}Enricher", enricherName) : enricherName; - }); + 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); @@ -107,22 +104,27 @@ where splitWriteTo.IsMatch(wt.Key) if (eventEnricherDirectives.Any()) { - var eventEnricherTypes = FindEventEnrichers(configurationAssemblies); + var eventEnricherTypes = FindEventEnricherConfigurationMethods(configurationAssemblies); foreach(var eventEnricherDirective in eventEnricherDirectives) { var target = eventEnricherTypes - .Where(e => e.AsType().Name == eventEnricherDirective) + .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) { - var instansiatedTarget = Activator.CreateInstance(target.AsType()) as Core.ILogEventEnricher; - loggerConfiguration.Enrich.With(instansiatedTarget); + target.Invoke(null, new object[] { loggerConfiguration.Enrich }); } } - - //eventEnricherDirectives } if (sinkDirectives.Any()) @@ -204,6 +206,16 @@ internal static object ConvertToType(string value, Type toType) } internal static IList FindSinkConfigurationMethods(IEnumerable configurationAssemblies) + { + return FindConfigurationMethods(configurationAssemblies, typeof(LoggerSinkConfiguration)); + } + + internal static IList FindEventEnricherConfigurationMethods(IEnumerable configurationAssemblies) + { + return FindConfigurationMethods(configurationAssemblies, typeof(LoggerEnrichmentConfiguration)); + } + + internal static IList FindConfigurationMethods(IEnumerable configurationAssemblies, Type configType) { return configurationAssemblies .SelectMany(a => a. @@ -215,21 +227,7 @@ internal static IList FindSinkConfigurationMethods(IEnumerable t.GetTypeInfo()).Where(t => t.IsSealed && t.IsAbstract && !t.IsNested)) .SelectMany(t => t.DeclaredMethods) .Where(m => m.IsStatic && m.IsPublic && m.IsDefined(typeof(ExtensionAttribute), false)) - .Where(m => m.GetParameters()[0].ParameterType == typeof(LoggerSinkConfiguration)) - .ToList(); - } - - internal static IList FindEventEnrichers(IEnumerable configurationAssemblies) - { - var logEventEnricherInterface = typeof(Core.ILogEventEnricher).GetTypeInfo(); - return configurationAssemblies - .SelectMany(a => a. -#if NET40 - GetTypes().Select(t => t.GetTypeInfo()) -#else - DefinedTypes -#endif - .Where(t => logEventEnricherInterface.IsAssignableFrom(t) && !t.IsAbstract)) + .Where(m => m.GetParameters()[0].ParameterType == configType) .ToList(); } } diff --git a/test/Serilog.Tests/Settings/AppSettingsTests.cs b/test/Serilog.Tests/Settings/AppSettingsTests.cs index 33737ec8c..be4b6a3cb 100644 --- a/test/Serilog.Tests/Settings/AppSettingsTests.cs +++ b/test/Serilog.Tests/Settings/AppSettingsTests.cs @@ -74,7 +74,7 @@ public void CustomPrefixCannotBeSerilog() public void ThreadIdEnricherIsApplied() { // Make sure we have the expected key in the App.config - Assert.NotNull(ConfigurationManager.AppSettings["serilog:enrich:with:ThreadId"]); + Assert.NotNull(ConfigurationManager.AppSettings["serilog:enrich:WithThreadId"]); LogEvent evt = null; var log = new LoggerConfiguration() @@ -93,7 +93,7 @@ public void ThreadIdEnricherIsApplied() public void MachineNameEnricherIsApplied() { // Make sure we have the expected key in the App.config - Assert.NotNull(ConfigurationManager.AppSettings["serilog:enrich:with:MachineName"]); + Assert.NotNull(ConfigurationManager.AppSettings["serilog:enrich:WithMachineName"]); LogEvent evt = null; var log = new LoggerConfiguration() @@ -112,7 +112,7 @@ public void MachineNameEnricherIsApplied() public void EnrivonmentUserNameEnricherIsApplied() { // Make sure we have the expected key in the App.config - Assert.NotNull(ConfigurationManager.AppSettings["serilog:enrich:with:EnvironmentUserName"]); + Assert.NotNull(ConfigurationManager.AppSettings["serilog:enrich:WithEnvironmentUserName"]); LogEvent evt = null; var log = new LoggerConfiguration() @@ -133,7 +133,7 @@ public void EnrivonmentUserNameEnricherIsApplied() public void ProcessIdEnricherIsApplied() { // Make sure we have the expected key in the App.config - Assert.NotNull(ConfigurationManager.AppSettings["serilog:enrich:with:ProcessId"]); + Assert.NotNull(ConfigurationManager.AppSettings["serilog:enrich:WithProcessId"]); LogEvent evt = null; var log = new LoggerConfiguration() @@ -154,7 +154,7 @@ public void ProcessIdEnricherIsApplied() public void LogContextEnricherIsApplied() { // Make sure we have the expected key in the App.config - Assert.NotNull(ConfigurationManager.AppSettings["serilog:enrich:with:LogContext"]); + Assert.NotNull(ConfigurationManager.AppSettings["serilog:enrich:FromLogContext"]); LogEvent evt = null; var log = new LoggerConfiguration() diff --git a/test/Serilog.Tests/Settings/KeyValuePairSettingsTests.cs b/test/Serilog.Tests/Settings/KeyValuePairSettingsTests.cs index 755463184..26059b6b3 100644 --- a/test/Serilog.Tests/Settings/KeyValuePairSettingsTests.cs +++ b/test/Serilog.Tests/Settings/KeyValuePairSettingsTests.cs @@ -84,7 +84,7 @@ public void FindsConfigurationMethodsWithinAnAssembly() public void FindsEventEnrichersWithinAnAssembly() { var eventEnrichers = KeyValuePairSettings - .FindEventEnrichers(new[] { typeof(RollingFileSink) + .FindEventEnricherConfigurationMethods(new[] { typeof(RollingFileSink) #if DNXCORE50 .GetTypeInfo() #endif @@ -96,16 +96,16 @@ public void FindsEventEnrichersWithinAnAssembly() #if LOGCONTEXT - Assert.True(eventEnrichers.Contains("LogContextEnricher")); + Assert.True(eventEnrichers.Contains("FromLogContext")); #endif #if !DOTNET5_1 - Assert.True(eventEnrichers.Contains("EnvironmentUserNameEnricher")); - Assert.True(eventEnrichers.Contains("MachineNameEnricher")); + Assert.True(eventEnrichers.Contains("WithEnvironmentUserName")); + Assert.True(eventEnrichers.Contains("WithMachineName")); #endif #if PROCESS - Assert.True(eventEnrichers.Contains("ProcessIdEnricher")); + Assert.True(eventEnrichers.Contains("WithProcessId")); #endif - Assert.True(eventEnrichers.Contains("ThreadIdEnricher")); + Assert.True(eventEnrichers.Contains("WithThreadId")); } [Fact] diff --git a/test/Serilog.Tests/app.config b/test/Serilog.Tests/app.config index c8558df9f..9a9dafa57 100644 --- a/test/Serilog.Tests/app.config +++ b/test/Serilog.Tests/app.config @@ -4,10 +4,10 @@ - - - - - + + + + + \ No newline at end of file