1

Hi,

I am looking for a simple way to log exceptions to the database in my ASP.NET MVC application. I have looked at the "Exception Management Application Block" but I can´t find any simple and clear articels about how to handle this in ASP.NET MVC?

Maby I should just catch the exception as far up as possible and then log it to database but Im not sure how to do that in ASP.NET MVC. In WindowsForms there is diffrent events(like unexpectedException) to listen to where I can log, is there anything like that in ASP.NET MVC?

BestRegards

Edit1: I found a tool called elmah http://code.google.com/p/elmah/ but im not sure if this is a good solution and if it works well with ASP.NET MVC.

Then I also found this article http://www.davidjuth.com/asp-net-mvc-error-handler.aspx that looks easy and clear but I also do not know if this is the right way to go?

Banshee
  • 15,376
  • 38
  • 128
  • 219

2 Answers2

3

I would suggest you have a look at NLog for logging exceptions: http://nlog-project.org - you can configure it to dump the exceptions into a database really easily.

As for MVC error logging - this question Error Handling in ASP.NET MVC has some really good solutions in it.

Community
  • 1
  • 1
kmp
  • 10,535
  • 11
  • 75
  • 125
  • Aha I have heard about nlog before, I will take a look at it. How would the elmah compare to nlog? It seems like elmah handles all exceptions, log them and provides monitor for online view? This sounds great but I dont know if it works that well with ASP.NET MVC? I also found this article that looks clear and straight forward : http://www.davidjuth.com/asp-net-mvc-error-handler.aspx. Any experience with those? – Banshee Jan 15 '12 at 10:35
  • @SnowJim: see my answer and the linked articles. The article at davidjuth.com is outdated. ELMAH works great with MVC. Compared to NLog: ELMAH will log all exceptions with little more than installing the ELMAH package but it will log nothing else. With NLog, you will be responsible for all logging calls but of course you could log anything - it also will not provide a web UI to view the logs. ELMAH is most likely what you want, it records a lot of detail from the HttpContext that you will want logged in a web app. – quentin-starin Jan 15 '12 at 10:39
  • Never used ELMAH - but I have used NLog with the Castle Windsor IoC container and ASP .NET MVC and it worked really well and easily. I would try them both and see which you prefer - there are so many logging frameworks out there and they are all good - often comes down to personal preference - just make sure you use one that sits nicely in the way you like to do things and then the friction is removed and you can get on with writing the applications you need! – kmp Jan 15 '12 at 10:41
  • @user1039947: I guess my point is that in an ASP.Net application, ELMAH removes *all* friction. You install the package and you are done. There is no logging code to write. But it is aimed just at unhandled exceptions. – quentin-starin Jan 15 '12 at 10:49
  • @qes - sounds good! Perhaps I should take a look :) Bit worried that you said "it will log nothing else" though - does that mean you can't log arbitrary debug messages? I find that really useful. Maybe there should be a wiki page or something to discuss this? Not sure if that is appropriate – kmp Jan 15 '12 at 10:51
  • Well you can log other messages to it, but it really is designed mainly to log exceptions in a web app - as a result of that narrow focus, it does its job very well. But it's not a general logging framework like NLog or log4net. If you need to log things that are not exceptions or need to log outside of a web app, ELMAH is probably not the right choice for that. I work in some solutions that spread across web and desktop and use NLog across the board, but still use ELMAH for all the web parts. – quentin-starin Jan 15 '12 at 11:08
3

Use ELMAH, it is available on NuGet. It will log unhandled exceptions in a web app and provide an interface to view them. It can log to a variety of storage mechanisms including xml and sql databases, it can also email you errors. It is basically the solution for logging exceptions in ASP.Net applications (both WebForms and MVC).

If you are using ELMAH in an MVC app, there are some integration points that should be addressed. These can be handled pretty well in one shot by installing the ELMAH.Mvc package from NuGet.

Community
  • 1
  • 1
quentin-starin
  • 26,121
  • 7
  • 68
  • 86
  • Sounds great! I will try this! I looked at the Alexander Beletsky article and top most he writes : Update : Elmah.MVC is now released as NuGet package. No need to read that long blog post, just install it. Is he refering the the article or another blog post? Do I need to follow his instructions or is it just to install the package? – Banshee Jan 15 '12 at 10:59
  • Also, pleas not that my site will be running at a webhost so I do not got complete control over the server. I will not be able to install extra software on to the server besides my website. Am I still able to use ELMAH? – Banshee Jan 15 '12 at 11:01
  • Yes you can, you do not need to install anything on the server. If you are not familiar with it, NuGet is just an easy way to reference dependencies while building your project. You can download ELMAH and reference it yourself. In either case, you simply deploy the ELMAH.dll with your web project - nothing to install on the server (if you want to log to SQL Server you will of course need access to that). – quentin-starin Jan 15 '12 at 11:10