[This is preliminary documentation and is subject to change.]

Calls the specified static method on each log message and passes contextual parameters to it.

Namespace:  NLog.Targets
Assembly:  NLog (in NLog.dll) Version: 2.0.1.0

Syntax

C#
public sealed class MethodCallTarget : MethodCallTargetBase
Visual Basic (Declaration)
Public NotInheritable Class MethodCallTarget _
	Inherits MethodCallTargetBase
Visual Basic (Usage)
Dim instance As MethodCallTarget

Examples

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

CopyXML
 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>

This assumes just one target and a single rule. More configuration options are described here.

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

CopyC#
 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}

Inheritance Hierarchy

Object
  NLog.Targets..::.Target
    NLog.Targets..::.MethodCallTargetBase
      NLog.Targets..::.MethodCallTarget

See Also