Skip to content
Rolf Kristensen edited this page Dec 2, 2023 · 15 revisions

Mock target for unit-testing. If looking for output to System.Diagnostics.Debug then use DebugSystem-target.

Platforms Supported: All

Configuration Syntax

<targets>
  <target xsi:type="Debug" name="String" layout="Layout" />
</targets>

Read more about using the Configuration File.

Parameters

General Options

  • name - Name of the target.

Layout Options

  • layout - Layout used to format log messages. Layout Required. Default: ${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}

Examples

Logging to Debug Target

(snippet from Debug Simple Example.cs )

        NLog.Targets.DebugTarget target = new NLog.Targets.DebugTarget() { Name = "Debug" };
        target.Layout = "${message}";

        NLog.Config.LoggingConfiguration config = new NLog.Config.LoggingConfiguration();
        config.AddRuleForAllLevels(target)
        NLog.LogManager.Configuration = config;

        NLog.Logger logger = NLog.LogManager.GetLogger("Example");
        logger.Debug("log message");
        logger.Debug("another log message");

        Console.WriteLine("The debug target has been hit {0} times.", target.Counter);
        Console.WriteLine("The last message was '{0}'.", target.LastMessage);

Some examples of DebugTarget use can be found in unit tests

Clone this wiki locally