Note that there are some explanatory texts on larger screens.

plurals
  1. POC# conditional logging/tracing
    primarykey
    data
    text
    <p>I want to add logging or tracing to my C# application but I don't want the overhead of formatting the string or calculating values to be logged if the log verbosity level is set so low that the message will not be logged.</p> <p>In C++, you can use the preprocessor to define macros that will prevent code from being executed at all like this:</p> <pre><code>#define VLOG(level,expr) if (level &gt;= g_log.verbosity) { g_log.output &lt;&lt; expr; } </code></pre> <p>Used like this:</p> <pre><code>VLOG(5,"Expensive function call returns " &lt;&lt; ExpensiveFunctionCall()); </code></pre> <p>How do you do that in C#?</p> <p>I've read the Microsoft docs explaining the Trace and Debug facilities <a href="http://msdn.microsoft.com/en-us/library/64yxa344(VS.80).aspx" rel="noreferrer">here</a>, and they claim that using #undef DEBUG and #undef TRACE removes all tracing and debugging code from the produced executable, but does it really remove the whole call? Meaning, if I write </p> <pre><code>System.Diagnostics.Trace.WriteLineIf(g_log.verbosity&gt;=5,ExpensiveFunctionCall()); </code></pre> <p>it won't call my expensive function if I undefine TRACE? Or does make the call, <strong>then</strong> decide it won't trace anything?</p> <p>Anyway, even if it does remove it, this is inferior to the C++ macro because I can't make that big ugly call look like my simple VLOG() call in C++ and still avoid evaluating parameters, can I? Nor can I avoid the overhead by defining the verbosity lower at runtime like I can in C++, right?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload