leora
leora

Reputation: 196439

Where should you do regular logging in asp.net-mvc ? Should you use ELMAH?

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?

Upvotes: 13

Views: 1317

Answers (2)

amurra
amurra

Reputation: 15401

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.

Upvotes: 5

Tom Stickel
Tom Stickel

Reputation: 20401

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..

Upvotes: 9

Related Questions