12

使用 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”键和值信息,只是显示异常字符串。

4

7 回答 7

20

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

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

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

于 2012-02-23T03:56:56.467 回答
7

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

string msg = "my custom error message";

ErrorSignal.FromCurrentContext().Raise(
               new Elmah.ApplicationException(msg,ex));
于 2010-02-25T14:43:13.630 回答
2

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

于 2009-08-04T05:35:18.703 回答
2

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

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

于 2012-11-23T14:24:54.893 回答
2

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

elmah 中的示例异常

我的异常类:

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
            );
    }
}
于 2014-11-19T08:39:18.293 回答
1

好吧,花了一天的时间来解决这个问题,我想我会分享我的解决方案。与上面 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();
    }
}
于 2014-03-25T21:52:14.307 回答
-1

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

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

如果您使用的是 MSSQL

  • 看看 SqlErrorLog.cs

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

我也在做同样的事情,但我需要添加一个额外的随机生成的“错误代码”列,并且我将使用异常数据来跟踪通常在 uber large xml 中的所有自定义、会话、表单、cookie 信息场地。我可能会将其更改为 JSON,但还不确定。

于 2012-05-09T17:01:34.563 回答