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>