2009-12-18 133 views
7

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

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

编辑:要添加一点点的更多信息:

应用程序是一个C#控制台应用程序,和我的结合被声明为:

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

显然有更多的代码,使这一切工作,但这应该给任何相关的部分。

回答

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> 

微软提供了一个Service Trace Viewer Tool阅读.svclog文件。

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

+1

你要确保了'initializeData ='属性的文件夹中有一个文件名您的服务可写入的位置。否则什么都不写。 – 2009-12-18 19:58:51

+0

什么也没有......它不会抛出任何异常或任何东西,但它也不会创建日志文件。 – 2009-12-18 20:21:08

+0

@ md5sum:你能检查你的'app.config'文件是否被主机应用程序读取?例如,尝试在'app.config'中添加''并尝试使用'System.Configuration.ConfigurationSettings.AppSettings [“TEST_KEY “]'来自主机应用程序。 – 2009-12-18 23:07:32