Click here to Skip to main content
15,885,757 members
Articles / Programming Languages / C#

Logging with NLog

Rate me:
Please Sign up or sign in to vote.
1.68/5 (13 votes)
6 Jan 20073 min read 52.2K   69   20  
The article describes how to configure nlog to log reports as per your requirement
<html><head><META http-equiv="Content-Type" content="text/html; charset=utf-8"><link rel="stylesheet" href="style.css" type="text/css"><meta name="generator" content="NAnt 0.85 style task"><meta name="keywords" content="NLog logging tracing debugging library easy simple C# .NET log4net log4j Logger C/C++ COM"><title>NLog - Logging API</title></head><body width="100%"><div class="titleimage" style="overflow: hidden"><img src="NLog.jpg"></div><table class="page" cellpadding="0" cellspacing="0"><tr><td valign="top" class="controls" rowspan="2"><table border="0" cellpadding="0" cellspacing="0"><tr><td class="nav"><a class="nav" href="index.html">Introduction</a></td></tr><tr><td class="nav"><a class="nav" href="news.html">News</a></td></tr><tr><td class="nav"><a class="nav" href="tutorial.html">Tutorial</a></td></tr><tr><td class="nav"><a class="nav" href="benchmark.html">Benchmark</a></td></tr><tr><td class="nav"><a class="nav" href="howto.html">How To...</a></td></tr><tr><td class="nav_selected"><a class="nav_selected" href="api.html">Logging API</a><table class="submenu" width="100%"><tr><td><a class="subnav" href="netapi.html">.NET API</a></td></tr><tr><td><a class="subnav" href="comapi.html">COM API</a></td></tr><tr><td><a class="subnav_selected">C/C++ API</a></td></tr></table></td></tr><tr><td class="nav"><a class="nav" href="reference.html">Reference</a></td></tr><tr><td class="nav"><a class="nav" href="download.html">Download</a></td></tr><tr><td class="nav"><a class="nav" href="technical.html">Technical Information</a></td></tr><tr><td class="nav"><a class="nav" href="mailinglists.html">Mailing Lists</a></td></tr><tr><td class="nav"><a class="nav" href="faq.html">FAQ</a></td></tr><tr><td class="nav"><a class="nav" href="blog.html">Blog</a></td></tr><tr><td class="nav"><a class="nav" href="links.html">Links</a></td></tr></table><p></p><p style="border: 1px solid #c0c0c0; background-color: white; padding: 4px; font-size: 13px; color: red; font-weight: bold">Important information for users of NLog versions 0.2 and 0.5! Click <a href="releasenotes.html">here</a>.</p><a href="http://www.cenqua.com/clover.net"><img src="http://www.cenqua.com/images/cloverednet1.gif" width="89" height="33" border="0" alt="Code Coverage by Clover.NET"></a><p></p><script type="text/javascript" language="javascript">
var sc_project=575077; 
var sc_partition=4; 
var sc_security="6fe22c9a"; 
</script><script type="text/javascript" language="javascript" src="http://www.statcounter.com/counter/counter.js"></script><noscript><a href="http://www.statcounter.com/" target="_blank"><img src="http://c5.statcounter.com/counter.php?sc_project=575077&amp;java=0&amp;security=6fe22c9a" alt="website tracking" border="0"></a></noscript><br><a href="http://sourceforge.net"><img src="http://sourceforge.net/sflogo.php?group_id=116456&amp;type=1" width="88" height="31" border="0" alt="SourceForge.net Logo"></a></td><td valign="top" align="left" class="content"><h1>C/C++ API</h1><p>
        NLog supports a C/C++ logging API that can be used in scenarios that require the use 
        of C/C++ components in .NET projects. Typical case is porting a legacy systen to .NET
        where some parts are rewritten in managed code while some remain unmanaged.
    </p><p>
        This API is available through <code>NLogC.dll</code> which is a mixed-mode (managed/unmanaged) 
        assembly. It provides the following set exported functions:
    </p><pre class="csharp-example"><pre xml:space="preserve"><span class="csharp">
<span class="c">// </span>
<span class="c">// ANSI functions</span>
<span class="c">// </span>
<span class="k">int</span> NLog_ConfigureFromFileA(<span class="k">const</span> <span class="k">char</span> *fileName);
<span class="k">void</span> NLog_LogA(NLogLevel level, <span class="k">const</span> <span class="k">char</span> *loggerName, <span class="k">const</span> <span class="k">char</span> *logMessage, ...); 
<span class="k">void</span> NLog_DebugA(<span class="k">const</span> <span class="k">char</span> *loggerName, <span class="k">const</span> <span class="k">char</span> *logMessage, ...); 
<span class="k">void</span> NLog_InfoA(<span class="k">const</span> <span class="k">char</span> *loggerName, <span class="k">const</span> <span class="k">char</span> *logMessage, ...); 
<span class="k">void</span> NLog_WarnA(<span class="k">const</span> <span class="k">char</span> *loggerName, <span class="k">const</span> <span class="k">char</span> *logMessage, ...); 
<span class="k">void</span> NLog_ErrorA(<span class="k">const</span> <span class="k">char</span> *loggerName, <span class="k">const</span> <span class="k">char</span> *logMessage, ...); 
<span class="k">void</span> NLog_FatalA(<span class="k">const</span> <span class="k">char</span> *loggerName, <span class="k">const</span> <span class="k">char</span> *logMessage, ...); 
<span class="k">void</span> NLog_LogVA(NLogLevel level, <span class="k">const</span> <span class="k">char</span> *loggerName, <span class="k">const</span> <span class="k">char</span> *logMessage, va_list args);

<span class="c">// </span>
<span class="c">// Unicode functions</span>
<span class="c">// </span>
<span class="k">int</span> NLog_ConfigureFromFileW(<span class="k">const</span> wchar_t *fileName);
<span class="k">void</span> NLog_LogW(NLogLevel level, <span class="k">const</span> wchar_t *loggerName, <span class="k">const</span> wchar_t *logMessage, ...); 
<span class="k">void</span> NLog_DebugW(<span class="k">const</span> wchar_t *loggerName, <span class="k">const</span> wchar_t *logMessage, ...); 
<span class="k">void</span> NLog_InfoW(<span class="k">const</span> wchar_t *loggerName, <span class="k">const</span> wchar_t *logMessage, ...); 
<span class="k">void</span> NLog_WarnW(<span class="k">const</span> wchar_t *loggerName, <span class="k">const</span> wchar_t *logMessage, ...); 
<span class="k">void</span> NLog_ErrorW(<span class="k">const</span> wchar_t *loggerName, <span class="k">const</span> wchar_t *logMessage, ...); 
<span class="k">void</span> NLog_FatalW(<span class="k">const</span> wchar_t *loggerName, <span class="k">const</span> wchar_t *logMessage, ...); 
<span class="k">void</span> NLog_LogVW(NLogLevel level, <span class="k">const</span> wchar_t *loggerName, <span class="k">const</span> wchar_t *logMessage, va_list args);
</span>
</pre></pre><p>
        For your convenience, the following defines which use a <code>TCHAR</code> data type are available:
    </p><pre class="csharp-example"><pre xml:space="preserve"><span class="csharp"><span class="c">//</span>
<span class="c">// TCHAR macros</span>
<span class="c">// </span>
#ifdef UNICODE

#define NLog_Log NLog_LogW
#define NLog_LogV NLog_LogVW
#define NLog_Debug NLog_DebugW
#define NLog_Info NLog_InfoW
#define NLog_Warn NLog_WarnW
#define NLog_Error NLog_ErrorW
#define NLog_Fatal NLog_FatalW
#define NLog_ConfigureFromFile NLog_ConfigureFromFileW

#<span class="k">else</span>

#define NLog_Log NLog_LogA
#define NLog_LogV NLog_LogVA
#define NLog_Debug NLog_DebugA
#define NLog_Info NLog_InfoA
#define NLog_Warn NLog_WarnA
#define NLog_Error NLog_ErrorA
#define NLog_Fatal NLog_FatalA
#define NLog_ConfigureFromFile NLog_ConfigureFromFileA

#endif
</span>
</pre></pre><p>
        Here's the short summary of what these functions do (modulo their ANSI/UNICODE variants):
    </p><ul><li><code>NLog_ConfigureFromFile(filename)</code></li><p>
            Configures NLog from the specified config file. You should call this function at the beginning of your
            program if you want to specify your own config file. If you don't do this, NLog will attempt to find
            the config file the first time you use any of the logging functions using the logic described <a href="config.html">here</a>.
        </p><li><code>NLog_Log(level,logger,logMessage,...)</code></li><p>
            Writes the specified <code>logMessage</code> at the specified <code>level</code> to the specified <code>logger</code>.
            <code>level</code> is a NLogLevel enumeration value an can be one of the following constants:
            <code>logMessage</code> is a message to be written. It may include <code>printf()</code>-style parameters.
        </p><ul><li>NLOG_DEBUG</li><li>NLOG_INFO</li><li>NLOG_WARN</li><li>NLOG_ERROR</li><li>NLOG_FATAL</li></ul><p></p><li><code>NLog_LogV(level,logger,logMessage,parameters)</code></li><p>
            Same as <code>NLog_Log()</code> but lets you pass message parameters as <code>va_list</code> (useful for wrapping 
            in your own logging layer).
        </p><li><code>NLog_Debug(logger,logMessage,...)</code></li><p>
            Equivalent to calling <code>NLog_Log(NLOG_DEBUG,logger,logMessage,...)</code></p><li><code>NLog_Info(logger,logMessage,...)</code></li><p>
            Equivalent to calling <code>NLog_Log(NLOG_INFO,logger,logMessage,...)</code></p><li><code>NLog_Warn(logger,logMessage,...)</code></li><p>
            Equivalent to calling <code>NLog_Log(NLOG_WARN,logger,logMessage,...)</code></p><li><code>NLog_Error(logger,logMessage,...)</code></li><p>
            Equivalent to calling <code>NLog_Log(NLOG_ERROR,logger,logMessage,...)</code></p><li><code>NLog_Fatal(logger,logMessage,...)</code></li><p>
            Equivalent to calling <code>NLog_Log(NLOG_FATAL,logger,logMessage,...)</code></p></ul><p>
        The C/C++ API as-is is meant primarily to be wrapped by your own high-level logging API (every programmer has one, hasn't he?).
        An example of such an API implemented as C++ class is provided in <code>src/NLogC/NLogger.h</code>.
    </p></td></tr><tr><td class="copyright">Copyright © 2003-2005 by Jarosław Kowalski</td></tr></table></body></html>

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
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions