Pages

11 Jan 2013

Simple Custom Error Page


  1. In web.config set: 
    •  CallStack="false" 
    •  customErrors mode="On"
  2. Run Powershell command "Set-SPCustomLayoutsPage -Identity Error -RelativePath /_layouts/CustomError/error.aspx -WebApplication http://Srv
  3. Add a new ASPX page to /_layouts/CustomError/error.aspx: 
You can download source code from here. My custom error page looks like that:


Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="Microsoft.SharePoint.Administration"  %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Error</title>
</head>
<script language="C#" runat="server">

    public void Page_Load(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("Error occured:");

        Exception[] errors = Context.AllErrors;

        if (errors == null)
        return;

        for (int i = 0; i < errors.Length; i++)
        {
            Exception err = errors[i];
            LogError(err.Message);

            sb.Append("<br>");
            sb.Append(err.Message);
            sb.Append("<br><br>Stack:<br>");
            sb.Append(err.StackTrace);

            if (err.InnerException != null && err.InnerException.Message != err.Message)
                sb.Append("<br><br>" + err.InnerException.Message);
        }

        lblError.Text = sb.ToString();        
    }

    private static void LogError(string message)
    {
        uint customId = 7777;
        SPDiagnosticsService.Local.WriteTrace(customId,
            new SPDiagnosticsCategory("CustomError Message", TraceSeverity.Unexpected, EventSeverity.Error),
            TraceSeverity.Unexpected, message);
    }

</script>

<body>
<asp:Label ID="lblError" runat="server" />
</body>
</html>

No comments:

Post a Comment