C# Log4Net XML配置需要ILogger存储库
我正在构建一个C#控制台应用程序,我正在使用Autofac和log4net。我已经按照Autofac的指南添加了log4net模块,但是在配置日志时遇到了一些困难。问题是,当我尝试创建XMLConfiguration时,编译器要求我提供C# Log4Net XML配置需要ILogger存储库,c#,log4net,autofac,C#,Log4net,Autofac,我正在构建一个C#控制台应用程序,我正在使用Autofac和log4net。我已经按照Autofac的指南添加了log4net模块,但是在配置日志时遇到了一些困难。问题是,当我尝试创建XMLConfiguration时,编译器要求我提供ILogger存储库,我不知道这是什么。我已经浏览了log4net文档,在本文中没有提到存储库 这是我的模块: using System.Reflection; using log4net; using Autofac.Core; using Autofac.Co
ILogger存储库
,我不知道这是什么。我已经浏览了log4net文档,在本文中没有提到存储库
这是我的模块:
using System.Reflection;
using log4net;
using Autofac.Core;
using Autofac.Core.Registration;
using System.Linq;
using log4net.Config;
using System.IO;
namespace Logging
{
public class LoggingModule : Autofac.Module
{
public LoggingModule()
{
// The error happens here!
XmlConfigurator.Configure(new FileInfo("log4net.xml"));
}
private static void InjectLoggerProperties(object instance)
{
var instanceType = instance.GetType();
// Get all the injectable properties to set.
// If you wanted to ensure the properties were only UNSET properties,
// here's where you'd do it.
var properties = instanceType
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.PropertyType == typeof(ILog) && p.CanWrite && p.GetIndexParameters().Length == 0);
// Set the properties located.
foreach (var propToSet in properties)
{
propToSet.SetValue(instance, LogManager.GetLogger(instanceType), null);
}
}
private static void OnComponentPreparing(object sender, PreparingEventArgs e)
{
e.Parameters = e.Parameters.Union(
new[]
{
new ResolvedParameter(
(p, i) => p.ParameterType == typeof(ILog),
(p, i) => LogManager.GetLogger(p.Member.DeclaringType)
),
});
}
protected override void AttachToComponentRegistration(IComponentRegistryBuilder componentRegistryBuilder, IComponentRegistration registration)
{
// Handle constructor parameters.
registration.Preparing += OnComponentPreparing;
// Handle properties.
registration.Activated += (sender, e) => InjectLoggerProperties(e.Instance);
}
}
}
编译错误发生在这里:XmlConfigurator.Configure(新文件信息(“log4net.xml”)代码>上面写着:
Severity Code Description Project File Line Suppression State
Error CS1503 Argument 1: cannot convert from 'System.IO.FileInfo' to 'log4net.Repository.ILoggerRepository' MyApp C:\Dev\MyApp\Logging\LoggingModule.cs 15 Active
我已经设法用来自的答案解决了我的问题
下面是我如何添加ilogger存储库
并添加到配置中的:
ILoggerRepository repository = log4net.LogManager.GetRepository(Assembly.GetCallingAssembly());
log4net.Config.XmlConfigurator.Configure(repository, new FileInfo("log4net.xml"));
这是我的完整工作代码:
using System.Reflection;
using log4net;
using Autofac.Core;
using Autofac.Core.Registration;
using System.Linq;
using log4net.Config;
using System.IO;
using log4net.Repository;
namespace Logging
{
public class LoggingModule : Autofac.Module
{
public LoggingModule()
{
ILoggerRepository repository = LogManager.GetRepository(Assembly.GetCallingAssembly());
XmlConfigurator.Configure(repository, new FileInfo("log4net.xml"));
}
private static void InjectLoggerProperties(object instance)
{
var instanceType = instance.GetType();
// Get all the injectable properties to set.
// If you wanted to ensure the properties were only UNSET properties,
// here's where you'd do it.
var properties = instanceType
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.PropertyType == typeof(ILog) && p.CanWrite && p.GetIndexParameters().Length == 0);
// Set the properties located.
foreach (var propToSet in properties)
{
propToSet.SetValue(instance, LogManager.GetLogger(instanceType), null);
}
}
private static void OnComponentPreparing(object sender, PreparingEventArgs e)
{
e.Parameters = e.Parameters.Union(
new[]
{
new ResolvedParameter(
(p, i) => p.ParameterType == typeof(ILog),
(p, i) => LogManager.GetLogger(p.Member.DeclaringType)
),
});
}
protected override void AttachToComponentRegistration(IComponentRegistryBuilder componentRegistryBuilder, IComponentRegistration registration)
{
// Handle constructor parameters.
registration.Preparing += OnComponentPreparing;
// Handle properties.
registration.Activated += (sender, e) => InjectLoggerProperties(e.Instance);
}
}
}
能否检查XmlConfigurator
的命名空间?看起来类XmlConfigurator
不是从log4net使用的。尝试连接log4net.XmlConfigurator.Configure(新文件信息(“log4net.xml”)代码>嗨,我想我使用的是正确的类。我已将代码更改为:log4net.Config.XmlConfigurator.Configure(新文件信息(“log4net.xml”)代码>但错误仍然存在。