Can you point me to some tutorial or samples on how I can log all un-handled exceptions that are occurring on my mvc web app using log4net. Thank you
-
3You may or may not find this interesting (I'm not sure if it will work with log4net): ELMAH - http://code.google.com/p/elmah/ – Michael Maddox Sep 02 '09 at 17:43
-
Thanks! I actually also hooked my app to Elmah while working on this problem and I really like the handy interface it has. I see my self using it more compared to log4 net for now but I will see how that goes with more experience.. – kaivalya Sep 02 '09 at 19:14
5 Answers
I paste below the code I use on my global.asax that seems to satisfy me for now.. I am getting all unhandled exceptions on my log4net generated log files..
public class MvcApplication : System.Web.HttpApplication
{
private static readonly ILog log = LogManager.GetLogger(typeof(MvcApplication));
void Application_Error(Object sender, EventArgs e)
{
Exception ex = Server.GetLastError().GetBaseException();
log.Error("App_Error", ex);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
log4net.Config.XmlConfigurator.Configure();
}
}

- 3,829
- 9
- 34
- 43
-
2You can also use an HttpModule to register the Error event handler: http://codebetter.com/blogs/karlseguin/archive/2006/06/12/146356.aspx – dahlbyk Sep 02 '09 at 19:22
-
1You'll want to add `routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });` to your RegisterRoutes method if you don't have a favicon located within your site – Tr1stan Oct 27 '10 at 15:50
-
About HttpModule: the method to register the module detailed in the linked article applies to the old Classic IIS pipeline mode, not the (now recommended) integrated mode. See https://msdn.microsoft.com/library/ms227673(v=vs.100).aspx for details on how to register it if you use integrated mode. – personne3000 Nov 11 '15 at 03:23
-
For ASP.NET applications you like to use the System.Web.HttpApplication.Error
event which is placed in Global.asax
file.
protected void Application_Error(Object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
// Stop error from displaying on the client browser
Context.ClearError();
logger.Fatal(ex.ToString);
}

- 71
- 1
- 1
You might be interested to check out what Aspect Oriented Programming (AOP) is.
We accomplished this with Post sharp (only - we didn't use log4net but custom tracer).
Just check out first if log4net haven't something for this 'out-of-the-box'. I'm not sure about that.

- 45,880
- 29
- 115
- 195
There is no built-in support for this in log4net. You should implement the Application_Error event in Global.asax and call your log4net logger there. The event will be triggered for all unhandled events in your app.

- 33,668
- 7
- 97
- 131
Without being facetious here you go.
try
{
NotImplementedException nie = new NotImplementedException();
throw nie;
}
catch (Exception e)
{
Log.Fatal(e);
}
Assuming you are using .net 3.5, you could use extension methods, and extend System.Exception and add a Log(log4net.ILog log) method, then wherever you catch an exception in your app, assuming you've setup the Log instance correctly, then you easily do it. Your extension class could grow quite a bit, with various overloads for Debug, Info, Warn, Error and Fatal etc.

- 3,043
- 1
- 27
- 36
-
1sorry my question was not clear - I need to log all unhandled exceptions – kaivalya Sep 02 '09 at 14:34