您应该在 asp.net-mvc 中的哪里进行定期日志记录?你应该使用 ELMAH 吗?

发布于 2024-11-19 09:09:31 字数 177 浏览 5 评论 0原文

我想在项目从我的 asp.net 缓存中删除时开始记录,并且我试图找出在哪里以及如何执行此操作。

我发现 ELMAH 被推荐用于应用程序范围的异常处理日志系统,但也可以(并且应该)用于常规日志记录仪器,如上面的示例?

I want to start logging when items get removed from my asp.net cache and i am trying to figure out where and how to do this.

i see that ELMAH is recommended for application wide exception handling logging system but can (and should) it also be used for regular logging instrumentation like the example above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梓梦 2024-11-26 09:09:31

我认为您可以同时使用 ELMAH 和 Log4Net(或 NLog)。使用两者将为您带来最大的灵活性。我希望现在就可以详细说明,但我至少想回复您并让您知道,根据我的经验以及我读到的其他人所做的事情,使用两者都是最好的解决方案。如果您需要解释等,请告诉我。

I think that use you use both ELMAH and Log4Net (or NLog). Using both will get you the most flexibility. I wish I could go into extreme details right now, but I wanted to at least respond to you and let you know that using BOTH are the best solution from what I have experienced, and what I have read others have done. Please let me know if you need explanations etc..

尘世孤行 2024-11-26 09:09:31

我们一直是一家纯粹的 Log4Net 商店,直到 Log4net 多次停止在生产中为我们记录日志。为了避免这种情况发生,我们决定为整个应用程序实现 ELMAH(即使是基本日志记录)。我们对结果非常满意,因为所有日志语句都出现在一个位置,并且不再需要遍历文本文件(除非我们想要遍历 XML 文件)。我们保留 Log4Net 又进行了一次迭代,但最终发现它没什么用处,并将其从我们的应用程序中完全删除。

我可以理解为什么您可能希望为一个站点使用两个不同的日志框架,特别是因为 ELMAH 需要 HttpContext 才能充分发挥其潜力。然而,我们确实找到了一种在没有 HttpContext 的情况下将异常传递给 ELMAH 的方法:

        /// <summary>
        /// Main logger method that will log an exception and/or a message
        /// </summary>
        /// <param name="exception">Exception to log</param>
        /// <param name="message">Message to log</param>
        private static void Log(Exception exception, string message)
        {
                Error elmahError = exception != null ? new Error(exception) : new Error();

                if (!string.IsNullOrWhiteSpace(message))
                {
                    elmahError.Message = message;
                }

                Elmah.ErrorLog.GetDefault(null).Log(elmahError);             
        }

然后在你的 web.config 中你只需要指定 applicationName

<elmah>
    <security allowRemoteAccess="1" />
    <errorLog name="ELMAHLogger" applicationName="xxx" type="Elmah.XmlFileErrorLog, Elmah" logPath="~/ELMAH_Production_Logs" />
  </elmah>

同样,我认为 Log4Net 是一个很好的日志框架,它为我们工作了很多年。我发现 ELMAH 在查找异常时更容易使用,并且上面的代码允许我们在应用程序中 HttpContext 可能不可用的任何地方使用它。

We had been a purely Log4Net shop until Log4net stopped logging for us in production on numerous occasions. To circumvent this from happening we decided to implement ELMAH for our entire application (even for basic logging). We could not be happier with the results as all of our log statements appear in one spot and there are no more text files to traverse (unless we wanted to traverse the XML files). We kept Log4Net around for one more iteration, but finally found it to be of little use and have completely removed it from our application.

I can see why you might want to have two different logging frameworks for a site, especially since ELMAH requires an HttpContext in order to work to its full potential. However, we did find a way to pass the exception to ELMAH without an HttpContext:

        /// <summary>
        /// Main logger method that will log an exception and/or a message
        /// </summary>
        /// <param name="exception">Exception to log</param>
        /// <param name="message">Message to log</param>
        private static void Log(Exception exception, string message)
        {
                Error elmahError = exception != null ? new Error(exception) : new Error();

                if (!string.IsNullOrWhiteSpace(message))
                {
                    elmahError.Message = message;
                }

                Elmah.ErrorLog.GetDefault(null).Log(elmahError);             
        }

Then in your web.config you just need to specify the applicationName:

<elmah>
    <security allowRemoteAccess="1" />
    <errorLog name="ELMAHLogger" applicationName="xxx" type="Elmah.XmlFileErrorLog, Elmah" logPath="~/ELMAH_Production_Logs" />
  </elmah>

Again, I think Log4Net is a fine logging framework and it worked for us for a number of years. I find ELMAH to be easier to use when looking for an exception and the above code has allowed us to use it anywhere in our application where the HttpContext might not be available.

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