简体   繁体   English

最小化生产中的日志语句

[英]Minimizing log statements in production

I have a C# console application project which uses log4net as the logging library. 我有一个C#控制台应用程序项目,它使用log4net作为日志库。 Throughout the application there are logging statements ( log.Debug() , log.Error() , etc.), which are printed to the console as the program runs. 整个应用程序中都有日志语句( log.Debug()log.Error()等),它们在程序运行时打印到控制台。

What is the best way to change the logging statements in a production environment, to minimize time of execution caused by logging? 在生产环境中更改日志记录语句的最佳方法是什么,以最大限度地减少日志记录导致的执行时间? I would like some of the statements to not print out at all, and for some logging statements to only be printed out during production. 我希望一些语句根本不打印出来,并且一些日志语句只能在生产过程中打印出来。

I am thinking of adding new setting in the Web.config file which determines how the logging changes. 我正在考虑在Web.config文件中添加新设置,该文件确定日志记录的更改方式。 However I think I'd also need to override the log.Debug() and log.Error() methods to work with the new setting, but am unsure how to do this. 但是我想我还需要覆盖log.Debug()log.Error()方法来处理新设置,但我不确定如何做到这一点。 Can anyone advise? 任何人都可以建议吗?

You should probably have abstracted away your logging library from your application, that way you would have control over your logging behaviour, and make your project loosely coupled to your log4net dependency. 您应该从应用程序中抽象出日志库,这样您就可以控制日志记录行为,并使您的项目松散地耦合到您的log4net依赖项。

Its probably not the short one-line answer you was hoping for, but i would highly recommend using this design pattern, since it will save you lots of time down the line. 它可能不是你希望的简短的单行答案,但我强烈建议使用这种设计模式,因为它可以节省你很多时间。 Heres a brief article that shows the concept 下面是一篇简短的文章,展示了这个概念

There is no setting in the Web.config file which can tell log4net to do things differently depending on your build. Web.config文件中没有设置可以告诉log4net根据您的构建做不同的事情。

However, you can have multiple Web.config files and deploy the one you want depending on the build or deployment environment. 但是,您可以拥有多个Web.config文件,并根据构建或部署环境部署所需的文件。 The following links might help: 以下链接可能会有所帮助:

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

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