Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/jcrefactor'
Browse files Browse the repository at this point in the history
  • Loading branch information
johncrn committed Jan 17, 2016
2 parents e35d82b + 54fde6c commit 98cf960
Showing 1 changed file with 44 additions and 50 deletions.
94 changes: 44 additions & 50 deletions src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,83 +81,70 @@ 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
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();

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);

if (eventEnricherDirectives.Any())
if (sinkDirectives.Any())
{
var eventEnricherTypes = FindEventEnricherConfigurationMethods(configurationAssemblies);

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 });
}
}
ApplyDirectives(sinkDirectives, FindSinkConfigurationMethods(configurationAssemblies), loggerConfiguration.WriteTo);
}

if (sinkDirectives.Any())
if (eventEnricherDirectives.Any())
{
var sinkConfigurationMethods = FindSinkConfigurationMethods(configurationAssemblies);
ApplyDirectives(eventEnricherDirectives, FindEventEnricherConfigurationMethods(configurationAssemblies), loggerConfiguration.Enrich);
}
}
}

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<IGrouping<string, MethodArgumentValue>> directives, IList<MethodInfo> configurationMethods, object loggerConfigMethod)
{
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
(p.Attributes & ParameterAttributes.HasDefault) != ParameterAttributes.None
#else
p.HasDefaultValue
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());
}
}
}
Expand Down Expand Up @@ -230,5 +217,12 @@ internal static IList<MethodInfo> FindConfigurationMethods(IEnumerable<Assembly>
.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; }
}
}
}

0 comments on commit 98cf960

Please sign in to comment.