使用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);
Run Code Online (Sandbox Code Playgroud)
当我从elmah.axd查看异常时,它似乎没有显示"ExtraInfo"键和值信息,只是异常字符串.
Mys*_*ter 20
我的解决方案是将信息添加到Server Variables集合中.
var context = HttpContext.Current;
context.Request.ServerVariables["ERROR_CALLING_WEBSERVICE_URI"] = uri;
Elmah.ErrorLog.GetDefault(context).Log(new Error(e, context))
Run Code Online (Sandbox Code Playgroud)
是的,我认为这是一个黑客.
对于少量的附加数据,请考虑按@Roma的建议封装错误.
但是,如果您有太多信息要放入"封装的错误消息",此方法尤其有用
小智 7
简单的方法是封装错误.外部错误将包含您自定义消息.
string msg = "my custom error message";
ErrorSignal.FromCurrentContext().Raise(
new Elmah.ApplicationException(msg,ex));
Run Code Online (Sandbox Code Playgroud)