登录应用程序的发布版本(C#) [英] Logging in Release Build of Application (C#)

查看:116
本文介绍了登录应用程序的发布版本(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在外部研究和StackOverflow/Progammers.StackExchange方面,我一直很难找到有关该主题的信息,所以我想在这里问.这主要是一个最佳实践" 问题,但我也对性能影响感兴趣.

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. This it mostly a 'best practice' question, but I am interested in the performance repercussions as well.

背景:
我目前正在具有支持Windows Service组件的WinForms应用程序中使用NLog日志记录框架.我在整个应用程序中有很多跟踪日志语句,用于测试,并通过UDP输出进行监视.错误/警告也转到事件日志和滚动日志文件,其中还包括INFO级别.

Background:
I currently am using the NLog logging framework in a WinForms application with a supporting Windows Service component. I have a lot of tracing log statements throughout the application for my testing which I monitor via UDP output. Errors/Warnings also go to the Event Log and rolling log files, which also include the INFO level as well.

问题:
编译应用程序的发行版时,处理所有日志记录代码的最佳实践是什么?是否应该从应用程序中删除所有跟踪/调试代码,因为它们可能会对性能产生负面影响?由于NLog日志记录由XML配置文件控制,因此删除跟踪/调试日志记录的侦听器是否可以消除潜在的性能陷阱?我是否应该一直在预处理器指令中放置跟踪/调试日志记录元素,以防止它们被编译为可发布的生产代码?

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? 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?

您是对的,日志记录会对性能产生负面影响.多少取决于how often you log and the logging output.您应该将日志记录限制在关键内容上,以帮助诊断某些内容或对问题/异常进行故障排除.

You are right, logging affects performance negatively. How much depends on 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.

由于NLog日志记录由XML配置文件控制,因此 删除跟踪/调试日志记录的侦听器,消除了潜在的可能 性能陷阱?

Since NLog logging is controlled by XML configuration files, would removing the listeners for trace/debug logging eliminate the potential performance pitfalls?

是的.如果将日志级别设置为Off,则可以提高性能.例如,因为不再有IO操作.唯一的花费就是检查日志框架中的日志级别.

Yes. If you set the log level to Off then you gain performance. Because there are, for example, no IO oprations any more. 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.

这篇关于登录应用程序的发布版本(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆