none
How to enable WCF traces programmatically? RRS feed

  • Question

  • Is there a way to enable/disable the WCF trace/logging for a perticular end point without changing the web.config ?
    Thursday, August 15, 2013 4:52 PM

Answers

  • Hi sk_aspnet,

    You can use IClientMessageInspector and IDispatchMessageInspector interfaces to trace all the incoming and outgoing message details.

    Message Inspector must be added in particular endpoint using IEndPointBehavior.

    This same trace can be enabled and disabled based on configuration settings defined in config file.

    Sample code is given below...

        public class CustomMessageInspector : IEndpointBehavior, IDispatchMessageInspector
        {
            public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
            {
                Console.WriteLine("AddBindingParameters");
            }
    
            public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
            {
                Console.WriteLine("ApplyClientBehavior");
            }
    
            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
            {
                Console.WriteLine("ApplyDispatchBehavior");
                endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
            }
    
            public void Validate(ServiceEndpoint endpoint)
            {
                Console.WriteLine("Validate");
            }
    
            public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
            {
                Console.WriteLine("AfterReceiveRequest");
                
    
                return null;
            }
    
            public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
            {
                Console.WriteLine("BeforeSendReply");
                System.Diagnostics.Debug.WriteLine(reply.ToString());
            }
        }
    }


    Thanks
    Subhendu DE - Visit My Blog If a post answers your question, please click Mark As Answer on that post. If you find a post helpful, please click Vote as Helpful.

    Friday, August 16, 2013 9:15 AM
  • Hello,

    This is more of a system.diagnostics question than WCF as all we are really doing with the web.config settings is telling diagnostics to log the ETW events from the System.ServiceModel and System.ServiceModel.MessageLogging. 

    Most of the microsoft libraries use ETW and you can get insight into other libraries also.  For example, if you suspect there is something happening to the messages after they leave the WCF stack or you need greater insight into security, try adding a listener to the System.Net library (re).

    If I was to approach what you want to do, I would create a custom event listener for example by extending the xmlwriter: 

    public class RollingXmlWriterTraceListener : XmlWriterTraceListener

    Then you could implement some form of turning on/off the writing by a method in this class.  This is mostly because it encapsulates the change and can then be used simply by adding it any .net project.

    Another approach would be to add the listener programmitically; but after some searching, I came across this that shows that adding a listener programmitically will not work.  For other libraries, you could add a tracesource and listener programmatically but it looks like it will not work for WCF sources.

    Hope this helps


    Jeff

    Thursday, August 15, 2013 11:09 PM
  • Hi,

    If you want to enable/disable tracing, please try to use the WMI which can meet your requirement.

    For more information, please try to refer to:

    #Using Windows Management Instrumentation for Diagnostics:
    http://msdn.microsoft.com/en-us/library/ms735120.aspx .

    #WMI Support in WCF:
    http://weblogs.asp.net/cibrax/archive/2007/07/03/wmi-support-in-wcf.aspx .

    #How to enable WCF traces programmatically:
    http://wcfpro.wordpress.com/2010/11/21/how-to-add-wcf-traces-programmatically/ .

    Here is a similar thread, please try to refer to:
    http://social.msdn.microsoft.com/Forums/vstudio/en-US/6a257576-7a19-48fc-9c1f-b8d8effb41fd/is-it-possible-to-programmatically-configure-wcf-logging .

    Best Regards.


    Amy Peng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Friday, August 16, 2013 5:42 AM
    Moderator

All replies

  • Hello,

    This is more of a system.diagnostics question than WCF as all we are really doing with the web.config settings is telling diagnostics to log the ETW events from the System.ServiceModel and System.ServiceModel.MessageLogging. 

    Most of the microsoft libraries use ETW and you can get insight into other libraries also.  For example, if you suspect there is something happening to the messages after they leave the WCF stack or you need greater insight into security, try adding a listener to the System.Net library (re).

    If I was to approach what you want to do, I would create a custom event listener for example by extending the xmlwriter: 

    public class RollingXmlWriterTraceListener : XmlWriterTraceListener

    Then you could implement some form of turning on/off the writing by a method in this class.  This is mostly because it encapsulates the change and can then be used simply by adding it any .net project.

    Another approach would be to add the listener programmitically; but after some searching, I came across this that shows that adding a listener programmitically will not work.  For other libraries, you could add a tracesource and listener programmatically but it looks like it will not work for WCF sources.

    Hope this helps


    Jeff

    Thursday, August 15, 2013 11:09 PM
  • Hi,

    If you want to enable/disable tracing, please try to use the WMI which can meet your requirement.

    For more information, please try to refer to:

    #Using Windows Management Instrumentation for Diagnostics:
    http://msdn.microsoft.com/en-us/library/ms735120.aspx .

    #WMI Support in WCF:
    http://weblogs.asp.net/cibrax/archive/2007/07/03/wmi-support-in-wcf.aspx .

    #How to enable WCF traces programmatically:
    http://wcfpro.wordpress.com/2010/11/21/how-to-add-wcf-traces-programmatically/ .

    Here is a similar thread, please try to refer to:
    http://social.msdn.microsoft.com/Forums/vstudio/en-US/6a257576-7a19-48fc-9c1f-b8d8effb41fd/is-it-possible-to-programmatically-configure-wcf-logging .

    Best Regards.


    Amy Peng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Friday, August 16, 2013 5:42 AM
    Moderator
  • Hi sk_aspnet,

    You can use IClientMessageInspector and IDispatchMessageInspector interfaces to trace all the incoming and outgoing message details.

    Message Inspector must be added in particular endpoint using IEndPointBehavior.

    This same trace can be enabled and disabled based on configuration settings defined in config file.

    Sample code is given below...

        public class CustomMessageInspector : IEndpointBehavior, IDispatchMessageInspector
        {
            public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
            {
                Console.WriteLine("AddBindingParameters");
            }
    
            public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
            {
                Console.WriteLine("ApplyClientBehavior");
            }
    
            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
            {
                Console.WriteLine("ApplyDispatchBehavior");
                endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
            }
    
            public void Validate(ServiceEndpoint endpoint)
            {
                Console.WriteLine("Validate");
            }
    
            public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
            {
                Console.WriteLine("AfterReceiveRequest");
                
    
                return null;
            }
    
            public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
            {
                Console.WriteLine("BeforeSendReply");
                System.Diagnostics.Debug.WriteLine(reply.ToString());
            }
        }
    }


    Thanks
    Subhendu DE - Visit My Blog If a post answers your question, please click Mark As Answer on that post. If you find a post helpful, please click Vote as Helpful.

    Friday, August 16, 2013 9:15 AM
  • I believe Amy Peng's suggestion for logging messages works for services that are *listening*.

    If your service *initiates* a request, you need to make a couple changes:

    1. CustomMessageInspector class needs to implement System.ServiceModel.Dispatcher.IClientMessageInspector

    2. Implement AfterReceiveReply and BeforeSendRequest (I'm using VB.Net)

           Public Sub AfterReceiveReply(ByRef reply As Channels.Message, correlationState As Object) Implements Dispatcher.IClientMessageInspector.AfterReceiveReply
                System.Console.WriteLine("AfterReceiveReply")

            End Sub

            Public Function BeforeSendRequest(ByRef request As Channels.Message, channel As IClientChannel) As Object Implements Dispatcher.IClientMessageInspector.BeforeSendRequest
                System.Console.WriteLine("BeforeSendRequest")
                Return Nothing
            End Function

    3. Make this change to ApplyClientBehavior:

            Public Sub ApplyClientBehavior(endpoint As Description.ServiceEndpoint, clientRuntime As Dispatcher.ClientRuntime) Implements Description.IEndpointBehavior.ApplyClientBehavior
                clientRuntime.MessageInspectors.Add(Me)
                System.Console.WriteLine("ApplyClientBehavior")

            End Sub

    • Edited by sevzas Wednesday, September 10, 2014 3:59 PM
    Wednesday, September 10, 2014 3:56 PM