Last updated: 2006-09-18

This website is based on NLog v1.0. Click here to view the documentation for other versions.

MethodCall Target


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

AssemblyClass.NET Framework.NET CFMono on WindowsMono on Unix
1.01.12.01.02.01.02.01.02.0
NLog.dllNLog.Targets.MethodCallTarget

Parameters (blue fields are required):

NameTypeDescription
namestring
The name of the target.
classNamestring
The class name.
methodNamestring
The method name. The method must be public and static.
Parameters Collection of MethodCallParameter. Each element is represented as <parameter/>
NameTypeDescription
namestring
The name of the parameter.
typestring
The type of the parameter.
layoutstring
The layout that should be use to calcuate the value for the parameter.

Example:

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

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <targets> 
    <target name="m" xsi:type="MethodCall" className="Example, MethodCall" 
            methodName="LogMethod"> 
      <parameter layout="${level}"/> 
      <parameter layout="${message}"/> 
    </target> 
  </targets> 
  <rules> 
    <logger name="*" minlevel="Debug" writeTo="msgbox"/> 
  </rules> 
</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:

using System; 
 
using NLog; 
using NLog.Targets; 
using System.Diagnostics; 
 
public class Example 
{ 
    public static void LogMethod(string level, string message) 
    { 
        Console.WriteLine("l: {0} m: {1}", level, message); 
    } 
    static void Main(string[] args) 
    { 
        MethodCallTarget target = new MethodCallTarget(); 
        target.ClassName = typeof(Example).AssemblyQualifiedName; 
        target.MethodName = "LogMethod"; 
        target.Parameters.Add(new MethodCallParameter("${level}")); 
        target.Parameters.Add(new MethodCallParameter("${message}")); 
 
        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug); 
 
        Logger logger = LogManager.GetLogger("Example"); 
        logger.Debug("log message"); 
        logger.Error("error message"); 
    } 
}

Back to the target list.
Webwww.nlog-project.org