获取正在创建的控制器类型
我正在 MVC 项目中设置日志记录。我的 NLogLogger 类采用 [Type] 类型的参数。无论如何,我可以让 autofac 实例化 NLogLogger 对象,传入当前正在实例化的控制器的类型吗?在下面的代码中,TTT 需要是控制器的类型。
builder.Register(c => new NLogLogger(TTT)).As<ILogger>();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
非常感谢您的帮助。
达米安.
I'm setting up logging in an MVC project. My class NLogLogger takes a parameter of type [Type]. Is there anyway that I can get autofac to instantiate the NLogLogger object passing in the type of the Controller that is currently being instantiated? In the code below, TTT needs to be the Type of the controller.
builder.Register(c => new NLogLogger(TTT)).As<ILogger>();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
Thanks very much for your help.
Damien.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够按照Log4NetIntegration 的工作方式来解决此问题。您设置了一个
Module
来监听每个组件的准备工作(当容器即将创建某个实例时),并为组件类型提供 NLog 记录器。You should be able to solve this in the same way as the Log4NetIntegration works. You set up a
Module
that listens in on every component preparation (when some instance is about to be created by the container) and supply the NLog logger for the component type.