Click here to Skip to main content
15,897,371 members
Articles / Mobile Apps

Introduction to NLog

Rate me:
Please Sign up or sign in to vote.
4.94/5 (101 votes)
13 Jul 200617 min read 903.2K   11K   281  
Introduction to managing diagnostic traces with NLog.
<?xml version="1.0" encoding="windows-1250" ?>
<?xml-stylesheet type="text/xsl" href="style.xsl" ?>
<content lang="en" id="documentation" subid="netapi">
    <h1>.NET API</h1>
    <p>
        NLog uses a simple logging API which is similar to the one provided by <a href="http://logging.apache.org/log4net">log4net</a>.
        There are 2 classes that you interact with: <code>LogManager</code> and <code>Logger</code>. 
        Note that <code>Logger</code> a concrete class not an interface (like ILog from log4net).
    </p>
    <h5>Log Manager</h5>
    <p>
        LogManager contains methods and properties to create loggers and manage logging configuration.
        Click on each method to see the API documentation:
    </p>
    <ul>
        <li><a href="help/NLog.LogManager.GetLogger.html">LogManager.GetLogger</a> - gets or creates the specified logger</li>
        <li><a href="help/NLog.LogManager.GetCurrentClassLogger.html">LogManager.GetCurrentClassLogger</a> - gets or creates the logger for currently executing class</li>
        <li><a href="help/NLog.LogManager.Configuration.html">LogManager.Configuration</a> - gets or sets the current logging configuration information</li>
        <li><a href="help/NLog.LogManager.GlobalThreshold.html">LogManager.GlobalThreshold</a> - gets or sets the global logging threshold</li>
    </ul>
    <p>Full list of members of the LogManager class can be found <a href="help/NLog.LogManagerMembers.html">here</a>.</p>
    <p>
        It's recommended to use <a href="help/NLog.LogManager.GetLogger.html">LogManager.GetLogger("loggerName")</a>
        to create a logger for each class, store it in a static field and use for logging. We recommend having a consistently-named
        variable that holds logger instance for each class. <code>logger</code> might not be a bad option for the logger
        name.
    </p>
    <p>
        The following example shows the code that uses the recommended way of creating and keeping logger instance.
    </p>
    <cs src="examples/getlogger.cs" />
    <p>
        It's also possible to use or 
        <a href="help/NLog.LogManager.GetCurrentClassLogger.html">LogManager.GetCurrentClassLogger()</a>
        but this feature isn't supported in Compact Framework configuration, so if you want
        to support mobile devices you shouldn't use this syntax. GetCurrentClassLogger is also quite
        costly because internally it uses the <code>StackTrace</code> class to get the name of the
        current class.
    </p>
    <cs src="examples/getcurrentclasslogger.cs" />
    <h5>Logger</h5>
    <p>
        The <a href="help/NLog.Logger.html">NLog.Logger</a> class has the following methods. Each method has a number
        of overloads designed to minimize the number of memory allocations in order to improve logging speed. 
        Click on a method name to see the list of overloads for it.
    </p>
    <ul>
        <li><a href="help/NLog.Logger.Log_overloads.html">Log()</a> - Writes the diagnostic message at the specified level using the specified format provider and format parameters. </li>
        <li><a href="help/NLog.Logger.Trace_overloads.html">Trace()</a> - Writes the diagnostic message at the Trace level using the specified format provider and format parameters. </li>
        <li><a href="help/NLog.Logger.Debug_overloads.html">Debug()</a> - Writes the diagnostic message at the Debug level using the specified format provider and format parameters. </li>
        <li><a href="help/NLog.Logger.Info_overloads.html">Info()</a> - Writes the diagnostic message at the Info level using the specified format provider and format parameters. </li>
        <li><a href="help/NLog.Logger.Warn_overloads.html">Warn()</a> - Writes the diagnostic message at the Warn level using the specified format provider and format parameters. </li>
        <li><a href="help/NLog.Logger.Error_overloads.html">Error()</a> - Writes the diagnostic message at the Error level using the specified format provider and format parameters. </li>
        <li><a href="help/NLog.Logger.Fatal_overloads.html">Fatal()</a> - Writes the diagnostic message at the Fatal level using the specified format provider and format parameters. </li>
    </ul>
    <p>
        The following methods and properties let you determine whether logging is enabled for the specified level:
    </p>
    <ul>
        <li><a href="help/NLog.Logger.IsEnabled.html">IsEnabled()</a> - Determines if logging is enabled for the specified level.</li>
        <li><a href="help/NLog.Logger.IsTraceEnabled.html">IsTraceEnabled</a> - Determines if logging is enabled for the Trace level.</li>
        <li><a href="help/NLog.Logger.IsDebugEnabled.html">IsDebugEnabled</a> - Determines if logging is enabled for the Debug level.</li>
        <li><a href="help/NLog.Logger.IsInfoEnabled.html">IsInfoEnabled</a> - Determines if logging is enabled for the Info level.</li>
        <li><a href="help/NLog.Logger.IsWarnEnabled.html">IsWarnEnabled</a> - Determines if logging is enabled for the Warn level.</li>
        <li><a href="help/NLog.Logger.IsErrorEnabled.html">IsErrorEnabled</a> - Determines if logging is enabled for the Error level.</li>
        <li><a href="help/NLog.Logger.IsFatalEnabled.html">IsFatalEnabled</a> - Determines if logging is enabled for the Fatal level.</li>
    </ul>
    <p>Full list of members of the Logger class can be found <a href="help/NLog.LoggerMembers.html">here</a>.</p>
</content>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions