Genexus 中的应用程序级错误处理
Application Level Error Handling in Genexus
我正在尝试在 Genexus Ev1(C# 生成器)中处理应用程序级别的错误,以便在发生异常时可以将其记录下来。
网上搜索发现需求可以这样解决:
在 web.config 级别添加 CustomError 页面效果很好,但我无法检索服务器关于页面错误的信息。下面显示的变量 &ErrorMsg 总是 return 空
Event Start
&ErrorMsg.SetEmpty()
CSHARP System.Web.HttpServerUtility Server = System.Web.HttpContext.Current.Server;
CSHARP Exception ex = Server.GetLastError();
CSHARP if (ex != null) {
CSHARP ex = ex.GetBaseException();
CSHARP [!&ErrorMsg!] = ex.ToString();
CSHARP }
EndEvent
上面的代码(以及许多其他变体)不起作用。
我假设没问题,因为当到达执行该代码的错误页面时正在清除服务器异常,所以异常为空,然后 Global.asax 文件必须配置为捕获并传输异常
问题:
在 GeneXus 网络应用程序上没有 global.asax 文件,因此手动添加它并执行下面的代码也不起作用
<%@ language="C#" %>
<script runat="server">
void Application_Error(object sender, EventArgs e){
// Code that runs when an unhandled error occurs.
// Get last error from the server
Exception exc = Server.GetLastError();
if (exc is HttpUnhandledException){
if (exc.InnerException != null){
exc = new Exception(exc.InnerException.Message);
Server.Transfer("ErrorPageLog.aspx?handler=Application_Error%20-%20Global.asax",
true);
}
}
}
有人已经尝试在 GeneXus 中这样做了吗?
任何人都可以澄清我做错了什么?
经过几天的研究,我设法解决了这个问题。
这是我所做的:
- 创建一个文本文件并将其命名为Global.asax
- 将该文件移动到应用程序的 Web 根文件夹
编辑文件内容并添加此代码
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Session["ServerException"] = "";
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
protected void Application_Error(Object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs.
// Get last error from the server
Exception exc = Server.GetLastError();
Session["ServerException"] = exc.InnerException.Message;
Server.ClearError();
Response.Clear();
Response.Redirect("ErrorPageLog.aspx");
}
</script>
在 GeneXus KB 中创建新的 Web 面板并将其命名为 ErrorPageLog
编辑事件开始,添加此代码并构建它
Event Start
&ErrorMsg.SetEmpty()
CSHARP [!&ErrorMsg!] = System.Web.HttpContext.Current.Session["ServerException"].ToString();
EndEvent
这应该将应用程序错误通过网络会话传输到页面,然后您可以对消息做任何您想做的事情,还可以通过 GeneXus 代码对其进行管理。
注意:在这个例子中,我发送了关于异常的最简单的信息,可以管理关于它的更多细节。
我正在尝试在 Genexus Ev1(C# 生成器)中处理应用程序级别的错误,以便在发生异常时可以将其记录下来。
网上搜索发现需求可以这样解决:
在 web.config 级别添加 CustomError 页面效果很好,但我无法检索服务器关于页面错误的信息。下面显示的变量 &ErrorMsg 总是 return 空
Event Start
&ErrorMsg.SetEmpty()
CSHARP System.Web.HttpServerUtility Server = System.Web.HttpContext.Current.Server;
CSHARP Exception ex = Server.GetLastError();
CSHARP if (ex != null) {
CSHARP ex = ex.GetBaseException();
CSHARP [!&ErrorMsg!] = ex.ToString();
CSHARP }
EndEvent
上面的代码(以及许多其他变体)不起作用。
我假设没问题,因为当到达执行该代码的错误页面时正在清除服务器异常,所以异常为空,然后 Global.asax 文件必须配置为捕获并传输异常
问题:
在 GeneXus 网络应用程序上没有 global.asax 文件,因此手动添加它并执行下面的代码也不起作用
<%@ language="C#" %>
<script runat="server">
void Application_Error(object sender, EventArgs e){
// Code that runs when an unhandled error occurs.
// Get last error from the server
Exception exc = Server.GetLastError();
if (exc is HttpUnhandledException){
if (exc.InnerException != null){
exc = new Exception(exc.InnerException.Message);
Server.Transfer("ErrorPageLog.aspx?handler=Application_Error%20-%20Global.asax",
true);
}
}
}
有人已经尝试在 GeneXus 中这样做了吗? 任何人都可以澄清我做错了什么?
经过几天的研究,我设法解决了这个问题。
这是我所做的:
- 创建一个文本文件并将其命名为Global.asax
- 将该文件移动到应用程序的 Web 根文件夹
编辑文件内容并添加此代码
<%@ Application Language="C#" %> <script runat="server"> void Application_Start(object sender, EventArgs e) { // Code that runs on application startup } void Application_End(object sender, EventArgs e) { // Code that runs on application shutdown } void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started Session["ServerException"] = ""; } void Session_End(object sender, EventArgs e) { // Code that runs when a session ends. // Note: The Session_End event is raised only when the sessionstate mode // is set to InProc in the Web.config file. If session mode is set to StateServer // or SQLServer, the event is not raised. } protected void Application_Error(Object sender, EventArgs e) { // Code that runs when an unhandled error occurs. // Get last error from the server Exception exc = Server.GetLastError(); Session["ServerException"] = exc.InnerException.Message; Server.ClearError(); Response.Clear(); Response.Redirect("ErrorPageLog.aspx"); } </script>
在 GeneXus KB 中创建新的 Web 面板并将其命名为 ErrorPageLog
编辑事件开始,添加此代码并构建它
Event Start &ErrorMsg.SetEmpty() CSHARP [!&ErrorMsg!] = System.Web.HttpContext.Current.Session["ServerException"].ToString(); EndEvent
这应该将应用程序错误通过网络会话传输到页面,然后您可以对消息做任何您想做的事情,还可以通过 GeneXus 代码对其进行管理。
注意:在这个例子中,我发送了关于异常的最简单的信息,可以管理关于它的更多细节。