简体   繁体   中英

WCF Diagnostics Message Logging With Unique Id

I'm logging messages for WCF web service using System.Diagnostics System.ServiceModel.MessageLogging with configuration below. And I extended TraceListener class as:

public class FormattedTraceListener : TraceListener
    {
        static readonly Logger logger = new Logger();

        public FormattedTraceListener()
            : base(string.Empty)
        {
        }

        public override void TraceData(TraceEventCache eventCache,String source,TraceEventType eventType,**Int32 id,**Object data)
        {
//....
 <system.diagnostics>
    <sources>
      <source name="System.ServiceModel.MessageLogging" >
        <listeners>
          <add name="pretty" />
          <remove name="Default"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="pretty" lockItem="true" type="LoggingTest.FormattedXmlWriterTraceListener,LoggingTest" />
    </sharedListeners>
    <trace autoflush="true" indentsize="4">
    </trace>
</system.diagnostics>

....

 <diagnostics >
      <messageLogging
                    logEntireMessage="true"
                    logMalformedMessages="false"
                    logMessagesAtServiceLevel="true"
                    logMessagesAtTransportLevel="false"
                    maxMessagesToLog="-1"
                    maxSizeOfMessageToLog="134217728">
        <filters >
          <add xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">/s:Envelope</add>
          <add xmlns:s="http://www.w3.org/2003/05/soap-envelope">/s:Envelope</add>
        </filters>
      </messageLogging>
      <endToEndTracing  propagateActivity="true" activityTracing="true" messageFlowTracing="true" />
    </diagnostics>
</system.serviceModel>

TraceData method fired twice (for request and response) for each service call. However, when method is hit, the id parameter is always 0.

I need a unique id in logging text to match request and response log. How can I specify that?

Your approach is not right. The TraceData(int id) field is not a unique id for the WCF request. It's the application defined code for the event type.

Just use the standard WCF trace classes and view correlated traces . If I understand what you are trying to accomplish in matching up request to response, the default behavior already meets your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM