我正在尝试配置Unity以将ILog注入到我的类中,其中LogManager.CreateLogger()中的类型/名称被设置为注入ILog的类。
这是similar question,但对于结构图
答案 0 :(得分:1)
我知道这个答案很晚,但我最近遇到了这个问题。
现在有一个NuGet包UnityLog4NetExtension,它注册log4net.ILog
接口和一些Unity构建策略,允许你只注入ILog
接口并让Unity使用它创建它正在注入的类。
该代码也可通过GitHub获得。
例如:
在需要注入ILog实例之前,将扩展添加到unity容器中。我已经在引导程序中完成了这项工作。
//add the extension to the unity container
container.AddNewExtension<Log4NetExtension>();
然后,只需注入ILog
接口即可正常工作
public class MyClass
{
private readonly ILog _logger;
public MyClass(ILog loggerForMyClass)
{
_logger = loggerForMyClass;
}
}
此代码就像我写的_logger = LogManager.GetLogger(typeof(MyClass))
答案 1 :(得分:0)
来自类似问题的this answer是否有帮助?它显示了如何使用Unity 2中的InjectionFactory注册ILog。
答案 2 :(得分:0)
这可能不是最好的方式,但这是我能找到的唯一方法。所以现在这是最好的方式: - )
Container.RegisterType<ILog>(new InjectionFactory(factory => new LoggerForInjection()));
-
public class LoggerForInjection : ILog
{
private ILog _log;
public bool IsDebugEnabled
{
get { return Log.IsDebugEnabled; }
}
public bool IsErrorEnabled
{
get { return Log.IsErrorEnabled; }
}
public bool IsFatalEnabled
{
get { return Log.IsFatalEnabled; }
}
public bool IsInfoEnabled
{
get { return Log.IsInfoEnabled; }
}
public bool IsWarnEnabled
{
get { return Log.IsWarnEnabled; }
}
public ILogger Logger
{
get { return _log.Logger; }
}
private ILog Log
{
get
{
if (_log == null)
{
// My tests shows, that is takes about 1 milli second to get the calling type
var stackTrace = new StackTrace();
string callingType = stackTrace.GetFrame(2).GetMethod().DeclaringType.FullName;
_log = LogManager.GetLogger(callingType);
}
return _log;
}
}
public void Debug(object message)
{
Log.Debug(message);
}
public void Debug(object message, Exception exception)
{
Log.Debug(message, exception);
}
public void DebugFormat(string format, params object[] args)
{
Log.DebugFormat(format, args);
}
public void DebugFormat(string format, object arg0)
{
Log.DebugFormat(format, arg0);
}
public void DebugFormat(string format, object arg0, object arg1)
{
Log.DebugFormat(format, arg0, arg1);
}
public void DebugFormat(string format, object arg0, object arg1, object arg2)
{
Log.DebugFormat(format, arg0, arg1, arg2);
}
public void DebugFormat(IFormatProvider provider, string format, params object[] args)
{
Log.DebugFormat(provider, format, args);
}
public void Error(object message)
{
Log.Error(message);
}
public void Error(object message, Exception exception)
{
Log.Error(message, exception);
}
public void ErrorFormat(string format, params object[] args)
{
Log.ErrorFormat(format, args);
}
public void ErrorFormat(string format, object arg0)
{
Log.ErrorFormat(format, arg0);
}
public void ErrorFormat(string format, object arg0, object arg1)
{
Log.ErrorFormat(format, arg0, arg1);
}
public void ErrorFormat(string format, object arg0, object arg1, object arg2)
{
Log.ErrorFormat(format, arg0, arg1, arg2);
}
public void ErrorFormat(IFormatProvider provider, string format, params object[] args)
{
Log.ErrorFormat(provider, format, args);
}
public void Fatal(object message)
{
Log.Fatal(message);
}
public void Fatal(object message, Exception exception)
{
Log.Fatal(message, exception);
}
public void FatalFormat(string format, params object[] args)
{
Log.FatalFormat(format, args);
}
public void FatalFormat(string format, object arg0)
{
Log.FatalFormat(format, arg0);
}
public void FatalFormat(string format, object arg0, object arg1)
{
Log.FatalFormat(format, arg0, arg1);
}
public void FatalFormat(string format, object arg0, object arg1, object arg2)
{
Log.FatalFormat(format, arg0, arg1, arg2);
}
public void FatalFormat(IFormatProvider provider, string format, params object[] args)
{
Log.FatalFormat(provider, format, args);
}
public void Info(object message)
{
Log.Info(message);
}
public void Info(object message, Exception exception)
{
Log.Info(message, exception);
}
public void InfoFormat(string format, params object[] args)
{
Log.InfoFormat(format, args);
}
public void InfoFormat(string format, object arg0)
{
Log.InfoFormat(format, arg0);
}
public void InfoFormat(string format, object arg0, object arg1)
{
Log.InfoFormat(format, arg0, arg1);
}
public void InfoFormat(string format, object arg0, object arg1, object arg2)
{
Log.InfoFormat(format, arg0, arg1, arg2);
}
public void InfoFormat(IFormatProvider provider, string format, params object[] args)
{
Log.InfoFormat(provider, format, args);
}
public void Warn(object message)
{
Log.Warn(message);
}
public void Warn(object message, Exception exception)
{
Log.Warn(message, exception);
}
public void WarnFormat(string format, params object[] args)
{
Log.WarnFormat(format, args);
}
public void WarnFormat(string format, object arg0)
{
Log.WarnFormat(format, arg0);
}
public void WarnFormat(string format, object arg0, object arg1)
{
Log.WarnFormat(format, arg0, arg1);
}
public void WarnFormat(string format, object arg0, object arg1, object arg2)
{
Log.WarnFormat(format, arg0, arg1, arg2);
}
public void WarnFormat(IFormatProvider provider, string format, params object[] args)
{
Log.WarnFormat(provider, format, args);
}
}