I try to find a way to set isdebugenabled false or true in configuration file,
so I can turn it on or off whenever I need. Thanks !
NLog is a . Net Library that enables you to add high-quality logs for your application. NLog can also be downloaded using Nugget in Visual Studio. Targets are used to display, store, or pass log messages to another destination.
nuget.org/packages/NLog/… NLog 4.7 has been released! See news post: nlog-project.org/2020/03/28/nlo… While older versions of NLog works well on ASP.NET Core 3, we have optimized the NLog.
Using a really simple logging setup
<targets>
<target xsi:type="File"
name="debug"
fileName="c:\temp\debug.txt"
layout="${longdate} ${uppercase:${level}} ${message}"
/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="debug" />
</rules>
And an equally simple code sample
class Program
{
static void Main(string[] args)
{
var logger = new NLog.LogFactory().GetCurrentClassLogger();
if (logger.IsDebugEnabled)
{
logger.Debug("this is a debug message");
}
logger.Debug("this is another debug message");
}
}
When minLevel
is set to Debug or Trace, both logger.Debug
statements will write to the log. If you raise minLevel
to a higher level (Info, Warn, Off) neither statement will be written to the log. logger.Debug
checks IsDebugEnabled
(which is inferred from the log level).
You can certainly get a performance increase (in cases where you are going to be logging calculated values and not just strings) by checking IsDebugEnabled
, and changing minLevel
for the logger is the way to toggle this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With