Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

8- Logging Unhandled Exceptions

0 (0 Likes / 0 Dislikes)
so to log unhandled exception we are going to use a very popular package called Elmah In Package Manager Console, install-package Elmah Elmah stands for Error Logging Module and handlers the way it works is that it plugs into the record processing pipeline and if there is an unhandled exception it will grab its details and stores it by default it stores its details in the memory but with small configuration we can have it store exception in different kind of databases now let's see this in action back to the browser I am gonna go to home/about to throw an exception now for your information I change the custom error setting to remoteOnly and that's why you are seeing the exception details here locally but let's say we have deployed our application to production and if the exception has happened here and there to see this exception we can navigate to /Elmah.axd so elmah registers endpoint and when we hits this endpoint we see the exception elmah has captured so we can see the host, the type of the exception the error message, the user who got that exception as well as data and time so here is the blt if you click in this link Details we see a page that simulates the yellow screen of death so we can see the exception message as well as the stack trace in this case, I can see the exception happens in the About action in HomeController on line 19 and if you scroll down you can see Server Variable at the time the exception happens so all this data helps us troubleshoot the problem now a couple of things here first of all this endpoint elma.axd is only accessible locally for security reason when you deploy your application and you want to access this remotely you need to do a bit of configuration in web.config search for elmah.axd you see this location element and in this element we see the familiar web.cofig structure so we have <system.web> inside system.web we have HttpHandlers so elmah registers a handler for this path elma.axd so when we hit that endpoint this class Elmah.LogPageFactory is executed this class is essentially a HttpHandler and it is similar to an action in MVC but unlike an action, it is not part of a framework it is a class with one method that get the request and returns the response and it is a very low level way of processing a request and these days having framework like MVC you don't really need to create http handlers unless you want to create a framework so moving on we have this section <authorization> I am gonna take this out of the comment now with this code we are authorizing anyone with the admin role to access this endpoint and we are denying all other users we can also change this to users so if there is a specific users that can access that endpoint we can add them here e.g allow users="[email protected]" now when we save web.config our web application is restarted and because elmah by default store the exception details in the memory our log would be clear so when we get back here, look there is no exception so have a look at elmah documentation to find the config you need to add to store the exception details in SQLserver also other useful features e.g you can configurate to send emails when it logs exception but I leave it to you as additional reading

Video Details

Duration: 4 minutes and 14 seconds
Language: English
License: Dotsub - Standard License
Genre: None
Views: 0
Posted by: lpk1234 on Nov 6, 2019

Caption and Translate

    Sign In/Register for Dotsub to translate this video.