简体   繁体   English

登录应用程序的发布版本(C#)

[英]Logging in Release Build of Application (C#)

I've been having a hard time finding information on this subject in both outside research and on StackOverflow/Progammers.StackExchange so I figured I'd ask here. 在外部研究和StackOverflow/Progammers.StackExchange ,我一直很难找到有关此主题的信息,所以我想在这里问。 This it mostly a 'best practice' question, but I am interested in the performance repercussions as well. 这主要是一个“最佳实践”问题,但我也对性能影响感兴趣。

Background: 背景:
I currently am using the NLog logging framework in a WinForms application with a supporting Windows Service component. 我目前在具有支持Windows Service组件的WinForms应用程序中使用NLog日志记录框架。 I have a lot of tracing log statements throughout the application for my testing which I monitor via UDP output. 我在整个应用程序中有很多跟踪日志语句,用于测试,并通过UDP输出进行监视。 Errors/Warnings also go to the Event Log and rolling log files, which also include the INFO level as well. 错误/警告也转到事件日志和滚动日志文件,其中还包括INFO级别。

Question: 题:
What is the best practice to handle all of the logging code when compiling the release version of the application? 编译应用程序的发行版时,处理所有日志记录代码的最佳实践是什么? Should all trace/debug code be removed from the application since it could negatively affect performance? 是否应该从应用程序中删除所有跟踪/调试代码,因为它们可能会对性能产生负面影响? Since NLog logging is controlled by XML configuration files, would removing the listeners for trace/debug logging eliminate the potential performance pitfalls? 由于NLog日志记录由XML配置文件控制,因此删除跟踪/调试日志记录的侦听器是否可以消除潜在的性能陷阱? Should I have been placing trace/debug logging elements within preprocessor directives to prevent them being compiled into releasable production code? 我是否应该一直在预处理器指令中放置跟踪/调试日志记录元素,以防止将它们编译为可发布的生产代码?

What is the best practice to handle all of the logging code when compiling the release version of the application? 编译应用程序的发行版时,处理所有日志记录代码的最佳实践是什么?

You can leave your logging calls like it is and control the behavior with the log levels. 您可以按原样保留日志记录调用,并通过日志级别控制行为。

Should all trace/debug code be removed from the application since it could negatively affect performance? 是否应该从应用程序中删除所有跟踪/调试代码,因为它们可能会对性能产生负面影响?

You are right, logging affects performance negatively. 没错,日志记录会对性能产生负面影响。 How much depends on how often you log and the logging output . 多少取决于how often you log and the logging output You should limit your logging to critical stuff, that helps in diagnose certain things or troubleshooting a problem/exceptions. 您应该将日志记录限制在关键内容上,这有助于诊断某些内容或对问题/异常进行故障排除。

Since NLog logging is controlled by XML configuration files, would removing the listeners for trace/debug logging eliminate the potential performance pitfalls? 由于NLog日志记录由XML配置文件控制,因此删除跟踪/调试日志记录的侦听器是否可以消除潜在的性能陷阱?

Yes. 是。 If you set the log level to Off then you gain performance. 如果将日志级别设置为“ Off则可以提高性能。 Because there are, for example, no IO oprations any more. 例如,因为不再有IO操作。 The only cost is the check for loglevels within the logging framework. 唯一的成本是检查日志框架内的日志级别。

Should I have been placing trace/debug logging elements within preprocessor directives to prevent them being compiled into releasable production code? 我是否应该一直在预处理器指令中放置跟踪/调试日志记录元素,以防止将它们编译为可发布的生产代码?

This would improve performance again. 这将再次提高性能。 But than you are unable get any diagnostic information in production for troubleshooting a problem. 但是,您将无法在生产中获得任何诊断信息来解决问题。 This would be possible when you work with the log levels. 当您使用日志级别时,这将是可能的。

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

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