我无法在控制台应用程序中解析ILogger实例...
我正在使用Unity IoC和Serilog。
注册是这样的:
container = new UnityContainer();
container.RegisterFactory<ILogger>(factory =>
{
ILogger log = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
return log;
}, new ContainerControlledLifetimeManager());
然后当我尝试此操作时:
var logger = container.Resolve<ILogger>();
我得到一个例外:
Unity.ResolutionFailedException HResult = 0x80131500消息=失败 选择Serilog.Core.Logger的构造函数 _____________________________________________________在以下时间发生异常:
•解析类型:“ ILogger”映射到“ Logger”
Source = Unity.Container StackTrace:at Unity.UnityContainer.ExecuteValidatingPlan(BuilderContext&context)
在Unity.UnityContainer.Unity.IUnityContainer.Resolve(Type type, 字符串名称,ResolverOverride []覆盖) Unity.UnityContainerExtensions.Resolve [T](IUnityContainer容器, ResolverOverride []覆盖) 内部异常1:InvalidOperationException:无法选择 Serilog.Core.Logger的构造函数内部异常2:InvalidRegistrationException:类型的异常 抛出了“ Unity.Exceptions.InvalidRegistrationException”。
我什至尝试像这样注册它:
container = new UnityContainer();
ILogger log = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
container.RegisterInstance<ILogger>(log);
但会引发相同的结果异常。
请帮助
答案 0 :(得分:0)
发现了问题... 问题在于这行代码,覆盖了我的配置:
container.RegisterTypes(AllClasses.FromAssembliesInBasePath(), WithMappings.FromMatchingInterface, WithName.Default);