NLog:WebServiceTarget能否直接使用JsonLayout而非包裹在<parameter/>中?
Great question! You’re right that WebServiceTarget doesn’t support directly embedding JsonLayout like File or Console targets do out of the box. But don’t worry—there are straightforward, effective ways to get the exact output you want, with all your log properties included. Let’s walk through the two most common approaches:
1. Serialize the entire log event to a JSON string parameter
This method wraps the JsonLayout inside a <parameter> tag, letting you send the complete serialized log event (including all properties) as a single string parameter to your web service. Your service can then deserialize this string to access all the log data.
Here’s a sample NLog configuration for this approach:
<target xsi:type="WebService" name="myWebServiceLog" url="https://your-service-endpoint.com/submit-log" protocol="Post"> <!-- Send the full JSON-serialized log event as a single parameter --> <parameter name="logPayload" type="System.String"> <layout xsi:type="JsonLayout" includeAllProperties="true"> <!-- Add explicit attributes for core log fields (optional but recommended) --> <attribute name="timestamp" layout="${date:format=o}" /> <attribute name="logLevel" layout="${level}" /> <attribute name="message" layout="${message}" /> <!-- includeAllProperties="true" will automatically add all custom log properties here --> </layout> </parameter> </target>
With this setup, your web service will receive a logPayload parameter containing a JSON string with all your log properties (both built-in and custom).
2. Send JSON directly as the request body
If your web service expects raw JSON in the request body (instead of form parameters), you can adjust the WebServiceTarget to send the JsonLayout output directly as the request content.
Here’s how to configure this:
<target xsi:type="WebService" name="myWebServiceLog" url="https://your-service-endpoint.com/submit-log" protocol="Post" contentType="application/json"> <!-- Leave the parameter name empty to send the JSON as the request body --> <parameter name="" type="System.String"> <layout xsi:type="JsonLayout" includeAllProperties="true"> <attribute name="timestamp" layout="${date:format=o}" /> <attribute name="logLevel" layout="${level}" /> <attribute name="message" layout="${message}" /> </layout> </parameter> </target>
Key changes here:
- Set
contentType="application/json"to tell the web service we’re sending JSON - Leave the
<parameter>name blank so NLog doesn’t wrap the JSON in form parameters, instead sending it directly as the request body
This way, your web service can parse the request body directly as a JSON object, with all log properties included.
Bonus: Customizing the JSON output
If you need more control over the serialized JSON (like excluding certain properties, formatting dates, or renaming fields), you can extend the JsonLayout configuration. For example:
<layout xsi:type="JsonLayout" includeAllProperties="true" excludeProperties="EventId,ProcessId"> <attribute name="timestamp" layout="${date:format=yyyy-MM-dd HH:mm:ss.fff}" /> <attribute name="level" layout="${level:uppercase=true}" /> </layout>
This excludes the EventId and ProcessId properties, and formats the timestamp and log level to match specific requirements.
内容的提问来源于stack exchange,提问作者user3205479

