Let's Explore Diagnostics in Asp.net Core 1.0

Datetime:2016-08-23 01:04:25          Topic: ASP.NET           Share

Introduction

In theprevious article, we have explored briefly about the Asp.net Core 1.0 in general. In this article, we will look into a small but important feature of Asp.net Core 1.0 which is Diagnostics .

Let's start the experiment

Let's fire us VS 2015 and choose Web->Asp.net Core Web Applications(.Net Core). Choose the target Framework as .Net Framework 4.6.1

Click on "OK" button to get the next screen

ASP.NET Core 1.0 includes only Web API and MVC but not SignalR or Web Pages yet. We choose the Web Application and Click "OK" button and here is our "Welcome Screen"

Add an action to the HomeController.cs file

public IActionResult ExploringDiagnosticsInAspNetCore()
{
    var x = 0;
    var y = 0;
    var z = x / y;

    ViewData["Message"] = " The value is " + z;           

    return View();
}

Add a corresponding view ExploringDiagnosticsInAspNetCore.cshtml

@{
    ViewData["Title"] = "Asp.Net Core 1.0 - Exploring Diagnostics Feature In AspNet Core";
}
<h2>@ViewData["Title"]</h2>
<h3>@ViewData["Message"]</h3>

<p>HAPPY LEARNING  - Asp.Net Core 1.0</p>

Add a link in _Layout.cshtml

<div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
        <li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
        <li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>
        <li><a asp-area="" asp-controller="Home" asp-action="ExploringDiagnosticsInAspNetCore">Exploring Diagnostics</a></li>
    </ul>
</div>

Now run the application. The landing page looks as under

When we click on the Exploring Diagnostics link, the below appears

This does not shows up anything. However, we need to find out the exact error. This can be done by using Microsoft.AspNet.Diagnostics .

Let's open the Startup.cs file and in the Configure method add the below piece of code.

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();    
}

This method belongs to the Microsoft.AspNetCore.Builder namespace of DeveloperExceptionPageExtensions static class. The purpose of this function is to capture synchronous and asynchronous System.Exception instances from the pipeline and generates HTML error responses. It returns a reference to the app after the operation is completed.

It has an overloaded method

public static IApplicationBuilder UseDeveloperExceptionPage(this IApplicationBuilder app, DeveloperExceptionPageOptions options);

The additional DeveloperExceptionPageOptions specifies options for the middleware.

We used the UseDeveloperException() extension method to render the exception during the development mode.

The complete code for the Configure method is as under

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseApplicationInsightsRequestTelemetry();

    if (env.IsDevelopment())
    {                
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseApplicationInsightsExceptionTelemetry();

    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

Let us again run the application and the following result will be displayed

This shows the stack trace . In our case it is DivideByZeroException: Attempted to divide by zero exception. Interesting thing to note is that, it shows the line number which is 34 in this case and also what code line cause the exception which is var z = x / y;

Now let us hover the cursor over the line

It's highlighting with maroon color. Click on that link and the below expands

It also presents the Action method that has the problem and also the Controller name which is HomeController.cs .

Now let us rectify the problem as under

public IActionResult ExploringDiagnosticsInAspNetCore()
{            
    var x = 100;
    var y = 20;
    var z = x / y;

    ViewData["Message"] = " The value is " + z;           

    return View();
}

and run the application again

Conclusion

In this article, we have learnt about the Diagnostics features of Asp.net Core 1.0 and have seen how easily we can figure and sort out the coding issue in the application. Hope this will be helpful. Thanks for reading. Zipped file attached.





About List