WCF跟踪代码

时间:2009-12-18 19:51:18

标签: c# .net wcf tracing

我的所有连接都是从我的代码中设置的,而不是使用我的配置文件。如何在从代码构建的连接上设置WCF跟踪。我尝试将跟踪添加到配置文件中,如here所述,但它不会生成任何日志。

我需要知道如何使用配置文件使其在代码中设置连接,或者如果有人有任何信息,如何在代码中手动配置它。谢谢!

编辑:添加更多信息:

该应用程序是一个C#Console应用程序,我的绑定声明为:

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;
}

然后我使用通用函数向我的应用添加服务:

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]);
}

显然还有一些代码可以使这一切工作,但这应该给出任何相关的部分。

2 个答案:

答案 0 :(得分:5)

以下是启用跟踪的.config示例,如果您想再次尝试。确保.config文件位于WCF服务主机的同一文件夹中。

<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提供Service Trace Viewer Tool来读取.svclog文件。

确保保存.svclog的路径具有必要的写入权限。

答案 1 :(得分:0)

这里的记录是如何通过代码

更改日志文件名

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