tags:

views:

482

answers:

2

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.

+2  A: 

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.

Daniel Vassallo
You'll want to make sure that the `initializeData=` attribute has the name of a file in a folder location that's writable by your service. Otherwise nothing is written.
Jeremy McGee
Nothing whatsoever... It doesn't throw any exceptions or anything, but it also doesn't create a log file either.
md5sum
@md5sum: Can you check that your `app.config` file is being read by the host application? For example, try adding `<appSettings><add key="TEST_KEY" value="1" /></appSettings>` in your `app.config` and try to read it with `System.Configuration.ConfigurationSettings.AppSettings["TEST_KEY"]` from your host application.
Daniel Vassallo
Yes, it is being read, as I have a configuration class that allows me to read and modify the app.Config file using System.Configuration.
md5sum
@md5: It really looks like a problem with write permissions... In any case, try adding `<trace autoflush="true" />` before closing the `</system.diagnostics>` tag.
Daniel Vassallo
It shouldn't be an issue with write permissions, as my application writes files to several other locations, creates folders, etc. on several other directories, unless there's something specific with the diagnostics that needs special permissions outside of my application's normal permission set. I will try adding the line and see what happens.
md5sum
@md5: To eliminate the doubt, create a folder like `C:\tracelogs` and give this folder full permissions. Then change the `initializeData` attribute to `initializeData="C:\tracelogs\TraceLog.svclog"`
Daniel Vassallo
Interestingly enough, it seems to be working properly now, with or without the `autoflush` property set, and without changing directories. Oddly, without any modification to the Server side, it started working around 11 today. I had been making the changes to the client. I found through making the name change to the folder that I was missing a character in my folder name, which would have caused the problem with the client, but I have no idea why the server just started working all the sudden. Most likely a PEBCAK... Thanks for your help though!
md5sum
A: 

Just for the records here is how to change the log file name by code

http://geekswithblogs.net/FlippinIT/archive/2009/11/12/wcf-tracing-in-code.aspx

MarcosMeli