forked from nblumhardt/autofac-serilog-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nblumhardt#34: providing strategy in addition to a bool flag
- Loading branch information
1 parent
0aafd1b
commit d0f6a63
Showing
7 changed files
with
212 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Autofac.Core; | ||
using Autofac.Core.Activators.Reflection; | ||
using Serilog; | ||
|
||
namespace AutofacSerilogIntegration | ||
{ | ||
internal static class ActivatorExtensions | ||
{ | ||
internal static bool TryFindLoggerDependencies(this IInstanceActivator activator, bool inspectProperties, out bool injectParameter, out PropertyInfo[] targetProperties) | ||
{ | ||
injectParameter = false; | ||
targetProperties = null; | ||
switch (activator) | ||
{ | ||
case ReflectionActivator ra: | ||
// As of Autofac v4.7.0 "FindConstructors" will throw "NoConstructorsFoundException" instead of returning an empty array | ||
// See: https://github.com/autofac/Autofac/pull/895 & https://github.com/autofac/Autofac/issues/733 | ||
ConstructorInfo[] ctors; | ||
try | ||
{ | ||
ctors = ra.ConstructorFinder.FindConstructors(ra.LimitType); | ||
} | ||
catch (Exception ex) when (ex.GetType().Name == "NoConstructorsFoundException" | ||
) // Avoid needing to upgrade our Autofac reference to 4.7.0 | ||
{ | ||
ctors = new ConstructorInfo[0]; | ||
} | ||
|
||
injectParameter = ctors.SelectMany(ctor => ctor.GetParameters()) | ||
.Any(pi => pi.ParameterType == typeof(ILogger)); | ||
|
||
if (inspectProperties) | ||
{ | ||
var logProperties = ra.LimitType | ||
.GetRuntimeProperties() | ||
.Where(c => c.CanWrite && c.PropertyType == typeof(ILogger) && c.SetMethod.IsPublic && | ||
!c.SetMethod.IsStatic) | ||
.ToArray(); | ||
|
||
if (logProperties.Any()) | ||
{ | ||
targetProperties = logProperties; | ||
} | ||
} | ||
|
||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/AutofacSerilogIntegration/DefaultRegistrationProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Reflection; | ||
using Autofac.Core; | ||
|
||
namespace AutofacSerilogIntegration | ||
{ | ||
internal class DefaultRegistrationProcessor : IRegistrationProcessor | ||
{ | ||
readonly bool _autowireProperties; | ||
|
||
public DefaultRegistrationProcessor(bool autowireProperties) | ||
{ | ||
_autowireProperties = autowireProperties; | ||
} | ||
|
||
public Type Process(IComponentRegistration registration, out bool injectParameter, out PropertyInfo[] targetProperties) | ||
{ | ||
if (!registration.Activator.TryFindLoggerDependencies(_autowireProperties, out injectParameter, out targetProperties)) | ||
{ | ||
//this is the legacy behavior: we skipped injection only if the answer was a definitive "no", not an "I don't know" | ||
injectParameter = true; | ||
} | ||
return registration.Activator.LimitType; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Reflection; | ||
using Autofac.Core; | ||
|
||
namespace AutofacSerilogIntegration | ||
{ | ||
public interface IRegistrationProcessor | ||
{ | ||
Type Process(IComponentRegistration registration, out bool injectParameter, out PropertyInfo[] targetProperties); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/AutofacSerilogIntegration/OnlyKnownCustomersRegistrationProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Reflection; | ||
using Autofac.Core; | ||
|
||
namespace AutofacSerilogIntegration | ||
{ | ||
internal class OnlyKnownCustomersRegistrationProcessor : IRegistrationProcessor | ||
{ | ||
readonly bool _autowireProperties; | ||
|
||
public OnlyKnownCustomersRegistrationProcessor(bool autowireProperties) | ||
{ | ||
_autowireProperties = autowireProperties; | ||
} | ||
|
||
public Type Process(IComponentRegistration registration, out bool injectParameter, out PropertyInfo[] targetProperties) | ||
{ | ||
if (registration.Activator.TryFindLoggerDependencies(_autowireProperties, out injectParameter, out targetProperties)) | ||
return registration.Activator.LimitType; | ||
else | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters