我想知道以下是否可行,如果可以,该怎么做。我将尝试用下面的代码来解释它。
public class RandomClass
{
...
//class is filled with stuff of none importance for this demonstration
...
[LogScope("Start", "End")] //The important LogScope attribute!
public virtual void MyMethod()
{
...
//Doing something here but not the point now...
...
}
...
}
public LogScopeAttribute : InterceptAttribute
{
private readonly string _header;
private readonly string _footer;
public LogScopeAttribute(string header, string footer)
{
_header = header;
_footer = footer;
// This is just one of many constructors....
}
public override IInterceptor CreateInterceptor(IProxyRequest request)
{
return request.Context.Kernel.Get<LogScopeInterceptor>(
new ConstructorArgument("header", _header),
new ConstructorArgument("footer", _footer));
// In the real version of this method there is more logic here for creating
// the right version of the interceptor, in regards to what constrcutor the
// LogScopeAttribute was called with.
}
}
public class LogScopeInterceptor : SimpleInterceptor
{
// No need to explain stuff here, (not of importance to the question)
// but what the interceptor does for me is write to log
// a header and footer and indent everything inside the logscope.
}
我想要的是将 aILogScopeInterceptorFactory
注入到LogScopeAttribute
with Ninject 中,这样我就不必在CreateInterceptor
方法内调用内核,并插入ConstructorArgument
我想这样做的对象......
public LogScopeAttribute : InterceptAttribute
{
private readonly string _header;
private readonly string _footer;
private readonly ILogScopeInterceptorFactory _logScopeInterceptorFactory;
public LogScopeAttribute(ILogScopeInterceptorFactory logScopeInterceptorFactory ,string header, string footer)
{
_header = header;
_footer = footer;
_logScopeInterceptorFactory = logScopeInterceptorFactory;
}
public override IInterceptor CreateInterceptor(IProxyRequest request)
{
return _logScopeInterceptorFactory.Create(_header, _footer);
}
}
这是ILogScopeInterceptorFactory
我绑定的接口,Ninject.extensions.Factory
如下所示:
Bind<ILogScopeInterceptorFactory>().ToFactory();
public interface ILogScopeInterceptorFactory
{
LogScopeInterceptor Create(char separatorChar, string header, string footer);
LogScopeInterceptor Create(char separatorChar, string header);
LogScopeInterceptor Create(string header, string footer);
LogScopeInterceptor Create(string header);
LogScopeInterceptor Create(char separatorChar);
LogScopeInterceptor Create();
}
现在我想做的仍然是LogScopeAttribute
这样
使用,而[LogScope("Start", "End")]
不必手动插入工厂,只需注入它,我该怎么做?
编辑2:
我正在使用 log4net 进行登录,我将在这里写下LogScopeAttribute
它的作用以及输出的样子。
[LogScope("The Start", "The End")]
public virtual void MyMethod()
{
logger.Info("Hello World!");
}
OUTPUT:
logger: The Start
logger: Hello World!
logger: The End
为了让ninject拦截所有方法,它们需要是public
and virtual
。我发现这对于记录非常方便,可以轻松缩进方法中记录的所有内容如果我想要页眉和页脚,或者打印该方法的执行时间,它都在那里。这也可以嵌套...
@FELIX
问题不在于我无法获取内核......如果我想通过调用内核来创建工厂,我可以这样做。
public override IInterceptor CreateInterceptor(IProxyRequest request)
{
var factory = request.Context.Kernel.Get<ILogScopeInterceptorFactory>();
return factory.Create(_header, _footer);
}
如果我手动插入工厂,我将不得不为每个属性执行此操作
[LogScope(_logScopeInterceptorFactory, "header", "footer")]
那会很丑陋