Asp.Net 中的 Log4Net 正确实现

Log4Net in Asp.Net Correct Implementation

我正在尝试在 asp.net 中实现 Log4Net 文件附加程序。我已经成功地实施了它。但是我不确定实现它的正确架构。

我可以在每个页面添加一个记录器并记录信息。但是,我正在考虑集中记录器 class。可以实现单例模式。但我想知道如果对同一页面的请求来自两个不同的浏览器会发生什么。我可以实现 Thread Static 然后每个页面而不是初始化他们自己的记录器将使用这个集中记录器 class 来记录。

我假设log4net file appender 或rolling file appender 使用队列机制写入日志文件。因为只能获取文件的一个句柄来写入文件。

任何人都可以在这方面帮助我。我走的路是否正确,否则当有来自不同浏览器的数十个和数百个请求时,我会遇到问题。

我建议不要使用单例,而是为每个控制器和您要从中记录的 class 使用一个记录器。记录器的创建成本很低 cached by log4net - and even more so if you declare them as static within the class - and by having one per class you can change logging per class or namespace by changing the log4net configuration at runtime - say to enable some debug logging to aid diagnosing a problem in production, or to turn down some logging which is unexpectedly noisy. You can do this without recycling your app if you have your log config in a separate file.

此外,如果您要使用文件日志记录,请确保使用 MinimalLock