Calls the specified web service on each log message.
Platforms Supported: All - Requires nuget-package NLog.Targets.WebService since NLog v6
<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>
<parameter>
defines a single parameter to be passed in the Http Request:
<header>
defines a single Http-header to be included in Http Request:
NLog 4.6 allows override of Http-header
SoapAction
for ProtocolSoap11
url - Web service URI. Layout Required.
NLog 5.0 changed property-type from string to NLog 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)
NLog v6 no longer supports this encoding, and marked option obsolete.
Protocol - Protocol to be used when calling web service. Default: Soap11
Possible values:
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:
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)
The web service must implement a method that accepts a number of string parameters.
<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>
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>
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4