Click here to Skip to main content
15,891,253 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 899.3K   11K   281  
Introduction to managing diagnostic traces with NLog.
using NLog;
using NLog.Targets;
using NLog.Targets.Wrappers;
using System.Threading;

class Example
{
    static void Main(string[] args)
    {
        FileTarget target = new FileTarget();
        target.Layout = "${longdate} ${logger} ${message}";
        target.FileName = "${basedir}/logs/logfile.${level}.txt";
        // where to store the archive files
        target.ArchiveFileName = "${basedir}/archives/${level}/log.{#####}.txt";
        target.ArchiveEvery = FileTarget.ArchiveEveryMode.Minute;
        target.ArchiveNumbering = FileTarget.ArchiveNumberingMode.Rolling;
        target.MaxArchiveFiles = 3;
        target.ArchiveAboveSize = 10000;

        // this speeds up things when no other processes are writing to the file
        target.ConcurrentWrites = true;

        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);

        Logger logger = LogManager.GetLogger("Example");

        // generate a large number of messages, sleeping 1/10 of second between writes
        // to observe time-based archiving which occurs every minute
        // the volume is high enough to cause ArchiveAboveSize to be triggered
        // so that log files larger than 10000 bytes are archived as well

        // in this version, a single File target keeps track of 3 sets of log and 
        // archive files, one for each level

        // you get:
        //      logs/logfile.Debug.txt
        //      logs/logfile.Error.txt
        //      logs/logfile.Fatal.txt
        //
        // and your archives go to:
        //
        //      archives/Debug/log.00000.txt
        //      archives/Debug/log.00001.txt
        //      archives/Debug/log.00002.txt
        //      archives/Debug/log.00003.txt
        //      archives/Error/log.00000.txt
        //      archives/Error/log.00001.txt
        //      archives/Error/log.00002.txt
        //      archives/Error/log.00003.txt
        //      archives/Fatal/log.00000.txt
        //      archives/Fatal/log.00001.txt
        //      archives/Fatal/log.00002.txt
        //      archives/Fatal/log.00003.txt

        for (int i = 0; i < 2500; ++i)
        {
            logger.Debug("log message {i}", i);
            logger.Error("log message {i}", i);
            logger.Fatal("log message {i}", i);
            Thread.Sleep(100);
        }
    }
}

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