Click or drag to resize

MethodCallTarget Class

Calls the specified static method on each log message and passes contextual parameters to it.
Inheritance Hierarchy
SystemObject
  NLog.TargetsTarget
    NLog.TargetsMethodCallTargetBase
      NLog.TargetsMethodCallTarget

Namespace:  NLog.Targets
Assembly:  NLog (in NLog.dll) Version: 5.3.4+73d83d3161d31288ca5c554cc50d27b6bed5f28b
Syntax
public sealed class MethodCallTarget : MethodCallTargetBase

The MethodCallTarget type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyClassName
Gets or sets the class name.
Public propertyMethodName
Gets or sets the method name. The method must be public and static. Use the AssemblyQualifiedName , https://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname(v=vs.110).aspx e.g.
Public propertyName
Gets or sets the name of the target.
(Inherited from Target.)
Public propertyParameters
Gets the array of parameters to be passed.
(Inherited from MethodCallTargetBase.)
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="m" xsi:type="MethodCall" className="Example, MethodCall" methodName="LogMethod">
 6            <parameter layout="${level}" />
 7            <parameter layout="${message}" />
 8        </target>
 9    </targets>
10
11    <rules>
12        <logger name="*" minlevel="Debug" writeTo="msgbox" />
13    </rules>
14</nlog>

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

C#
 1using System;
 2
 3using NLog;
 4using NLog.Targets;
 5using System.Diagnostics;
 6
 7public class Example
 8{
 9    public static void LogMethod(string level, string message)
10    {
11        Console.WriteLine("l: {0} m: {1}", level, message);
12    }
13    static void Main(string[] args)
14    {
15        MethodCallTarget target = new MethodCallTarget();
16        target.ClassName = typeof(Example).AssemblyQualifiedName;
17        target.MethodName = "LogMethod";
18        target.Parameters.Add(new MethodCallParameter("${level}"));
19        target.Parameters.Add(new MethodCallParameter("${message}"));
20
21        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
22
23        Logger logger = LogManager.GetLogger("Example");
24        logger.Debug("log message");
25        logger.Error("error message");
26    }
27}
See Also