Skip to content

Commit

Permalink
filtering support
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Komisarchik committed Jan 17, 2017
1 parent 5266181 commit 4424523
Showing 1 changed file with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Serilog.Core;
using Serilog.Debugging;
using Serilog.Events;
using System.Linq.Expressions;

namespace Serilog.Settings.Configuration
{
Expand All @@ -33,9 +34,20 @@ public void Configure(LoggerConfiguration loggerConfiguration)

ApplyMinimumLevel(loggerConfiguration);
ApplyEnrichment(loggerConfiguration, configurationAssemblies);
ApplyFilters(loggerConfiguration, configurationAssemblies);
ApplySinks(loggerConfiguration, configurationAssemblies);
}

void ApplyFilters(LoggerConfiguration loggerConfiguration, Assembly[] configurationAssemblies)
{
var filterDirective = _configuration.GetSection("Filter");
if (filterDirective != null)
{
var methodCalls = GetMethodCalls(filterDirective);
CallConfigurationMethods(methodCalls, FindFilterConfigurationMethods(configurationAssemblies), loggerConfiguration.Filter);
}
}

void ApplySinks(LoggerConfiguration loggerConfiguration, Assembly[] configurationAssemblies)
{
var writeToDirective = _configuration.GetSection("WriteTo");
Expand Down Expand Up @@ -273,21 +285,20 @@ internal static IList<MethodInfo> FindSinkConfigurationMethods(IEnumerable<Assem
return FindConfigurationMethods(configurationAssemblies, typeof(LoggerSinkConfiguration));
}

// Unlike the other configuration methods, FromLogContext is an instance method rather than an extension.
internal static LoggerConfiguration FromLogContext(LoggerEnrichmentConfiguration loggerEnrichmentConfiguration)
internal static IList<MethodInfo> FindFilterConfigurationMethods(IEnumerable<Assembly> configurationAssemblies)
{
return loggerEnrichmentConfiguration.FromLogContext();
}
var found = FindConfigurationMethods(configurationAssemblies, typeof(LoggerFilterConfiguration));
if (configurationAssemblies.Contains(typeof(LoggerFilterConfiguration).GetTypeInfo().Assembly))
found.Add(GetSurrogateConfigurationMethod<LoggerFilterConfiguration, ILogEventFilter>((c, f) => With(c, f)));

static readonly MethodInfo SurrogateFromLogContextConfigurationMethod = typeof(ConfigurationReader)
.GetTypeInfo()
.DeclaredMethods.Single(m => m.Name == "FromLogContext");
return found;
}

internal static IList<MethodInfo> FindEventEnricherConfigurationMethods(IEnumerable<Assembly> configurationAssemblies)
{
var found = FindConfigurationMethods(configurationAssemblies, typeof(LoggerEnrichmentConfiguration));
if (configurationAssemblies.Contains(typeof(LoggerEnrichmentConfiguration).GetTypeInfo().Assembly))
found.Add(SurrogateFromLogContextConfigurationMethod);
found.Add(GetSurrogateConfigurationMethod<LoggerEnrichmentConfiguration, object>((c, _) => FromLogContext(c)));

return found;
}
Expand All @@ -303,5 +314,22 @@ internal static IList<MethodInfo> FindConfigurationMethods(IEnumerable<Assembly>
.Where(m => m.GetParameters()[0].ParameterType == configType)
.ToList();
}

// don't support (yet?) arrays in the parameter list (ILogEventEnricher[])
internal static LoggerConfiguration With(LoggerFilterConfiguration loggerFilterConfiguration, ILogEventFilter filter)
{
return loggerFilterConfiguration.With(filter);
}

// Unlike the other configuration methods, FromLogContext is an instance method rather than an extension.
internal static LoggerConfiguration FromLogContext(LoggerEnrichmentConfiguration loggerEnrichmentConfiguration)
{
return loggerEnrichmentConfiguration.FromLogContext();
}

internal static MethodInfo GetSurrogateConfigurationMethod<TConfiguration, TArg>(Expression<Action<TConfiguration, TArg>> method)
{
return (method.Body as MethodCallExpression)?.Method;
}
}
}

0 comments on commit 4424523

Please sign in to comment.