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
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:
To get the user IP you can use
UserHostAddressproperty:To filter out HTTP exceptions that you are not going to handle you can use something like:
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
Controllerclass has a method: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.