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

Calls the specified web service on each log message.

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

Syntax

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

Remarks

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

Examples

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

CopyXML
 1<?xml version="1.0" ?>
 2<nlog autoReload="true"
 3      xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
 4      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 5    <targets>
 6        <target name="ws" xsi:type="WebService" namespace="http://www.nlog-project.org/example" protocol="Soap11" methodName="HelloWorld" url="http://localhost:2648/Service1.asmx">
 7            <parameter name="n1" type="System.String" layout="${message}"/>
 8            <parameter name="n2" type="System.String" layout="${logger}"/>
 9            <parameter name="n3" type="System.String" layout="${level}"/>
10        </target>
11    </targets>
12
13    <rules>
14        <logger name="*" writeTo="ws" />
15    </rules>
16</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 NLog;
 2using NLog.Targets;
 3
 4class Example
 5{
 6    static void Main(string[] args)
 7    {
 8        WebServiceTarget target = new WebServiceTarget();
 9        target.Url = "http://localhost:2648/Service1.asmx";
10        target.MethodName = "HelloWorld";
11        target.Namespace = "http://www.nlog-project.org/example";
12        target.Protocol = WebServiceTarget.WebServiceProtocol.Soap11;
13
14        target.Parameters.Add(new MethodCallParameter("n1", "${message}"));
15        target.Parameters.Add(new MethodCallParameter("n2", "${logger}"));
16        target.Parameters.Add(new MethodCallParameter("n3", "${level}"));
17
18        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
19
20        Logger logger = LogManager.GetLogger("Example");
21        logger.Trace("log message 1");
22        logger.Debug("log message 2");
23        logger.Info("log message 3");
24        logger.Warn("log message 4");
25        logger.Error("log message 5");
26        logger.Fatal("log message 6");
27    }
28}

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

CopyC#
 1using System;
 2using System.Data;
 3using System.Web;
 4using System.Collections;
 5using System.Web.Services;
 6using System.Web.Services.Protocols;
 7using System.ComponentModel;
 8
 9namespace WebService1
10{
11    [WebService(Namespace = "http://www.nlog-project.org/example")]
12    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
13    [ToolboxItem(false)]
14    public class Service1 : System.Web.Services.WebService
15    {
16        [WebMethod]
17        public void HelloWorld(string n1, string n2, string n3)
18        {
19            HttpContext.Current.Trace.Write("n1 " + n1);
20            HttpContext.Current.Trace.Write("n2 " + n2);
21            HttpContext.Current.Trace.Write("n3 " + n3);
22        }
23    }
24}

Inheritance Hierarchy

Object
  NLog.Targets..::.Target
    NLog.Targets..::.MethodCallTargetBase
      NLog.Targets..::.WebServiceTarget

See Also