我的 Web 表单继承了一个名为 的类MyPageMain
,该类继承System.Web.UI.Page
并位于App_Code
文件夹中。
在这个MyPageMain
类中,我创建了这个方法,用于记录和输出异常(错误消息):
public string Error(Exception pException, string pFriendlyMessage)
{
using (BusError erro = new BusError())
{
int? errorId = //HERE Routine to log the error;
StringWriter stringWriter = new StringWriter();
using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, "erroMain");
writer.RenderBeginTag(HtmlTextWriterTag.Div); //<div>
writer.RenderBeginTag(HtmlTextWriterTag.P); //<p>
writer.Write(pFriendlyMessage);
writer.RenderEndTag(); // </p>
writer.RenderBeginTag(HtmlTextWriterTag.Small);
writer.Write("Event tracker:");
writer.Write(errorId);
writer.RenderEndTag();
writer.RenderEndTag(); // </div>
Console.WriteLine(stringWriter.ToString());
}
}
}
然后,当页面出现异常时,我调用它,如下所示:
Protected void btnLoad_Click(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
//Loading some data here.
}
catch (Exception ex)
{
Error(ex, "Unexpected error trying to load data.");
}
}
这很好,但不起作用......我认为原因之一可能是整个事情都在UpdatePanel内。关于如何使这项工作的任何建议?
这Console.Writeline
适合我想做的事情吗?这适用于 UpdatePanels 吗?
已经尝试过Response.Write(...)
,它确实有效。但不在一个UpdatePanel