0

I am trying to log error using log4net in asp.net MVC5 c#.

For test purpose, I have configured log4net in application_start and initialize logger and log error from home controller. For some reason, log4net only logs error for the first time when I start the application. When I navigate away from homecontroller and come back to home controller again, no error is logged. I have put my logging code in HomeController index. When debugged using breakpoint, the logging code is reachable but the log is not recorded.

Am I missing anything?

Here is my log4net configuration.

  1. Configured log4net in Global.asax Application_Start() as

    log4net.Config.XmlConfigurator.Configure();
    
  2. In my home controller index (For Testing), I am logging error as

    var logger = log4net.LogManager.GetLogger(this.GetType());
    Random rnd = new Random();
    int rndint = rnd.Next(52);
    
    logger.Error(rndint.ToString());
    
  3. My config file is using standard log4net AdoNetAppender with buffer size=100 and level is set to WARN

1 Answer 1

1

AdoNetAppender batches log statements that are inserted into the database. If you set the buffer size to 1 you will see the log statements are added to the table immediately.

The best thing to do is use a buffer size of 1 for local development, and use a larger value when deployed to production if performance is a concern and lots of logging is used.

Sign up to request clarification or add additional context in comments.

1 Comment

That was it.Thanks a lot. I was banging my head all this morning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.