使用 Ninject 在 MVC.NET 上使用 Log4Net
我正在尝试使用 Ninject 在 MVC.net 项目中设置 Log4Net。 我已经能够看到 Ninject 调试输出,但仍然没有看到任何日志输出。
这一切都在我的本地主机上运行,因此不应该有任何安全问题。
我的设置如下:
在 AssemblyInfo.cs 中,我添加了这个以将 log4net 指向 Web.config 进行配置
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
在 Web.config 中,我添加了以下部分
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
...
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
...
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<encoding value="utf-8" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
在我的基本控制器中,其中的所有其他控制器系统继承我有以下内容:
[Inject]
public ILogger logger { get; set; }
然后,我在登录页面上显式抛出新异常(“testException”),以强制出现我希望在日志文件中看到的错误。
从调试跟踪中,我在启动时看到以下内容。
log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\portal\63a118bb\781d84b7\assembly\dl3\40d137a6\9fb44da2_c951cc01\log4net.dll]. (.NET Runtime [4.0.30319.235] on Microsoft Windows NT 6.1.7601 Service Pack 1)
log4net: DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository for assembly [Ninject.Extensions.Logging.Log4Net, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7]
log4net: DefaultRepositorySelector: Assembly [Ninject.Extensions.Logging.Log4Net, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7] Loaded From [C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\portal\63a118bb\781d84b7\assembly\dl3\edd36351\91af46a2_c951cc01\Ninject.Extensions.Logging.Log4Net.dll]
log4net: DefaultRepositorySelector: Assembly [Ninject.Extensions.Logging.Log4Net, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7] does not have a RepositoryAttribute specified.
log4net: DefaultRepositorySelector: Assembly [Ninject.Extensions.Logging.Log4Net, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
我感觉这可能是一个配置问题,但任何帮助将不胜感激。
I'm trying to set up Log4Net in a MVC.net project with Ninject.
I've reached the point where I'm able to see the Ninject debug output but I am still not seeing any logging output.
This is all running on my localhost so there shouldn't be any issues with security.
The setup I have is as follows:
In the AssemblyInfo.cs I have added this to point log4net to Web.config for configuration
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
In the Web.config I've added the following sections
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
...
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
...
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<encoding value="utf-8" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
In my base controller, from which all the other controllers in the system inherit I have the following:
[Inject]
public ILogger logger { get; set; }
I then put an explicit throw new Exception("testException") on the login page to force an error which I would have expected to see showing up in the log file.
From the debug trace I see the following on startup.
log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\portal\63a118bb\781d84b7\assembly\dl3\40d137a6\9fb44da2_c951cc01\log4net.dll]. (.NET Runtime [4.0.30319.235] on Microsoft Windows NT 6.1.7601 Service Pack 1)
log4net: DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository for assembly [Ninject.Extensions.Logging.Log4Net, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7]
log4net: DefaultRepositorySelector: Assembly [Ninject.Extensions.Logging.Log4Net, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7] Loaded From [C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\portal\63a118bb\781d84b7\assembly\dl3\edd36351\91af46a2_c951cc01\Ninject.Extensions.Logging.Log4Net.dll]
log4net: DefaultRepositorySelector: Assembly [Ninject.Extensions.Logging.Log4Net, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7] does not have a RepositoryAttribute specified.
log4net: DefaultRepositorySelector: Assembly [Ninject.Extensions.Logging.Log4Net, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
I have the feeling that this is probably a configuration issue but any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注入
ILog
或ILogger
实例不会隐式注册任何类型的异常处理程序。它只是让您访问记录器。您仍然需要告诉它记录什么以及何时记录。如果您想测试您的配置,请从控制器的操作之一进行日志记录调用,或使用
OnAction
事件调用程序之一。这可能应该做你想要的:这将记录每个控制器操作 - 包括异常。如果您只关心异常,那么可以在记录之前检查 context.Exception 和 context.ExceptionHandled 属性,或者最好编写一个自定义 ActionFilter 来执行此操作。
Injecting an
ILog
orILogger
instance does not implicitly register any kind of exception handler. It just gives you access to a logger. You still have to tell it what to log and when to log it.If you want to test your configuration, make a logging call from one of your controller's actions, or use one of the
OnAction
event invokers. This should probably do what you want:That will log every controller action - including exceptions. If you only care about exceptions then either check the
context.Exception
andcontext.ExceptionHandled
properties before logging, or better yet write a custom ActionFilter to do this.