What are the Methods available to Log an Error and Trace an error in Mvc


I want to discuss how to log and trace an Error in Model View Controller Architecture . what are the Methods that exists to log and trace an error in Mvc. In this Article i will give a detail description of those methods to log and trace an error at Application level.

I want to discuss how to log and trace an Error in Model View Controller Architecture . what are the Methods that exists to log and trace an error in MVC. In this Article i will give a detail description of those methods to log and trace an error at Application level.

When you are working an application and deployed/hosted at the Client Place and there is an event that Causes an error you want to fix the error but you are not aware of the root cause of it hence we need to Log and trace those errors.

Now we know that we need to log and trace the error but what are the methods available and how to achieve that task in model view Controller Architecture using Asp.net.

There are several options to log the errors in asp.net MVC, They are given below.

So first we will try with a simple example how to log an exception



public Class BhushanLogger
{
public static void LogException(Exception ex)
{
EventLog log =new EventLog();
log.Source ="Erroredlog"
log.WriteEntry(ex.message);

}

}




Now with this piece of code the event will be logged in your hosted application and exception will be written in to it. But where you will find the exception than you need to do the following things .

First step is create key in the registry called ErroredLog. how to do it

Type regedit in the run command. click the below link to see where you want to create a Key.

path in your local computer

After that you need to create a Key in the Register key. Click the below link how to create a key.


logerrorkey


Method 2 : Simple Try / Catch Handler

In General Asp.net Try and Catch are very familiar to all the Programmers but in Model view Controller Architecture You have to place try and Catch block in every Controller This is how it looks like



Public ActionResult about()
{

Viewstate[Message] ="Your Successful app description page";
throw new Exception("Error thrown by the Application");
}
catch(Exception ex)
{
LogException(ex);
}



Custom Error Filters

Custom Error Filters is the best approach of handling the errors in Asp.net Model view Controller Architecture, It will allow you to write the logic at one place and apply throughout your site so the redundancy of the code will be Curtailed

To create a Custom Error Filter you need to inherit the HandleErrorAttribute class and override the onException() Method



public class CustomHandleError : HandleErrorAttribute
{
public Override void onException(ExceptionContext filterContext)
{
if(filterContext == null)
{
base.OnException(filterContext);
}

LogException(filterContext.OnException)

if(filterContext.httpContext.IsCustomErrorEnabled)
{
filterContext.ExceptionHandled =true;
base.OnException(filterContext);
}
}
private void LogException(Exception ex)
{
EventLog log =new EventLog();
log.source = "ErroredLog";
log.writeEntry(ex.message);
}
}


Attachments

  • path in your local computer (46230-1-path-in-your-local-computer.bmp)
  • logerrorkey (46230-3-logerrorkey.bmp)
  • Article by srirama
    A Good advice from parent to a Child , Master to a Student , Scholar to an Ignorant is like a doctor prescribed pill it is bitter to take but when they take it will do all good for them --- Bhushan

    Follow srirama or read 73 articles authored by srirama

    Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: