Click or drag to resize

WebServiceTarget Class

Calls the specified web service on each log message.
Inheritance Hierarchy
SystemObject
  NLog.TargetsTarget
    NLog.TargetsMethodCallTargetBase
      NLog.TargetsWebServiceTarget

Namespace:  NLog.Targets
Assembly:  NLog (in NLog.dll) Version: 5.3.4+73d83d3161d31288ca5c554cc50d27b6bed5f28b
Syntax
public sealed class WebServiceTarget : MethodCallTargetBase

The WebServiceTarget type exposes the following members.

Constructors
  NameDescription
Public methodWebServiceTarget
Initializes a new instance of the WebServiceTarget class.
Public methodWebServiceTarget(String)
Initializes a new instance of the WebServiceTarget class.
Top
Properties
  NameDescription
Public propertyEncoding
Gets or sets the encoding.
Public propertyEscapeDataRfc3986
Gets or sets a value whether escaping be done according to Rfc3986 (Supports Internationalized Resource Identifiers - IRIs)
Public propertyHeaders
Gets the array of parameters to be passed.
Public propertyIncludeBOM
Should we include the BOM (Byte-order-mark) for UTF? Influences the Encoding property. This will only work for UTF-8.
Public propertyMethodName
Gets or sets the Web service method name. Only used with Soap.
Public propertyName
Gets or sets the name of the target.
(Inherited from Target.)
Public propertyNamespace
Gets or sets the Web service namespace. Only used with Soap.
Public propertyParameters
Gets the array of parameters to be passed.
(Inherited from MethodCallTargetBase.)
Public propertyPreAuthenticate
Indicates whether to pre-authenticate the HttpWebRequest (Requires 'Authorization' in Headers parameters)
Public propertyProtocol
Gets or sets the protocol to be used when calling web service.
Public propertyProxyAddress
Gets or sets the custom proxy address, include port separated by a colon
Public propertyProxyType
Gets or sets the proxy configuration when calling web service
Public propertyUrl
Gets or sets the web service URL.
Public propertyUserAgent
Gets or sets the value of the User-agent HTTP header.
Public propertyXmlRoot
Gets or sets the name of the root XML element, if POST of XML document chosen. If so, this property must not be null. (see Protocol and XmlPost).
Public propertyXmlRootNamespace
Gets or sets the (optional) root namespace of the XML document, if POST of XML document chosen. (see Protocol and XmlPost).
Top
Methods
  NameDescription
Public methodDispose
Closes the target.
(Inherited from Target.)
Public methodFlush
Flush any pending log messages (in case of asynchronous targets).
(Inherited from Target.)
Public methodPrecalculateVolatileLayouts
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.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Target.)
Public methodWriteAsyncLogEvent
Writes the log to the target.
(Inherited from Target.)
Public methodWriteAsyncLogEvents(AsyncLogEventInfo)
Writes the array of log events.
(Inherited from Target.)
Public methodWriteAsyncLogEvents(IListAsyncLogEventInfo)
Writes the array of log events.
(Inherited from Target.)
Top
Remarks
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:

XML
 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" parameterType="System.String" layout="${message}"/>
 8            <parameter name="n2" parameterType="System.String" layout="${logger}"/>
 9            <parameter name="n3" parameterType="System.String" layout="${level}"/>
10        </target>
11    </targets>
12
13    <rules>
14        <logger name="*" writeTo="ws" />
15    </rules>
16</nlog>

To set up the log target programmatically use code like this:

C#
 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

C#
 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}
See Also