使用 NLog 记录未处理的异常? ELMAH 和 NLog 应该一起使用吗?
我在 ASP.NET MVC 项目中使用 ELMAH,我真的很喜欢它的简单性。但我需要能够以 log.Info("message") 方式在代码中记录某些事件。由于 ELMAH 不提供这种能力,我开始研究 NLog。
我想到了几个问题:
- 同时使用 ELMAH 和 NLog 是否太过分了?使用 ELMAH 处理未处理的异常,使用 NLog 处理其他所有事情?
- 是否可以配置 NLog 以在 nlog.config 中记录未处理的异常,或者是否需要自定义代码?
- 如果需要自定义代码,添加它的最佳位置在哪里? MVC框架中的OnException方法? global.asax 文件?
非常感谢任何反馈。到目前为止我还没有找到关于这个问题的好帖子?
I am using ELMAH in my ASP.NET MVC projects and I really like its simplicity. But I needed the ability to log certain events in my code in a log.Info("message") manner. Since ELMAH does not provide that ability I started looking at NLog.
A few questions came to mind:
- Is using both ELMAH and NLog overkill? Using ELMAH for unhandled exceptions and NLog for everything else?
- Is it possible to configure NLog to log unhandled exceptions in the nlog.config or is custom code needed?
- If custom code is needed where would be the best place to add it? The OnException method in the MVC framework? The global.asax file?
Anny feedback is greatly appreciated. So far I haven't found any good posts on this matter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回答第二个问题,我刚刚阅读了这篇文章,其中解释了如何记录未处理的日志NLog 的异常。基本上是在 Global.asax.cs 文件中添加如下内容:
Answering question number 2, I just read this article where it explains how to log un-handled exceptions with NLog. Basically is adding in the Global.asax.cs file something like:
事实上,您可以触发 ELMAH 的警报。就像这样:
不要忘记添加对 elmah.dll 的引用并将
using Elmah;
添加到文件中。请参阅此处 更多示例。
You can in fact trigger an alert from ELMAH. Like so:
Don't forget to add a reference to elmah.dll and add
using Elmah;
to the file.See here for a bit more examples.