简体   繁体   English

Log4Net记录错误仅一次

[英]Log4Net logging error just once

I am trying to log error using log4net in asp.net MVC5 c#. 我正在尝试在asp.net MVC5 c#中使用log4net记录错误。

For test purpose, I have configured log4net in application_start and initialize logger and log error from home controller. 为了进行测试,我在application_start中配置了log4net,并初始化了家庭控制器的记录器和日志错误。 For some reason, log4net only logs error for the first time when I start the application. 由于某种原因,log4net仅在我启动应用程序时第一次记录错误。 When I navigate away from homecontroller and come back to home controller again, no error is logged. 当我离开homecontroller导航并再次回到home controller时,没有错误记录。 I have put my logging code in HomeController index. 我已将我的日志记录代码放入HomeController索引中。 When debugged using breakpoint, the logging code is reachable but the log is not recorded. 使用断点调试时,可以访问日志记录代码,但不记录日志。

Am I missing anything? 我有什么想念的吗?

Here is my log4net configuration. 这是我的log4net配置。

  1. Configured log4net in Global.asax Application_Start() as 在Global.asax Application_Start()中将log4net配置为

     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 我的配置文件使用标准log4net AdoNetAppender ,缓冲区大小为100,级别设置为WARN

AdoNetAppender batches log statements that are inserted into the database. AdoNetAppender批处理插入数据库的日志语句。 If you set the buffer size to 1 you will see the log statements are added to the table immediately. 如果将缓冲区大小设置为1,您将看到日志语句立即添加到表中。

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. 最好的做法是将缓冲区大小设置为1进行本地开发,如果需要考虑性能并使用大量日志记录,则在部署到生产环境时使用较大的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM