编译器指令 - 建议 - 仅在调试模式下运行代码

发布于 2024-11-18 02:56:43 字数 778 浏览 2 评论 0原文

仅当应用程序在调试模式下运行时,我才需要记录消息。我找到了两种方法:

第一种:需要日志记录时,需要到处写3行。但是,Logger 语句仅在编译时被禁用,这正是我所需要的。 Logger.Log根本不会被执行。

#if DEV_ENV
        Logger.Log("Application started !"); // This line is grayed. Perfect !
#endif

public static void Log(string message)
{
    Debug.WriteLine(message);
}

第二:非常整洁。需要记录的地方只需一行代码。不确定Logger.Log语句是否被执行。 如果仅在编译时删除函数调用(与第一种方法相同。但是,现在确定代码行没有变灰),我想继续这样做。

Logger.Log("Application started !"); // This line is not grayed out. But, function is not called. So, confused whether its removed at compile time.

[Conditional("DEV_ENV")]
public static void Log(string message)
{
    Debug.WriteLine(message);
}

我担心性能差异。

I need to Log messages only when application is running in debug mode. I have found 2 ways:

First: Need to write 3 lines everywhere when logging is needed. But, Logger statement is disabled at compile time only which is exactly I need. Logger.Log will not be executed at all.

#if DEV_ENV
        Logger.Log("Application started !"); // This line is grayed. Perfect !
#endif

public static void Log(string message)
{
    Debug.WriteLine(message);
}

Second: Very neat. Only one line of code wherever logging is required. Not sure, whether Logger.Log statement is executed or not. If function call is removed at compile time only (same as first approach. But, now sure as line of code is not greyed out), I want to go with this.

Logger.Log("Application started !"); // This line is not grayed out. But, function is not called. So, confused whether its removed at compile time.

[Conditional("DEV_ENV")]
public static void Log(string message)
{
    Debug.WriteLine(message);
}

I am concerned about the performance differences.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦巷 2024-11-25 02:56:43

MSDN 页面获取 ConditionalAttribute

将 ConditionalAttribute 应用于
方法向编译器表明a
不应该调用该方法
编译为 Microsoft 中间体
语言 (MSIL) 除非有条件
关联的编译符号
定义了 ConditionalAttribute。


因此,正如它所说,方法调用在编译时被删除,与#if相同。

From the MSDN page for the ConditionalAttribute:

Applying ConditionalAttribute to a
method indicates to compilers that a
call to the method should not be
compiled
into Microsoft intermediate
language (MSIL) unless the conditional
compilation symbol that is associated
with ConditionalAttribute is defined.

So, as it says, the method call is removed at compile time, same as the #if.

当梦初醒 2024-11-25 02:56:43

根据您的编译设置,您可以使用:

if (System.Diagnostics.Debugger.IsAttached)
   Logger.Log("Application started !"); 

或者,

#if DEBUG
    Logger.Log("Application started !"); 
#endif 

Depending on your compile settings, you could use:

if (System.Diagnostics.Debugger.IsAttached)
   Logger.Log("Application started !"); 

or,

#if DEBUG
    Logger.Log("Application started !"); 
#endif 
淡看悲欢离合 2024-11-25 02:56:43

正如 George 指出的那样,如果应用了 Conditional 属性,则不会编译方法调用。这也意味着(与直接使用 #If DEV_ENV 删除代码一样),方法调用中包含的任何副作用也不会发生 - 与往常一样,有关记录代码中存在副作用的警告是很好的成立:

public static void Main(String[] args)
{
    int i = 92;
    Log(string.Format("{0} became {1}", i++, i));
    Console.WriteLine(i);
    Console.ReadLine();
}

[Conditional("SKIP")]
private static void Log(string msg)
{
    Console.WriteLine(msg);
}

如果未定义SKIP,则此代码将打印92。如果定义了SKIP,则会打印92变为9393

As George points out, the method call will not be compiled if the Conditional attribute is applied. This also means (as with removing the code directly using #If DEV_ENV) that any side effects included in the method call will also not occur - as always, the warning about having side effects in logging code are well founded:

public static void Main(String[] args)
{
    int i = 92;
    Log(string.Format("{0} became {1}", i++, i));
    Console.WriteLine(i);
    Console.ReadLine();
}

[Conditional("SKIP")]
private static void Log(string msg)
{
    Console.WriteLine(msg);
}

If SKIP is not defined, this code prints out 92. If SKIP is defined, it prints 92 became 93 and 93.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文