【问题标题】:WCF Tracing From Code从代码跟踪 WCF
【发布时间】:2009-12-18 19:51:17
【问题描述】:

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

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

编辑:添加更多信息:

应用程序是一个 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]);
}

显然还有一些代码可以使这一切正常工作,但这应该提供任何相关部分。

【问题讨论】:

    标签: c# .net wcf trace


    【解决方案1】:

    如果您想再次尝试,以下是启用跟踪的.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 的路径具有必要的写入权限。

    【讨论】:

    • 您需要确保initializeData= 属性具有您的服务可写的文件夹位置中的文件名。否则什么都不写。
    • 什么都没有......它不会抛出任何异常或任何东西,但它也不会创建日志文件。
    • @md5sum:您能检查一下您的app.config 文件是否正在被主机应用程序读取?例如,尝试在您的app.config 中添加&lt;appSettings&gt;&lt;add key="TEST_KEY" value="1" /&gt;&lt;/appSettings&gt;,并尝试在您的主机应用程序中使用System.Configuration.ConfigurationSettings.AppSettings["TEST_KEY"] 读取它。
    • 是的,它正在被读取,因为我有一个配置类允许我使用 System.Configuration 读取和修改 app.Config 文件。
    • @md5:看起来确实是写权限的问题……无论如何,在关闭&lt;/system.diagnostics&gt;标签之前尝试添加&lt;trace autoflush="true" /&gt;
    【解决方案2】:

    这里仅记录一下如何通过代码更改日志文件名

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

    【讨论】:

      猜你喜欢
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      相关资源
      最近更新 更多