Click or drag to resize

TraceTarget Class

Sends log messages through System.Diagnostics.Trace.
Inheritance Hierarchy
SystemObject
  NLog.TargetsTarget
    NLog.TargetsTargetWithLayout
      NLog.TargetsTargetWithLayoutHeaderAndFooter
        NLog.TargetsTraceTarget

Namespace:  NLog.Targets
Assembly:  NLog (in NLog.dll) Version: 5.5.0+1873145cbc26e0312d9481234d69ce7dc68aac4e
Syntax
public sealed class TraceTarget : TargetWithLayoutHeaderAndFooter

The TraceTarget type exposes the following members.

Constructors
  NameDescription
Public methodTraceTarget
Initializes a new instance of the TraceTarget class.
Public methodTraceTarget(String)
Initializes a new instance of the TraceTarget class.
Top
Properties
  NameDescription
Public propertyEnableTraceFail
Forward Fatal to Fail(String) (Instead of TraceError(String))
Public propertyFooter
Gets or sets the footer.
(Inherited from TargetWithLayoutHeaderAndFooter.)
Public propertyHeader
Gets or sets the header.
(Inherited from TargetWithLayoutHeaderAndFooter.)
Public propertyLayout
Gets or sets the text to be rendered.
(Inherited from TargetWithLayoutHeaderAndFooter.)
Public propertyName
Gets or sets the name of the target.
(Inherited from Target.)
Public propertyRawWrite
Force use WriteLine(String) independent of LogLevel
Top
Methods
  NameDescription
Public methodDispose
Closes the target.
(Inherited from Target.)
Public methodFlush
Flush any pending log messages (in case of asynchronous targets).
(Inherited from Target.)
Public methodPrecalculateVolatileLayouts
Calls the Precalculate(LogEventInfo) on each volatile layout used by this target. This method won't prerender if all layouts in this target are thread-agnostic.
(Inherited from Target.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Target.)
Public methodWriteAsyncLogEvent
Writes the log to the target.
(Inherited from Target.)
Public methodWriteAsyncLogEvents(AsyncLogEventInfo)
Writes the array of log events.
(Inherited from Target.)
Public methodWriteAsyncLogEvents(IListAsyncLogEventInfo)
Writes the array of log events.
(Inherited from Target.)
Top
Remarks
Examples

To set up the target in the configuration file, use the following syntax:

XML
 1<?xml version="1.0" ?>
 2<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
 3      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 4    <targets>
 5        <target name="n" xsi:type="Trace" layout="${message}" />
 6    </targets>
 7
 8    <rules>
 9        <logger name="*" minlevel="Debug" writeTo="n" />
10    </rules>
11</nlog>

To set up the log target programmatically use code like this:

C#
 1using System;
 2
 3using NLog;
 4using NLog.Config;
 5using NLog.Targets;
 6using System.Diagnostics;
 7
 8class Example
 9{
10    static void Main(string[] args)
11    {
12        Trace.Listeners.Add(new ConsoleTraceListener());
13
14        TraceTarget target = new TraceTarget();
15        target.Layout = "${message}";
16
17        LoggingConfiguration nlogConfig = new LoggingConfiguration();
18        nlogConfig.AddRuleForAllLevels(target);
19        LogManager.Configuration = nlogConfig;
20
21        Logger logger = LogManager.GetLogger("Example");
22        logger.Debug("log message");
23    }
24}
See Also