ELMAH显示异常数据字典中的数据

发布于 2024-07-30 10:56:13 字数 359 浏览 6 评论 0原文

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

When using ELMAH (which is brilliant) is it possible to view extra information that you have added to an exception.

E.g.

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);    

When I view the exception from elmah.axd it doesn’t seem to show the “ExtraInfo” key and value information, just the exception string.

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

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

发布评论

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

评论(7

指尖凝香 2024-08-06 10:56:13

我的解决方案是将信息添加到服务器变量集合中,如下所示。

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

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

My solution was to add the information to the Server Variables collection like so.

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

Yes, I Think this is a hack.
For small amounts of additional data consider encapsulating the error as suggested by @Roma
However this method is especially useful where you have too much information to put into an 'encapsulated error message'

寄离 2024-08-06 10:56:13

解决这个问题的简单方法就是封装错误。 外部错误将包含您的自定义消息。

string msg = "my custom error message";

ErrorSignal.FromCurrentContext().Raise(
               new Elmah.ApplicationException(msg,ex));

The simple way to go around, is to encapsulate the error. The outer error will contain you custom message.

string msg = "my custom error message";

ErrorSignal.FromCurrentContext().Raise(
               new Elmah.ApplicationException(msg,ex));
假情假意假温柔 2024-08-06 10:56:13

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

No, it is not possible to view the extra information with the current 1.x releases.

诠释孤独 2024-08-06 10:56:13

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

您可以在这里找到它: https://github.com/boena/elmah-with-custom -数据

Ok, so I have been waiting for this to be included for too long now and decided to make a own fork of it and include the feature myself.

You can find it here: https://github.com/boena/elmah-with-custom-data

够运 2024-08-06 10:56:13

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

sample exception in 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
            );
    }
}

Elmah uses ToString() method to get exception details. Just override your custom exception ToString() method and this way it will work:

sample exception in elmah

My exception class:

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
            );
    }
}
━╋う一瞬間旳綻放 2024-08-06 10:56:13

好吧,花了一天时间研究这个问题,我想我应该分享我的解决方案。 与上面 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();
    }
}

Well, having spent the day working on this, I thought I'd share my solution. Very similar to Myster's solution above, the difference being I modify the error object used by elmah instead of the request server variables, like so:

    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);
    }

Then, depending on your application to call logger.LogError:

mvc add a custom error filter (http://maheshde.blogspot.ca/2012/09/error-handing-with-mvc-using-custom.html)

webforms override Application_Error in global.asax

Then, just one final step in the global.asax, dismiss any errors that have already been logged:

public void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
    if (e.Exception.Data.Contains("Logged"))
    {
        if (e.Exception.Data["Logged"].Equals(true)) e.Dismiss();
    }
}
农村范ル 2024-08-06 10:56:13

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

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

如果您使用 MSSQL

  • 查看 SqlErrorLog.cs

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

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

Since it is open source, you could just grab the project and customize Elmah for your own purposes:

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

If you are using MSSQL

  • take a look at SqlErrorLog.cs

  • SQLServer.sql to generate your own database

I'm doing the same, but I need to add an extra randomly generated "error code" column and I'll be using exception data to track all the custom, session, form, cookie information that would normally be in the uber large xml field. I may change this to JSON, not sure yet.

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