WebService Target


Calls the specified web service on each logging message.

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

Remarks:

The web service must implement a method that accepts a number of string parameters.

Parameters (blue fields are required):

NameTypeDescription
namestring
The name of the target.
methodNamestring
Web service method name.
namespacestring
Web service namespace.
protocolWebServiceProtocol
The protocol to be used when calling web service.

Default value is: Soap11.

Possible values are:

  • Soap11 - SOAP 1.1
  • Soap12 - SOAP 1.2
  • HttpPost - HTTP POST
  • HttpGet - HTTP GET

urlstring
Web service URL.
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 autoReload="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <targets> 
    <target name="ws" xsi:type="WebService" 
            namespace="http://www.nlog-project.org/example" protocol="Soap11" 
            methodName="HelloWorld" url="http://localhost:2648/Service1.asmx"> 
      <parameter name="n1" type="System.String" layout="${message}"/> 
      <parameter name="n2" type="System.String" layout="${logger}"/> 
      <parameter name="n3" type="System.String" layout="${level}"/> 
    </target> 
  </targets> 
  <rules> 
    <logger name="*" writeTo="ws"/> 
  </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 NLog; 
using NLog.Targets; 
 
class Example 
{ 
    static void Main(string[] args) 
    { 
        WebServiceTarget target = new WebServiceTarget(); 
        target.Url = "http://localhost:2648/Service1.asmx"; 
        target.MethodName = "HelloWorld"; 
        target.Namespace = "http://www.nlog-project.org/example"; 
        target.Protocol = WebServiceTarget.WebServiceProtocol.Soap11; 
 
        target.Parameters.Add(new MethodCallParameter("n1", "${message}")); 
        target.Parameters.Add(new MethodCallParameter("n2", "${logger}")); 
        target.Parameters.Add(new MethodCallParameter("n3", "${level}")); 
 
        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug); 
 
        Logger logger = LogManager.GetLogger("Example"); 
        logger.Trace("log message 1"); 
        logger.Debug("log message 2"); 
        logger.Info("log message 3"); 
        logger.Warn("log message 4"); 
        logger.Error("log message 5"); 
        logger.Fatal("log message 6"); 
    } 
}

The example web service that works with this example is shown below

using System; 
using System.Data; 
using System.Web; 
using System.Collections; 
using System.Web.Services; 
using System.Web.Services.Protocols; 
using System.ComponentModel; 
 
namespace WebService1 
{ 
    [WebService(Namespace = "http://www.nlog-project.org/example")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [ToolboxItem(false)] 
    public class Service1 : System.Web.Services.WebService 
    { 
        [WebMethod] 
        public void HelloWorld(string n1, string n2, string n3) 
        { 
            HttpContext.Current.Trace.Write("n1 " + n1); 
            HttpContext.Current.Trace.Write("n2 " + n2); 
            HttpContext.Current.Trace.Write("n3 " + n3); 
        } 
    } 
}

Back to the target list.