【问题标题】:ELMAH display data in exception data dictionaryELMAH 在异常数据字典中显示数据
【发布时间】:2023-03-30 12:44:01
【问题描述】:

使用 ELMAH(非常棒)时,可以查看您添加到异常中的额外信息。

例如

Exception ex = new Exception("New exception to use ErrorSignal functionality");
ex.Data.Add("ExtraInfo", "Here is some extra information i would like to be displayed.");
ErrorSignal.FromCurrentContext().Raise(ex);    

当我从 elmah.axd 查看异常时,它似乎没有显示“ExtraInfo”键和值信息,只是显示异常字符串。

【问题讨论】:

    标签: elmah


    【解决方案1】:

    我的解决方案是像这样将信息添加到服务器变量集合中。

    var context = HttpContext.Current;
    context.Request.ServerVariables["ERROR_CALLING_WEBSERVICE_URI"] = uri;
    Elmah.ErrorLog.GetDefault(context).Log(new Error(e, context))
    

    是的,我认为这是一个 hack。
    对于少量附加数据,请考虑按照@Roma
    的建议封装错误 但是,当您有太多信息无法放入“封装的错误消息”时,此方法特别有用

    【讨论】:

    • 此方法在您引发错误之前有效,但您也可以通过 global.asax 对void ErrorLog_Filtering(object sender, Elmah.ExceptionFilterEventArgs e) 中的所有错误执行此操作(在创建错误对象之前)并转换e.Context as HttpContext跨度>
    • 非常好的答案,但这仅适用于 Web 应用程序,而不适用于应用程序的域,因为您需要访问 HTTP 上下文。
    【解决方案2】:

    绕过的简单方法是封装错误。外部错误将包含您的自定义消息。

    string msg = "my custom error message";
    
    ErrorSignal.FromCurrentContext().Raise(
                   new Elmah.ApplicationException(msg,ex));
    

    【讨论】:

      【解决方案3】:

      不,当前 1.x 版本无法查看额外信息。

      【讨论】:

      • Atif,我强烈推荐添加这个功能!
      • 同意,这已经是一个记录问题 [1]。您可以为该问题加注星标以关注它​​的更新。 [1]code.google.com/p/elmah/issues/detail?id=162
      • 我认为该页面上已经有一个补丁,但不幸的是它还没有进入正式版本。
      • 正式发布了吗?
      • 问题注册已移至 github,所以你现在可以去这里:github.com/elmah/Elmah/issues/162
      【解决方案4】:

      好的,所以我一直在等待它被包含太久,并决定制作一个自己的分支并自己包含该功能。

      你可以在这里找到它:https://github.com/boena/elmah-with-custom-data

      【讨论】:

      • 你有它的代码吗?您上面给出的链接无效。
      • 哦,对不起。从那以后我已经重命名了它,我现在看到了。希望您通过浏览我的存储库找到它,否则您可以找到它link,如果您也不想显示自定义数据的 Log Analyzer 桌面应用程序,它就在这里link
      • 我已经修复了断开的链接
      • @boena,链接又坏了。
      【解决方案5】:

      Elmah 使用 ToString() 方法获取异常详细信息。只需覆盖您的自定义异常 ToString() 方法,这样它就可以工作:

      我的异常类:

      public class MyCustomException : Exception
      {
          public string CustomProperty { get; set; }
      
          public MyCustomException(string message, string customProperty): base(message)
          {
              this.CustomProperty = customProperty;
          }
      
          public override string ToString()
          {
              var result = base.ToString();
              var propertyData = String.Format("CustomProperty: {0}", this.CustomProperty);
      
              return result.Insert(
                  result.IndexOf(Environment.NewLine),
                  Environment.NewLine + propertyData
                  );
          }
      }
      

      【讨论】:

        【解决方案6】:

        好吧,花了一天的时间来解决这个问题,我想我会分享我的解决方案。与上面 Myster 的解决方案非常相似,不同之处在于我修改了 elmah 使用的错误对象而不是请求服务器变量,如下所示:

            public static void LogError(Exception e, IDictionary<string, string> customFields = null)
            {
                var logger = ErrorLog.GetDefault(HttpContext.Current);
        
                customFields = customFields ?? new Dictionary<string, string>();
        
                // Used to tell elmah not to log this twice (See global.asax)
                e.Data.Add("Logged", true);
        
                var error = new Error(e, HttpContext.Current);
        
                customFields.ForEach(pair => error.ServerVariables.Add(pair.Key, pair.Value));
        
                logger.Log(error);
            }
        

        然后,根据你的应用调用logger.LogError:

        mvc 添加自定义错误过滤器 (http://maheshde.blogspot.ca/2012/09/error-handing-with-mvc-using-custom.html)

        webforms 覆盖 global.asax 中的 Application_Error

        然后,仅在 global.asax 中的最后一步,消除已记录的所有错误:

        public void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
        {
            if (e.Exception.Data.Contains("Logged"))
            {
                if (e.Exception.Data["Logged"].Equals(true)) e.Dismiss();
            }
        }
        

        【讨论】:

          【解决方案7】:

          由于它是开源的,您可以直接获取该项目并根据自己的目的自定义 Elmah:

          http://code.google.com/p/elmah/

          如果您使用的是 MSSQL

          • 看看 SqlErrorLog.cs

          • SQLServer.sql 生成自己的数据库

          我也在做同样的事情,但是我需要添加一个额外的随机生成的“错误代码”列,并且我将使用异常数据来跟踪通常在超级大的 xml 字段。我可能会将其更改为 JSON,还不确定。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-06-15
            • 1970-01-01
            • 2012-06-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多