MethodCall Target Calls the specified static method on each logging message and passes contextual parameters to it. | Assembly | Class | .NET Framework | .NET CF | Mono on Windows | Mono on Unix |
|---|
| 1.0 | 1.1 | 2.0 | 1.0 | 2.0 | 1.0 | 2.0 | 1.0 | 2.0 |
|---|
| NLog.dll | NLog.Targets.MethodCallTarget |  |  |  |  |  |  |  |  |  |
Parameters (blue fields are required):| Name | Type | Description |
|---|
| name | string | | | className | string | | | methodName | string | | The method name. The method must be public and static. |
| | Parameters |
Collection of
MethodCallParameter. Each element is represented as <parameter/>
| | Name | Type | Description |
|---|
| name | string | | The name of the parameter. |
| | type | string | | The type of the parameter. |
| | layout | string | | 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. |