<p>I'm looking for the best way to log errors in an ASP.NET application.
I want to be able to receive emails when errors occurs in my application, with detailed information about the Exception and the current Request.</p>

<p>In my company we used to have our own ErrorMailer, catching everything in the Global.asax Application_Error. It was "Ok" but not very flexible nor configurable.</p>

<p>We switched recently to NLog. It's much more configurable, we can define different targets for the errors, filter them, buffer them (not tried yet). It's a very good improvement.</p>

<p>But I discovered lately that there's a whole Namespace in the .Net framework for this purpose : <a href="http://msdn.microsoft.com/en-us/library/system.web.management.aspx" rel="noreferrer">System.Web.Management</a> and it can be configured in the <a href="http://msdn.microsoft.com/en-us/library/2fwh2ss9(VS.80).aspx" rel="noreferrer">healthMonitoring</a> section of web.config.</p>

<p>Have you ever worked with .Net health monitoring? What is your solution for error logging?</p>

[{'answer_id': 11961, 'author': 'brendan', 'author_id': 225, 'author_profile': 'https://Stackoverflow.com/users/225', 'pm_score': 0, 'selected': False, 'text': '<p>We use a custom homegrown logging util we wrote.  It requires you to implement logging on your own everywhere you need it.  But, it also allows you to capture a lot more than just the exception.</p>\n\n<p>For example our code would look like this:</p>\n\n<pre><code>Try\n  Dim p as New Person()\n  p.Name = "Joe"\n  p.Age = 30\nCatch ex as Exception\n  Log.LogException(ex,"Err creating person and assigning name/age")\n  Throw ex\nEnd Try\n</code></pre>\n\n<p>This way our logger will write all the info we need to a SQL database.  We have email alerts set up at the DB level to look for certain errors or frequently occurring errors.  It helps us identify exactly where the errors are coming from.</p>\n\n<p>This might not be exactly what you\'re looking for.  Another approach similar to using Global.asax is to us a code injection technique like <a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" rel="nofollow noreferrer">AOP</a> with <a href="http://www.postsharp.org/" rel="nofollow noreferrer">PostSharp</a>.  This allows you to inject custom code at the beginning and end of every method or on every exception.  It\'s an interesting approach but I believe it may have a heavy performance overhead.</p>\n'}, {'answer_id': 11965, 'author': 'Brandon Wood', 'author_id': 423, 'author_profile': 'https://Stackoverflow.com/users/423', 'pm_score': 0, 'selected': False, 'text': '<p>My team uses <a href="http://logging.apache.org/log4net/index.html" rel="nofollow noreferrer">log4net</a> from Apache.  It\'s pretty lightweight and easy to setup.  Best of all, it\'s completely configurable from the web.config file, so once you\'ve got the hooks in your code setup, you can completely change the way logging is done just by changing the web.config file.  </p>\n\n<p>log4net supports logging to a wide variety of locations - database, email, text file, Windows event log, etc.  My team has it configured to send detailed error information to a database, and also send an email to the entire team with enough information for us to determine in which part of the code the error originated.  Then we know who is responsible for that piece of code, and they can go to the database to get more detailed information.</p>\n'}, {'answer_id': 11967, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 6, 'selected': True, 'text': '<p>I use <a href="http://code.google.com/p/elmah/" rel="noreferrer">elmah</a>.  It has some really nice features and here is a <a href="http://www.codeproject.com/KB/aspnet/ELMAHDemo.aspx" rel="noreferrer">CodeProject</a> article on it.  I think the StackOverflow team uses elmah also!</p>\n'}, {'answer_id': 12011, 'author': 'Brian Childress', 'author_id': 721, 'author_profile': 'https://Stackoverflow.com/users/721', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve been using the Enterprise Library\'s Logging objects. It allows you to have different types of logging (flat file, e-mail, and/or database). It\'s pretty customizable and has a pretty good interface for updating your web.config for the configuration of the logging. Usually I call my logging from the On Error in the Global.asax.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/cc511708.aspx" rel="nofollow noreferrer">Here\'s a link to the MSDN</a></p>\n'}, {'answer_id': 12025, 'author': 'Mun', 'author_id': 775, 'author_profile': 'https://Stackoverflow.com/users/775', 'pm_score': 3, 'selected': False, 'text': '<p>I\'ve been using <a href="http://logging.apache.org/log4net/index.html" rel="nofollow noreferrer">Log4net</a>, configured to email details of fatal errors.  It\'s also set up to log everything to a log file, which is invaluable when trying to debug problems.  The other benefit is that if that standard functionality doesn\'t do what you want it to, it\'s fairly easy to write a custom appender which can process the logging information as required.</p>\n\n<p>Having said that, I\'m using this in tandem with a custom error handler which sends out a html email with a bit more information than is included in the standard log4net emails - page, session variables, cookies, http server variables, etc.</p>\n\n<p>These are both wired up in the Application_OnError event, where the exception is logged as a fatal exception in log4net (which then causes it to be emailed to a specified email address), and also handled using the custom error handler.</p>\n\n<p>First heard about <a href="http://code.google.com/p/elmah/" rel="nofollow noreferrer">Elmah</a> from the Coding Horror blog entry, <a href="http://www.codinghorror.com/blog/archives/001118.html" rel="nofollow noreferrer">Crash Responsibly</a>, and although it looks promising I\'m yet to implement it any projects.</p>\n'}, {'answer_id': 64871, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>I recently built an asp.net webservice with NLog, which I use for all my desktop apps.  The logging works fine when I'm debugging in Visual Studio, but as soon as I switch to IIS the log file isn't created; I've not yet determined why, but it the fact that I need to look for a solution makes me want to try something else for my asp.net needs!</p>\n"}, {'answer_id': 472402, 'author': 'Aaron Powell', 'author_id': 11388, 'author_profile': 'https://Stackoverflow.com/users/11388', 'pm_score': 2, 'selected': False, 'text': "<p>I use log4net and where ever I <em>expect</em> an exception I log it to the appropriate level. I tend not to re-throw the exception because it doesn't really allow for as-nice user experience, there is less info you can provide at the current state.</p>\n\n<p>I'll have Application_Error also configured to catch any exception which was not expected and the error is logged as a Fatal priority through log4net (well, 404's are detected and logged as Info as they aren't that high severity).</p>\n"}, {'answer_id': 31906549, 'author': 'matt-dot-net', 'author_id': 380518, 'author_profile': 'https://Stackoverflow.com/users/380518', 'pm_score': 0, 'selected': False, 'text': '<p>We use EnterpriseLibrary.ExceptionHandling.Logging.  I like it a bit better than log4net because not only do we control the logging completely, but we can control the Throw/NoThrow decision within config as well.</p>\n'}]
['https://Stackoverflow.com/questions/11950', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1130/']