0

I have deployed web api(C#.Net framework 4.7.2) in Azure App Service. All the exceptions captured in the application is logged into Application Insights. The code is running fine and users are using it currently. the problem is any exception thrown in the code is displayed to end users with stack track info. I want to avoid showing stack info and unfriendly error to user. I can implement custom error page to redirect the user to show user friendly message.

Before implementing custom error page, is there any way using Azure can I show user friendly error messages if there are any application exceptions thrown in the app. I googled it for quite sometime but could not find any solid handy info.

ErrorHandler.cs:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)] 
public class AiHandleErrorAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        if (filterContext != null && filterContext.HttpContext != null && filterContext.Exception != null)
        {
            //If customError is Off, then AI HTTPModule will report the exception
            if (filterContext.HttpContext.IsCustomErrorEnabled)
            {   
                var ai = new TelemetryClient();
                ai.TrackException(filterContext.Exception);
            } 
        }
        base.OnException(filterContext);
    }
}

Web.config

<system.web>
<customErrors mode="Off" />
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.5" />
<httpModules>
  <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
Govind
  • 979
  • 4
  • 14
  • 45
  • Unless I am missing something, is it not easier to show a user friendly message on the front-end if your back-end API is throwing exceptions ( http 500 status codes) rather than doing things on Azure App Service. – rohit Sep 27 '20 at 17:40
  • I am assuming you have a web api which is being called by front-end (Angular, React, Vue etc). – rohit Sep 27 '20 at 18:03
  • @rohit no we dont have front end.. it is backend service.. this web api is consumed by 3rd party application – Govind Sep 28 '20 at 04:19
  • 1
    You can read this post, hope it useful to you. https://stackoverflow.com/questions/63581460/need-to-preserve-http-status-code-when-using-httperrors-custom-404-error-using-r/63591653#63591653 – Jason Pan Sep 28 '20 at 05:10

0 Answers0