In code …

It’s really easy to add logging to any application that uses our .net network library. Under the hood we are using NLog and it can be enabled as follows:

If you just want to log to a console window add this C# code snippet wherever you are also enabling the library:

LoggingConfiguration logConfig = new LoggingConfiguration();
ConsoleTarget consoleTarget = new ConsoleTarget();
consoleTarget.Layout = "${date:format=HH\\:mm\\:ss} [${threadid} - ${level}] - ${message}";
logConfig.AddTarget("console", consoleTarget);
logConfig.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, consoleTarget));
NetworkComms.EnableLogging(logConfig);

Note: These code snippets will require the following namespace references:

using NLog;
using NLog.Config;
using NLog.Targets;

If you also want to log to the console and a text file (log.txt) then use this C# code snippet instead:

LoggingConfiguration logConfig = new LoggingConfiguration();
FileTarget fileTarget = new FileTarget();
fileTarget.FileName = "${basedir}/log.txt";
fileTarget.Layout = "${date:format=HH\\:mm\\:ss} [${threadid} - ${level}] - ${message}";
ConsoleTarget consoleTarget = new ConsoleTarget();
consoleTarget.Layout = "${date:format=HH\\:mm\\:ss} [${threadid} - ${level}] - ${message}";
logConfig.AddTarget("file", fileTarget);
logConfig.AddTarget("console", consoleTarget);

logConfig.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, fileTarget));
logConfig.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, consoleTarget));
NetworkComms.EnableLogging(logConfig);

You can also add entries to the log from anywhere in your own code as follows:

//Add a Trace level log message
NetworkComms.Logger.Trace("my log message");

//Add a Debug level log message
NetworkComms.Logger.Debug("my log message");

//Add a Info level log message
NetworkComms.Logger.Info("my log message");

//Add a Warn level log message
NetworkComms.Logger.Warn("my log message");

//Add a Fatal level log message
NetworkComms.Logger.Fatal("my log message");

If you want to disable logging at any point use:

NetworkComms.DisableLogging();

Using an external logging configuration …

It is also possible to specify the logging configuration in an external file. An external logging configuration file may look as follows, named nlog.config, for more information please see here:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>
        <target name="logfile" xsi:type="File" fileName="file.txt" />
    </targets>

    <rules>
        <logger name="*" minlevel="Info" writeTo="logfile" />
    </rules>
</nlog>

In your own code, should this file exist you can enable logging using it’s contents as follows:

string logFileName = "nlog.config";
if(System.IO.File.Exists(logFileName))
    NetworkComms.EnableLogging(new NLog.Config.XmlLoggingConfiguration(logFileName));

The advantage of this syntax is that you don’t need to rebuild your code should your logging requirements change.