Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

In MVC app - Trace statement put in part of a controller action is NOT showing up if an exception is occured say like divide by zero exception in one of the actions #1004

Open
arulmoli opened this issue Jul 20, 2017 · 0 comments

Comments

@arulmoli
Copy link

I am trying to write a "trace" statement AFTER an exception had occurred. For example in a controller i am overriding the "protected override void OnException(ExceptionContext filterContext)" function and i simply put a trace statement. To simulate an exception, inside an action in that controller where i am overriding the "OnException" function, i just try to divide a number by 0, so that it will throw an exception and i see the exception getting routed to "OnException" handler.

After that, I don't see that in "Glimpse-Trace" tab. Does anyone know what might be happening? I do see the normal traces that i put in before any exception occurs. Thank you!

My sample code:
Note, i called the action below called "GetAwardsLeadershipPage" using an ajax call from a razor view.

public class MembersPortfolioController : Controller
{

    protected override void OnException(ExceptionContext filterContext)
    {
         ///////////////////////////////////////////////////////////////////////////////////////////////////
        // Write to log until i figure a way out on how to show the exception.
        // Write to Trace later and install Glimpse where youc an see the exceptions on the client side.
        ///////////////////////////////////////////////////////////////////////////////////////////////////
        if (_log != null)
        {
            string errMsg = string.Format("[{0}][{1}]: Exception:{2} StackTrace={3}", 
                                            filterContext.RouteData.Values["controller"].ToString(),
                                            filterContext.RouteData.Values["action"].ToString(),
                                            filterContext.Exception.Message,
                                            filterContext.Exception.StackTrace);

                 // For Glimpse to catch "System.Diagnostics.Trace" and display it in Glimpse's view - this will 
                 // be useful when we want to debug what's happening on the server side from the browser 
                 //itself (without logging into production machine).
            Trace.Write(errMsg);
        }

    }

    public ActionResult GetAwardsLeadershipPage()
    {
        Trace.Write("Before divide by zero");

        // To simulate divide by zero exception.
        int result = 100 / int.Parse("0");

        return PartialView("_AwardsLeadership", null);
    }
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant