使用 Castle Logging Facility 结合 Castle Windsor 版本 4.0.0 和 NLog
Combining Castle Windsor version 4.0.0 and NLog using Castle Logging Facility
我正在尝试使用 Castle Windsor 设置新的控制台应用程序。我想将 NLog 合并到应用程序中。但是,每当我尝试 运行 应用程序时,我都会在尝试向 Windsor 容器添加 NLog 工具时收到以下错误。
我得到的错误是:
Castle.MicroKernel.SubSystems.Conversion.ConverterException: Could not convert string 'Castle.Services.Logging.NLogIntegration.NLogFactory,Castle.Services.Logging.NLogIntegration,Version=4.0.0.0, Culture=neutral,PublicKeyToken=407dd0808d44fbdc' to a type. ---> System.IO.FileLoadException: Could not load file or assembly 'Castle.Services.Logging.NLogIntegration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
at System.Type.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase)
at Castle.MicroKernel.SubSystems.Conversion.TypeNameConverter.GetType(String name)
at Castle.MicroKernel.SubSystems.Conversion.TypeNameConverter.PerformConversion(String value, Type targetType)
--- End of inner exception stack trace ---
at Castle.MicroKernel.SubSystems.Conversion.TypeNameConverter.PerformConversion(String value, Type targetType)
at Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion(String value, Type targetType)
at Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion[TTarget](String value)
at Castle.Facilities.Logging.LoggingFacility.GetLoggingFactoryType(LoggerImplementation loggerApi)
at Castle.Facilities.Logging.LoggingFacility.CreateProperLoggerFactory(LoggerImplementation loggerApi)
at Castle.Facilities.Logging.LoggingFacility.Init()
at Castle.MicroKernel.Facilities.AbstractFacility.Castle.MicroKernel.IFacility.Init(IKernel kernel, IConfiguration facilityConfig)
at Castle.MicroKernel.DefaultKernel.AddFacility(String key, IFacility facility)
at Castle.MicroKernel.DefaultKernel.AddFacility(IFacility facility)
at Castle.MicroKernel.DefaultKernel.AddFacility[T](Action`1 onCreate)
at Castle.Windsor.WindsorContainer.AddFacility[T](Action`1 onCreate)
at CastleWindsorNLogIntegration.MyWindsorInstaller.Install(IWindsorContainer container, IConfigurationStore store) in c:\users\mchaleg\documents\visual studio 2015\Projects\CastleWindsorNLogIntegration\CastleWindsorNLogIntegration\Program.cs:line 41
at Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers, DefaultComponentInstaller scope)
at Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers)
at CastleWindsorNLogIntegration.Program.Main(String[] args) in c:\users\mchaleg\documents\visual studio 2015\Projects\CastleWindsorNLogIntegration\CastleWindsorNLogIntegration\Program.cs:line 17
当我尝试将新的日志记录工具添加到 Windsor 容器时发生。触发异常的代码显示在下面的代码片段中。错误发生在classMyWindsorInstaller
.
namespace CastleWindsorNLogIntegration
{
class Program
{
static void Main(string[] args)
{
try
{
var container = new WindsorContainer().Install(new MyWindsorInstaller());
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
internal class MyWindsorInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
// The error occurs when running this line
container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.NLog)
.WithConfig("NLog.config"));
container.Register(
Component.For<IObjectGraphRoot>().ImplementedBy<ObjectGraphRoot>());
}
}
internal interface IObjectGraphRoot
{
void Log();
}
internal class ObjectGraphRoot : IObjectGraphRoot
{
private ILogger _logger = NullLogger.Instance;
public ILogger Logger
{
get { return _logger; }
set { _logger = value; }
}
public void Log()
{
_logger.Info("Log something");
}
}
}
据我所知,我已经使用 NuGet 安装了所有正确的包。您可以在下面看到已安装软件包的列表。我检查了每个包的依赖关系,一切似乎都是正确的。谁能解释我做错了什么?
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="4.1.1" targetFramework="net452" />
<package id="Castle.Core-NLog" version="4.1.1" targetFramework="net452" />
<package id="Castle.LoggingFacility" version="4.0.0" targetFramework="net452" />
<package id="Castle.Windsor" version="4.0.0" targetFramework="net452" />
<package id="NLog" version="4.4.12" targetFramework="net452" />
<package id="NLog.Config" version="4.4.12" targetFramework="net452" />
<package id="NLog.Schema" version="4.4.12" targetFramework="net452" />
</packages>
下面描述的解决方案取自 link https://www.bountysource.com/issues/47957245-fix-dependency-specification-of-logging-packages-to-the-exact-same-explicit-version-of-castle-core-package
中包含的信息
最初(通过 NuGet)安装 Castle Windsor 和 NLog 的包时,我得到了以下包组合:
<package id="Castle.Core" version="4.0.0" targetFramework="net452"/>
<package id="Castle.Core-NLog" version="3.3.0" targetFramework="net452" />
<package id="Castle.LoggingFacility" version="4.0.0" targetFramework="net452"/>
<package id="Castle.Windsor" version="4.0.0" targetFramework="net452"/>
<package id="Castle.Windsor-NLog" version="3.4.0" targetFramework="net452"/>
出现此问题是因为不同版本的单独 Castle-Core 软件包不应一起使用。
解决方案是重新加载包(再次通过 NuGet)并为每个包指定相同的版本,如下表所示。
<package id="Castle.Core" version="4.0.0" targetFramework="net452"/>
<package id="Castle.Core-NLog" version="4.0.0" targetFramework="net452" />
<package id="Castle.LoggingFacility" version="4.0.0" targetFramework="net452"/>
<package id="Castle.Windsor" version="4.0.0" targetFramework="net452"/>
请注意,不再需要 Castle.Windsor-NLog
,因此不应安装它。
我正在尝试使用 Castle Windsor 设置新的控制台应用程序。我想将 NLog 合并到应用程序中。但是,每当我尝试 运行 应用程序时,我都会在尝试向 Windsor 容器添加 NLog 工具时收到以下错误。
我得到的错误是:
Castle.MicroKernel.SubSystems.Conversion.ConverterException: Could not convert string 'Castle.Services.Logging.NLogIntegration.NLogFactory,Castle.Services.Logging.NLogIntegration,Version=4.0.0.0, Culture=neutral,PublicKeyToken=407dd0808d44fbdc' to a type. ---> System.IO.FileLoadException: Could not load file or assembly 'Castle.Services.Logging.NLogIntegration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
at System.Type.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase)
at Castle.MicroKernel.SubSystems.Conversion.TypeNameConverter.GetType(String name)
at Castle.MicroKernel.SubSystems.Conversion.TypeNameConverter.PerformConversion(String value, Type targetType)
--- End of inner exception stack trace ---
at Castle.MicroKernel.SubSystems.Conversion.TypeNameConverter.PerformConversion(String value, Type targetType)
at Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion(String value, Type targetType)
at Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion[TTarget](String value)
at Castle.Facilities.Logging.LoggingFacility.GetLoggingFactoryType(LoggerImplementation loggerApi)
at Castle.Facilities.Logging.LoggingFacility.CreateProperLoggerFactory(LoggerImplementation loggerApi)
at Castle.Facilities.Logging.LoggingFacility.Init()
at Castle.MicroKernel.Facilities.AbstractFacility.Castle.MicroKernel.IFacility.Init(IKernel kernel, IConfiguration facilityConfig)
at Castle.MicroKernel.DefaultKernel.AddFacility(String key, IFacility facility)
at Castle.MicroKernel.DefaultKernel.AddFacility(IFacility facility)
at Castle.MicroKernel.DefaultKernel.AddFacility[T](Action`1 onCreate)
at Castle.Windsor.WindsorContainer.AddFacility[T](Action`1 onCreate)
at CastleWindsorNLogIntegration.MyWindsorInstaller.Install(IWindsorContainer container, IConfigurationStore store) in c:\users\mchaleg\documents\visual studio 2015\Projects\CastleWindsorNLogIntegration\CastleWindsorNLogIntegration\Program.cs:line 41
at Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers, DefaultComponentInstaller scope)
at Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers)
at CastleWindsorNLogIntegration.Program.Main(String[] args) in c:\users\mchaleg\documents\visual studio 2015\Projects\CastleWindsorNLogIntegration\CastleWindsorNLogIntegration\Program.cs:line 17
当我尝试将新的日志记录工具添加到 Windsor 容器时发生。触发异常的代码显示在下面的代码片段中。错误发生在classMyWindsorInstaller
.
namespace CastleWindsorNLogIntegration
{
class Program
{
static void Main(string[] args)
{
try
{
var container = new WindsorContainer().Install(new MyWindsorInstaller());
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
internal class MyWindsorInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
// The error occurs when running this line
container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.NLog)
.WithConfig("NLog.config"));
container.Register(
Component.For<IObjectGraphRoot>().ImplementedBy<ObjectGraphRoot>());
}
}
internal interface IObjectGraphRoot
{
void Log();
}
internal class ObjectGraphRoot : IObjectGraphRoot
{
private ILogger _logger = NullLogger.Instance;
public ILogger Logger
{
get { return _logger; }
set { _logger = value; }
}
public void Log()
{
_logger.Info("Log something");
}
}
}
据我所知,我已经使用 NuGet 安装了所有正确的包。您可以在下面看到已安装软件包的列表。我检查了每个包的依赖关系,一切似乎都是正确的。谁能解释我做错了什么?
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="4.1.1" targetFramework="net452" />
<package id="Castle.Core-NLog" version="4.1.1" targetFramework="net452" />
<package id="Castle.LoggingFacility" version="4.0.0" targetFramework="net452" />
<package id="Castle.Windsor" version="4.0.0" targetFramework="net452" />
<package id="NLog" version="4.4.12" targetFramework="net452" />
<package id="NLog.Config" version="4.4.12" targetFramework="net452" />
<package id="NLog.Schema" version="4.4.12" targetFramework="net452" />
</packages>
下面描述的解决方案取自 link https://www.bountysource.com/issues/47957245-fix-dependency-specification-of-logging-packages-to-the-exact-same-explicit-version-of-castle-core-package
中包含的信息最初(通过 NuGet)安装 Castle Windsor 和 NLog 的包时,我得到了以下包组合:
<package id="Castle.Core" version="4.0.0" targetFramework="net452"/>
<package id="Castle.Core-NLog" version="3.3.0" targetFramework="net452" />
<package id="Castle.LoggingFacility" version="4.0.0" targetFramework="net452"/>
<package id="Castle.Windsor" version="4.0.0" targetFramework="net452"/>
<package id="Castle.Windsor-NLog" version="3.4.0" targetFramework="net452"/>
出现此问题是因为不同版本的单独 Castle-Core 软件包不应一起使用。
解决方案是重新加载包(再次通过 NuGet)并为每个包指定相同的版本,如下表所示。
<package id="Castle.Core" version="4.0.0" targetFramework="net452"/>
<package id="Castle.Core-NLog" version="4.0.0" targetFramework="net452" />
<package id="Castle.LoggingFacility" version="4.0.0" targetFramework="net452"/>
<package id="Castle.Windsor" version="4.0.0" targetFramework="net452"/>
请注意,不再需要 Castle.Windsor-NLog
,因此不应安装它。