8

I have code which is catching all exceptions in Global.asax

 protected void Application_Error(object sender, EventArgs e) 
        {
            System.Web.HttpContext context = HttpContext.Current;
            System.Exception exc = context.Server.GetLastError();
            var ip = context.Request.ServerVariables["REMOTE_ADDR"];
            var url = context.Request.Url.ToString();
            var msg = exc.Message.ToString();
            var stack = exc.StackTrace.ToString();
        }

How I can get controller name where this error happened

How I can get request client IP?

And can I filter exceptions? I dont need 404, 504.... erors

thanks

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
Irakli Lekishvili
  • 33,492
  • 33
  • 111
  • 169
  • `I dont need 404, 504.... erors` ehh? That's how HTTP works. And what do you mean with `can I filter exceptions`? – jgauffin Sep 26 '12 at 06:52
  • This post will definitely help you http://prideparrot.com/blog/archive/2012/5/exception_handling_in_asp_net_mvc – VJAI Sep 26 '12 at 12:39

3 Answers3

6

Global.asax has not notion of controllers and actions, so I believe there is no an API for retrieving controller and action names. However you might give a try for resolving request URL:

HttpContextBase currentContext = new HttpContextWrapper(HttpContext.Current);
UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
RouteData routeData = urlHelper.RouteCollection.GetRouteData(currentContext);
string action = routeData.Values["action"] as string;
string controller = routeData.Values["controller"] as string;

To get the user IP you can use UserHostAddress property:

string userIP = HttpContext.Current.Request.UserHostAddress;

To filter out HTTP exceptions that you are not going to handle you can use something like:

HttpException httpException = exception as HttpException;
if (httpException != null)
{
    switch (httpException.GetHttpCode())
    {
        case 404:
        case 504:
            return;
    }
}

One last remark about exception handling - it is not a best practice to do it at global level when there is a way to perform it more locally. For instance in ASP.NET MVC base Controller class has a method:

protected virtual void OnException(ExceptionContext filterContext)

which, when overridden, will give you full control on the occurred exception. You can have all the info that is available for you in Global.asax plus ASP.NET MVC specific features like a reference to controller, view context, route data etc.

Andrei
  • 55,890
  • 9
  • 87
  • 108
  • Andrei thank you for replay your code works perfectly. Can you provide some good tutorial about base controller class? – Irakli Lekishvili Sep 26 '12 at 08:02
  • @Acid, sure. For basic examples check out this [blog post](http://www.entwicklungsgedanken.de/2009/07/16/redirect-from-the-onexception-method-inside-the-controller-with-asp-net-mvc/) and this [article](http://www.devproconnections.com/article/aspnetmvc/aspnet-exception-141263). – Andrei Sep 26 '12 at 08:23
  • You can skip the using UrlHelper usage (And skip the MVC reference) with...'RouteData routeData = System.Web.Routing.RouteTable.Routes.GetRouteData(currentContext);' – Carter Medlin Feb 19 '13 at 22:03
3

i used like this it's below

you can get user ip like this

var userip = context.Request.UserAgent; 

and you can get your url where this error happened like this

var ururl = System.Web.HttpContext.Current.Request.Url; 

i think this will help you...

Rajpurohit
  • 1,951
  • 2
  • 16
  • 19
0

I'd take a different tack and put use an attribute on your controllers (or base controller if you have one)

 public class HandleErrorAttributeCustom : HandleErrorAttribute
    {
       public override void OnException(ExceptionContext context)
        {
            //you can get controller by using
            context.Controller.GetType()

            //however, I'd recommend pluggin in Elmah here instead 
            //as it gives this easily and also can has filtering
            //options that you want

        }
}
dove
  • 20,469
  • 14
  • 82
  • 108