Skip to content
Rolf Kristensen edited this page Dec 20, 2022 · 40 revisions

Calls the specified web service on each log message.

Platforms Supported: All

Configuration Syntax

<targets>
  <target xsi:type="WebService"
          name="String"
          url="System.Uri"
          encoding="Encoding"
          includeBOM="Nullable boolean"
          protocol="Enum"
          namespace="String"
          methodName="String"
          preAuthenticate="Boolean"
          userAgent="Layout">
    <parameter name="String" layout="Layout" parametertype="System.Type"/><!-- repeated -->
    <header name="String" layout="Layout"/><!-- repeated -->
  </target>
</targets>

Parameters

General Options

  • name - Name of the target.

HttpRequest Parameters Options

<parameter> defines a single parameter to be passed in the Http Request:

  • name - Name of the parameter.
  • layout - Layout that should be use to calculate the value for the parameter. Layout Required.
  • parametertype - Type of the parameter. System.Type

HttpRequest Headers Options

<header> defines a single Http-header to be included in Http Request:

  • name - Name of the header parameter.
  • layout - Layout that should be use to calculate the value for the parameter. Layout Required.

NLog 4.6 allows override of Http-header SoapAction for Protocol Soap11

Web Service Options

  • url - Web service URI. Layout Required.

    NLog 5.0 changed datatype from string to Layout.

  • encoding - Encoding. Encoding name like "utf-8", "ascii" or "utf-16". See Encoding class on MSDN

  • includeBOM - Skip or add Byte-order-mark (BOM) for UTF-8. Only used if encoding is set to UTF-8, because a BOM is optional in UTF-8. default: false. For more info on BOM, check Wikipedia. Possible options:

    • null: doesn’t change BOM.
    • true: always include UTF-8 BOM UTF-8 encodings.
    • false: default, always skip BOM on UTF-8 encodings.
  • escapeDataRfc3986 - NLog will by default encode parameters as UTF8 and escape special characters according to Rfc2396. To escape data according to standard Rc3986, set this option to 'true' (Available from NLog 4.4)

  • escapeDataNLogLegacy - NLog will by default encode parameters as UTF8 and escape special characters according to Rfc2396. To escape data according to the old non-standard NLog style, set this option to 'true' (Available from NLog 4.4)

  • Protocol - Protocol to be used when calling web service. Default: Soap11
    Possible values:

    • JsonPost - JSON POST - introduced in NLog 4.4 - ContentType "application/json"
    • XmlPost - XML POST - introduced in NLog 4.4 - ContentType "application/xml"
    • HttpGet - Use HTTP GET Protocol.
    • HttpPost - Use HTTP POST Protocol. ContentType "application/x-www-form-urlencoded"
    • Soap11 - Use SOAP 1.1 Protocol. Remember to configure Namespace + MethodName
    • Soap12 - Use SOAP 1.2 Protocol. Remember to configure Namespace + MethodName
  • Namespace - Web service namespace for soap:Envelope (Soap11 + Soap12)

  • MethodName - Web service method name for soap:Envelope (Soap11 + Soap12)

  • PreAuthenticate - Indicates whether to pre-authenticate the HttpWebRequest (Requires 'Authorization' in Headers parameters)

  • ProxyType - Configuration of HttpWebRequest.Proxy. Default: DefaultWebProxy (Introduced with NLog 4.5)
    Possible values:

    • DefaultWebProxy - Default proxy configuration from WebRequest.DefaultWebProxy (defaultProxy in app.config)
    • AutoProxy - WebRequest.GetSystemWebProxy() with default network credentials (Not supported on NetCore)
    • ProxyAddress - User defined proxy address retrieved from the ProxyAddress parameter.
    • NoProxy - Disables use of proxy (fast)
  • ProxyAddress - User defined proxy address, include port separated by a colon (Introduced with NLog 4.5)

  • UserAgent - User defined Http-Header 'User-Agent' for the web-request (Introduced with NLog 4.7.11)

Remarks

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

Example

Example config:

<nlog>
    <targets>
        <target type='WebService'
                name='ws'
                url='http://localhost:1234/logme/post'
                protocol='HttpPost'
                encoding='UTF-8'>
            <parameter name='param1' type='System.String' layout='${message}'/> 
            <parameter name='param2' type='System.String' layout='${level}'/>
        </target>
    </targets>
    <rules>
      <logger name='*'writeTo='ws'></logger>
    </rules>
</nlog>

Example API controller

public class LogMeController : ApiController
{
    /// <summary>
    /// We need a complex type for modelbinding because 
    /// of content-type: "application/x-www-form-urlencoded" 
    /// in <see cref="WebServiceTarget"/>
    /// </summary>
    public class ComplexType
    {
        public string Param1 { get; set; }
        public string Param2 { get; set; }
    }
    
    /// <summary>
    /// Post
    /// </summary>
    public void Post([FromBody] ComplexType complexType)
    {
        //do something
    }
}

Example with JSON document

NLog v4.5 supports sending custom JSON document using a single nameless parameter, instead individual named parameters:

<target type="WebService"
   name="ws"
   url="http://localhost:1234/logme/jsonpost"
   protocol="JsonPost">
   <parameter name="">
      <layout xsi:type="JsonLayout">
         <attribute name='logger' layout='${logger}' />
         <attribute name='level' layout='${level}' />
         <attribute name="message" layout="${message}" />
      </layout>
   </parameter>
</target>
Clone this wiki locally