Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to log JSON response in WCF at service level
    text
    copied!<p>I have a WCF service that returns JSON to the clients.</p> <p>This is the app.config</p> <pre><code> &lt;system.serviceModel&gt; &lt;services&gt; &lt;service name="MyServices.MyService"&gt; &lt;endpoint address="" behaviorConfiguration="JsonBehavior" binding="webHttpBinding" contract="Myervices.IMyervice"&gt; &lt;identity&gt; &lt;dns value="localhost" /&gt; &lt;/identity&gt; &lt;/endpoint&gt; &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt; &lt;host&gt; &lt;baseAddresses&gt; &lt;add baseAddress="http://localhost:8732/Design_Time_Addresses/MyEmulator/Service1/" /&gt; &lt;/baseAddresses&gt; &lt;/host&gt; &lt;/service&gt; &lt;/services&gt; &lt;behaviors&gt; &lt;serviceBehaviors&gt; &lt;behavior&gt; &lt;serviceMetadata httpGetEnabled="True"/&gt; &lt;serviceDebug includeExceptionDetailInFaults="True"/&gt; &lt;/behavior&gt; &lt;/serviceBehaviors&gt; &lt;endpointBehaviors&gt; &lt;behavior name="JsonBehavior"&gt; &lt;webHttp /&gt; &lt;/behavior&gt; &lt;/endpointBehaviors&gt; &lt;/behaviors&gt; </code></pre> <p></p> <p>This is a sample opertaion exposed by the service</p> <pre><code> [OperationContract] [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, Method = "GET", UriTemplate = "me/{id}")] JsonObject GetObject(string id, string connection_type); </code></pre> <p>Now, I would like to log every request that the service receives from clients and response that is being sent back to clients...in this case it sin JSOn.</p> <p>I have create a custom MessageInspector as follows</p> <pre><code>public class MyMessageInspector : IDispatchMessageInspector { private bool LoggingEnabled; #region IDispatchMessageInspector Members public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext) { Guid correlationState = Guid.NewGuid(); if (this.LoggingEnabled) { StringBuilder message = new StringBuilder(); message.AppendFormat("CorrelationState = {0}{1}", correlationState, Environment.NewLine); message.AppendFormat("Local Address = {0}{1}", channel.LocalAddress, Environment.NewLine); message.AppendFormat("Remote Address = {0}{1}", channel.RemoteAddress, Environment.NewLine); message.AppendLine("Request"); message.AppendLine(request.ToString()); message.AppendLine(); this.logger.LogMessage(message.ToString()); } return correlationState; } public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState) { if (this.LoggingEnabled) { StringBuilder message = new StringBuilder(); message.AppendFormat("CorrelationState = {0}{1}", correlationState, Environment.NewLine); message.AppendLine("Reply"); message.AppendLine(reply.ToString()); message.AppendLine(); this.logger.LogMessage(message.ToString()); } } #endregion } </code></pre> <p>But this doesnt seem to log any of the resposne.. the response.Tostring() only says "...stream"</p> <p>the Above works if my service returns data in SOAP XML format..but i want it to log JSON response which is the text inside the *.js file downloaded in the browser when i call the REST service endpoint. ex: <a href="http://localhost:8080/MyService/me/2/" rel="nofollow">http://localhost:8080/MyService/me/2/</a></p> <p>any ideas how I can achieve this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload