13

I want to start logging when items get removed from my asp.net cache and i am trying to figure out where and how to do this.

i see that ELMAH is recommended for application wide exception handling logging system but can (and should) it also be used for regular logging instrumentation like the example above?

leora
  • 188,729
  • 360
  • 878
  • 1,366

2 Answers2

9

I think that use you use both ELMAH and Log4Net (or NLog). Using both will get you the most flexibility. I wish I could go into extreme details right now, but I wanted to at least respond to you and let you know that using BOTH are the best solution from what I have experienced, and what I have read others have done. Please let me know if you need explanations etc..

Tom Stickel
  • 19,633
  • 6
  • 111
  • 113
5

We had been a purely Log4Net shop until Log4net stopped logging for us in production on numerous occasions. To circumvent this from happening we decided to implement ELMAH for our entire application (even for basic logging). We could not be happier with the results as all of our log statements appear in one spot and there are no more text files to traverse (unless we wanted to traverse the XML files). We kept Log4Net around for one more iteration, but finally found it to be of little use and have completely removed it from our application.

I can see why you might want to have two different logging frameworks for a site, especially since ELMAH requires an HttpContext in order to work to its full potential. However, we did find a way to pass the exception to ELMAH without an HttpContext:

        /// <summary>
        /// Main logger method that will log an exception and/or a message
        /// </summary>
        /// <param name="exception">Exception to log</param>
        /// <param name="message">Message to log</param>
        private static void Log(Exception exception, string message)
        {
                Error elmahError = exception != null ? new Error(exception) : new Error();

                if (!string.IsNullOrWhiteSpace(message))
                {
                    elmahError.Message = message;
                }

                Elmah.ErrorLog.GetDefault(null).Log(elmahError);             
        }

Then in your web.config you just need to specify the applicationName:

<elmah>
    <security allowRemoteAccess="1" />
    <errorLog name="ELMAHLogger" applicationName="xxx" type="Elmah.XmlFileErrorLog, Elmah" logPath="~/ELMAH_Production_Logs" />
  </elmah>

Again, I think Log4Net is a fine logging framework and it worked for us for a number of years. I find ELMAH to be easier to use when looking for an exception and the above code has allowed us to use it anywhere in our application where the HttpContext might not be available.

amurra
  • 15,221
  • 4
  • 70
  • 87
  • I was curious so I found a good response to using both here: [Using both ELMAH and Log4Net](http://stackoverflow.com/questions/800009/using-log4net-and-where-to-implement-it-and-usign-with-elmah/800045#800045) – Tom Stickel Jul 12 '11 at 06:44
  • @Tom - Yeah I know you can use both and I did for awhile, but my response was just saying you don't necessarily have to and that you can use ELMAH for all your logging needs. – amurra Jul 12 '11 at 11:57
  • 1
    Per the link I used "They're used for two different purposes." Besides that ELMAH is only for websites, while some of us like to use Log4Net for Thick client applications as well. To get rusty on a "staple" solid product like Log4Net is not what I prefer. "Log4net on the other hand is for logging events (including errors caused by exceptions) that you kind of knew could happen (otherwise you would not have a try / catch block)." I like using ELMAH and Log4Net in MVC applications. But sure, software development is more of an "Art" and not a "Science" – Tom Stickel Jul 12 '11 at 18:57