有没有办法在 C# 运行时分配条件方法或添加预处理器指令?
目的是能够在运行时从生产构建上的数据库切换调试调用......
The aim is to be able to switch debugging calls on at run-time from database on a production build ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不; 条件方法和预处理器指令的要点是它们会导致编译器省略最终可执行文件中的代码。 这些的运行时等效项是
if
语句。但是,面向方面的编程大致相当于您所要求的,因为它允许您将代码注入到正在运行的程序中否则就不会在那里。
编辑:正如 Joe 提到的,实现此目的的方法是针对 log4net 等日志记录框架进行编程,该框架允许对记录的内容进行细粒度控制。
No; the point of conditional methods and preprocessor directives is that they cause the compiler to omit code from the final executable. The runtime equivalent of these is an
if
statement.However, aspect-orientated programming is roughly equivalent to what you're asking, in that it allows you to inject code into a running program that wouldn't otherwise be there.
Edit: as Joe mentioned, the way to do this is to program against a logging framework like log4net that allows fine-grained control over what gets logged.
不在预处理器级别。 毕竟,您正在运行该过程的结果。 当然,您可以将预处理器检查更改为正常的代码检查,这显然可以根据需要进行切换。
Not at the pre-processor level. After all, you're running the result of that process. Of course, you could change your pre-processor checks to be normal code checks, which can obviously be switched as required.
不,但大多数日志记录子系统都提供这种能力。 例如,使用 log4net,您可以动态更改日志记录级别,或者如果使用 System.Diagnostics.Trace,您可以更改 TraceSwitch 动态级别。
No, but most logging subsystems provide this ability. E.g. with log4net you can change the logging level dynamically, or if using System.Diagnostics.Trace you can change the TraceSwitch level dynamically.
显然,您不能影响二进制代码(如使用 #if 语句)。
我通常做的是确保我的代码包含大量日志记录,这些日志记录由 跟踪开关。 这样,您就可以让系统在生产环境中运行,而很少或没有日志记录,当您想研究某些问题时,您可以通过更改配置文件来打开日志记录。
You obviously can't affect the binary code as such (as with the #if statement).
What I usually do is to make sure that my code contains a lot of logging, that is activated by trace switches. That way you can have a system run in production with little or no logging, and when you want to research some problem you can switch logging on by altering the config file.