Search code examples
c#.netgenexus

Application Level Error Handling in Genexus


I'm trying to handle errors at aplication level in Genexus Ev1 (C# generator) so when an exception ocurs it can be logged.

Searching the web found the requirement can be resolved with this:

https://learn.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/aspnet-error-handling

adding a CustomError page at web.config level works fine, but i can't retrieve server's information about the error in the page. Variable &ErrorMsg shown below always return empty

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

The code above (and many other variations) doesn't work.

I assume It's ok, because the server exception is being cleared when the error page executing that code is reached so exception is null, then Global.asax file must be configured to catch and transfer the exception

The problem:

On GeneXus web apps there's no global.asax file, so adding it manually executing code below doesn't work either

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

Theres anyone who have tried to do this in GeneXus already? Anyone can clarify what I'm doing wrong?


Solution

  • After a few days of research I've managed to solve this issue.

    Here is what i've done:

    • Create a text file and name it Global.asax
    • Move that file to the application's web root folder
    • Edit file content and add this code

      <%@ 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>
      
    • Create new Web Panel within GeneXus KB and name it ErrorPageLog

    • Edit Event Start, add this code and build it

      Event Start
          &ErrorMsg.SetEmpty()
      
          CSHARP [!&ErrorMsg!] = System.Web.HttpContext.Current.Session["ServerException"].ToString();
      EndEvent    
      

    That should transfer the application error trought web session to the page, then you can do whatever you want with the message and also be able to manage it trought GeneXus code.

    NOTE: In this example I'm sending the simplest information about the exception, further details about it can be managed.