使用 NLog 记录未处理的异常? ELMAH 和 NLog 应该一起使用吗?

发布于 2024-09-30 05:03:35 字数 384 浏览 0 评论 0原文

我在 ASP.NET MVC 项目中使用 ELMAH,我真的很喜欢它的简单性。但我需要能够以 log.Info("message") 方式在代码中记录某些事件。由于 ELMAH 不提供这种能力,我开始研究 NLog。

我想到了几个问题:

  1. 同时使用 ELMAH 和 NLog 是否太过分了?使用 ELMAH 处理未处理的异常,使用 NLog 处理其他所有事情?
  2. 是否可以配置 NLog 以在 nlog.config 中记录未处理的异常,或者是否需要自定义代码?
  3. 如果需要自定义代码,添加它的最佳位置在哪里? 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:

  1. Is using both ELMAH and NLog overkill? Using ELMAH for unhandled exceptions and NLog for everything else?
  2. Is it possible to configure NLog to log unhandled exceptions in the nlog.config or is custom code needed?
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忱杏 2024-10-07 05:03:35

回答第二个问题,我刚刚阅读了这篇文章,其中解释了如何记录未处理的日志NLog 的异常。基本上是在 Global.asax.cs 文件中添加如下内容:

protected void Application_Error() 
{
    Exception lastException = Server.GetLastError();
    NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
    logger.Fatal(lastException);
}

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:

protected void Application_Error() 
{
    Exception lastException = Server.GetLastError();
    NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
    logger.Fatal(lastException);
}
爱格式化 2024-10-07 05:03:35

事实上,您可以触发 ELMAH 的警报。就像这样:

 ErrorSignal.FromCurrentContext().Raise(new System.ApplicationException("An error occured in SendEmail()", ex));

不要忘记添加对 elmah.dll 的引用并将 using Elmah; 添加到文件中。
请参阅此处 更多示例。

You can in fact trigger an alert from ELMAH. Like so:

 ErrorSignal.FromCurrentContext().Raise(new System.ApplicationException("An error occured in SendEmail()", ex));

Don't forget to add a reference to elmah.dll and add using Elmah; to the file.
See here for a bit more examples.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文