Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Tracing From Code

Tags:

c#

.net

trace

wcf

I have all of my connections set up from my code, as opposed to using my config file. How does one go about setting up WCF Tracing on a connection built from code. I tried adding the tracing to the config file, as explained here, but it produces no logs whatsoever.

I need to know either how to make it work from the config file for connections set up in code, or how to configure it manually in code if anyone has any info. Thanks!

EDIT: To add a little more information:

The application is a C# Console application, and my binding is declared as:

private Binding getBinding()
{
    NetTcpBinding tcp = new NetTcpBinding();
    tcp.ReaderQuotas.MaxArrayLength = 65535;
    tcp.TransferMode = TransferMode.Streamed;
    tcp.ReaderQuotas.MaxArrayLength = int.MaxValue;
    tcp.ReaderQuotas.MaxDepth = int.MaxValue;
    tcp.ReaderQuotas.MaxStringContentLength = int.MaxValue;
    tcp.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
    tcp.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
    tcp.MaxReceivedMessageSize = int.MaxValue;
    return tcp;
}

And I then add services to my app using a generic function:

private List<ServiceHost> m_Hosts = new List<ServiceHost>();
private static List<string> m_Services = new List<string>();

public void AddHost<T1, T2>(string uri)
    where T1 : class
    where T2 : class
{
    m_Services.Add("net.tcp://<ipaddress>:<port>/" + uri);
    m_Hosts.Add(new ServiceHost(typeof(T1)));
    m_Hosts[m_Hosts.Count - 1].AddServiceEndpoint(typeof(T2), getBinding(), m_Services[m_Services.Count - 1]);
}

There's obviously a little more code to make this all work, but this should give any relevant parts.

like image 317
Nathan Wheeler Avatar asked Dec 18 '09 19:12

Nathan Wheeler


People also ask

How can I trace a WCF service call?

Tracing is not enabled by default. To activate tracing, you must create a trace listener and set a trace level other than "Off" for the selected trace source in configuration; otherwise, WCF does not generate any traces. If you do not specify a listener, tracing is automatically disabled.

How do I enable tracing and message logging in WCF?

Windows Communication Foundation (WCF) does not log messages by default. To activate message logging, you must add a trace listener to the System. ServiceModel. MessageLogging trace source and set attributes for the <messagelogging> element in the configuration file.

How do I enable WCF tracing on client?

Configure TracingOpen the app. config file of the WCF service library and add the configuration for system. diagnostics. In this configuration we are using "Critical, Information, ActivityTracing" as switch value.


1 Answers

The following is an .config example to enable tracing, if you want to give it another attempt. Make sure the .config file is located in the same folder of your WCF service host.

<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Warning" propagateActivity="true" >
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>

      <source name="myUserTraceSource" switchValue="Warning, ActivityTracing">
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
    </sources>

    <sharedListeners>
      <add name="xml" 
           type="System.Diagnostics.XmlWriterTraceListener" 
           initializeData="TraceLog.svclog" />
    </sharedListeners>

  </system.diagnostics>
</configuration>

Microsoft provides a Service Trace Viewer Tool to read .svclog files.

Make sure the path where you will be saving the .svclog has the necessary write permissions.

like image 173
Daniel Vassallo Avatar answered Sep 28 '22 14:09

Daniel Vassallo