blob: 23250983d03a329e9a44229ef405dd175a5252d6 [file] [log] [blame]
<html dir="LTR">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252" />
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5" />
<title>IsDebugEnabled Property</title>
<xml>
</xml>
<link rel="stylesheet" type="text/css" href="MSDN.css" />
</head>
<body id="bodyID" class="dtBODY">
<div id="nsbanner">
<div id="bannerrow1">
<table class="bannerparthead" cellspacing="0">
<tr id="hdr">
<td class="runninghead">Apache log4net� SDK Documentation - Microsoft .NET Framework 4.0</td>
<td class="product">
</td>
</tr>
</table>
</div>
<div id="TitleRow">
<h1 class="dtH1">ILog.IsDebugEnabled Property</h1>
</div>
</div>
<div id="nstext">
<p> Checks if this logger is enabled for the <a href="log4net.Core.Level.Debug.html">Debug</a> level. </p>
<div class="syntax">
<span class="lang">[Visual�Basic]</span>
<br />Property�IsDebugEnabled�As�<a href="ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfSystemBooleanClassTopic.htm">Boolean</a></div>
<div class="syntax">
<span class="lang">[C#]</span>
<br />
<a href="ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfSystemBooleanClassTopic.htm">bool</a>�IsDebugEnabled�{get;}</div>
<p>
</p>
<h4 class="dtH4">Property Value</h4>
<p>
<code>true</code> if this logger is enabled for <a href="log4net.Core.Level.Debug.html">Debug</a> events, <code>false</code> otherwise. </p>
<h4 class="dtH4">Remarks</h4>
<p> This function is intended to lessen the computational cost of disabled log debug statements. </p>
<p> For some ILog interface <code>log</code>, when you write:</p>
<pre class="code"><span class="lang">[C#]
</span>log.Debug("This is entry number: " + i );
</pre>
<p> You incur the cost constructing the message, string construction and concatenation in this case, regardless of whether the message is logged or not. </p>
<p> If you are worried about speed (who isn't), then you should write: </p>
<pre class="code"><span class="lang">[C#]
</span>if (log.IsDebugEnabled)
{
log.Debug("This is entry number: " + i );
}
</pre>
<p> This way you will not incur the cost of parameter construction if debugging is disabled for <code>log</code>. On the other hand, if the <code>log</code> is debug enabled, you will incur the cost of evaluating whether the logger is debug enabled twice. Once in <b>IsDebugEnabled</b> and once in the <b>Debug</b>. This is an insignificant overhead since evaluating a logger takes about 1% of the time it takes to actually log. This is the preferred style of logging. </p>
<p>Alternatively if your logger is available statically then the is debug enabled state can be stored in a static variable like this: </p>
<pre class="code"><span class="lang">[C#]
</span>private static readonly bool isDebugEnabled = log.IsDebugEnabled;
</pre>
<p> Then when you come to log you can write: </p>
<pre class="code"><span class="lang">[C#]
</span>if (isDebugEnabled)
{
log.Debug("This is entry number: " + i );
}
</pre>
<p> This way the debug enabled state is only queried once when the class is loaded. Using a <code>private static readonly</code> variable is the most efficient because it is a run time constant and can be heavily optimized by the JIT compiler. </p>
<p> Of course if you use a static readonly variable to hold the enabled state of the logger then you cannot change the enabled state at runtime to vary the logging that is produced. You have to decide if you need absolute speed or runtime flexibility. </p>
<h4 class="dtH4">See Also</h4><p><a href="log4net.ILog.html">ILog Interface</a> | <a href="log4net.html">log4net Namespace</a> | <b>Debug</b> | <b>DebugFormat</b></p><object type="application/x-oleobject" classid="clsid:1e2a7bd0-dab9-11d0-b93a-00c04fc99f9e" viewastext="true" style="display: none;"><param name="Keyword" value="IsDebugEnabled property"></param><param name="Keyword" value="IsDebugEnabled property, ILog interface"></param><param name="Keyword" value="ILog.IsDebugEnabled property"></param></object><hr /><div id="footer"><a href='http://logging.apache.org/log4net/'>Copyright 2004-2013 The Apache Software Foundation.</a><br></br>Apache log4net, Apache and log4net are trademarks of The Apache Software Foundation.</div></div>
</body>
</html>