C# 从代码跟踪WCF
我的所有连接都是通过代码设置的,而不是使用配置文件。如何在由代码构建的连接上设置WCF跟踪。如前所述,我尝试将跟踪添加到配置文件中,但它不会生成任何日志 我需要知道如何从代码中设置的连接的配置文件使其工作,或者如果任何人有任何信息,如何在代码中手动配置它。谢谢 编辑:要添加更多信息,请执行以下操作: 该应用程序是一个C#控制台应用程序,我的绑定声明为:C# 从代码跟踪WCF,c#,.net,wcf,trace,C#,.net,Wcf,Trace,我的所有连接都是通过代码设置的,而不是使用配置文件。如何在由代码构建的连接上设置WCF跟踪。如前所述,我尝试将跟踪添加到配置文件中,但它不会生成任何日志 我需要知道如何从代码中设置的连接的配置文件使其工作,或者如果任何人有任何信息,如何在代码中手动配置它。谢谢 编辑:要添加更多信息,请执行以下操作: 该应用程序是一个C#控制台应用程序,我的绑定声明为: private Binding getBinding() { NetTcpBinding tcp = new NetTcpBinding
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]);
}
private List m_Hosts=new List();
私有静态列表m_服务=新列表();
公共void AddHost(字符串uri)
其中T1:class
其中T2:类
{
m_Services.Add(“net.tcp://:/”+uri);
m_Hosts.Add(新服务主机(typeof(T1));
m_Hosts[m_Hosts.Count-1].AddServiceEndpoint(typeof(T2),getBinding(),m_Services[m_Services.Count-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>
读取.svclog文件
确保保存.svclog的路径具有必要的写入权限。仅针对此处的记录,介绍如何通过代码更改日志文件名
您需要确保initializeData=
属性具有文件夹位置中可由您的服务写入的文件名。否则什么都没写,什么都没写。。。它不会抛出任何异常或任何东西,但也不会创建日志文件。@md5sum:您能检查主机应用程序是否正在读取您的app.config
文件吗?例如,尝试在您的app.config
中添加
,并尝试使用主机应用程序中的System.Configuration.ConfigurationSettings.AppSettings[“TEST_KEY”]
读取它。是的,正在读取,因为我有一个配置类,它允许我使用System.configuration读取和修改app.Config文件。@md5:看起来写权限确实有问题。。。在任何情况下,请在关闭
标记之前尝试添加
。