MethodCallTarget Class |
Namespace: NLog.Targets
The MethodCallTarget type exposes the following members.
Name | Description | |
---|---|---|
MethodCallTarget |
Initializes a new instance of the MethodCallTarget class.
| |
MethodCallTarget(String) |
Initializes a new instance of the MethodCallTarget class.
| |
MethodCallTarget(String, ActionLogEventInfo, Object) |
Initializes a new instance of the MethodCallTarget class.
|
Name | Description | |
---|---|---|
ClassName |
Gets or sets the class name.
| |
MethodName |
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.
| |
Name |
Gets or sets the name of the target.
(Inherited from Target.) | |
Parameters |
Gets the array of parameters to be passed.
(Inherited from MethodCallTargetBase.) |
Name | Description | |
---|---|---|
Dispose |
Closes the target.
(Inherited from Target.) | |
Flush |
Flush any pending log messages (in case of asynchronous targets).
(Inherited from Target.) | |
PrecalculateVolatileLayouts |
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.) | |
ToString | Returns a string that represents the current object. (Inherited from Target.) | |
WriteAsyncLogEvent |
Writes the log to the target.
(Inherited from Target.) | |
WriteAsyncLogEvents(AsyncLogEventInfo) |
Writes the array of log events.
(Inherited from Target.) | |
WriteAsyncLogEvents(IListAsyncLogEventInfo) |
Writes the array of log events.
(Inherited from Target.) |
To set up the target in the configuration file, use the following syntax:
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:
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}