代码中的WCF跟踪不遵循MessageLogging设置 [英] WCF tracing in code does not follow MessageLogging settings

查看:76
本文介绍了代码中的WCF跟踪不遵循MessageLogging设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在应用程序中使用WCF跟踪,但是需要尽可能地通过代码进行控制.

I need to use WCF tracing in my application but it needs to be controlled from code as much as possible.

有人建议我在app.config文件中安装以下部分:

IT was suggested that I install the following sections in my app.config file:

<configuration>
  <system.serviceModel>
    <diagnostics>
      <messageLogging
        maxMessagesToLog="100"
        logEntireMessage="true"
        logMessagesAtServiceLevel="true"
        logMalformedMessages="true"
        logMessagesAtTransportLevel="true">
      </messageLogging>
    </diagnostics>
  </system.serviceModel>  
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" >
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="dummy"/>
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
</configuration>

然后可以使用以下代码来使跟踪根据需要运行:

Then the following code could be used to get the trace running as needed:

BindingFlags privateMember = BindingFlags.NonPublic | BindingFlags.Instance;
BindingFlags privateStaticMember = privateMember | BindingFlags.Static;

Type type = Type.GetType("System.ServiceModel.DiagnosticUtility, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
MethodInfo[] mi = type.GetMethods(privateStaticMember);

// invoke InitializeTracing   
object diagnosticTrace = mi.FirstOrDefault(e => e.Name == "InitializeTracing").Invoke(null, null);
if (diagnosticTrace != null)
{
    // get TraceSource   
    Type type2 = Type.GetType("System.ServiceModel.Diagnostics.DiagnosticTrace, SMDiagnostics, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
    PropertyInfo pi = type2.GetProperty("TraceSource", privateMember);
    TraceSource traceSource = pi.GetValue(diagnosticTrace, null) as TraceSource;

    // clear all listeners in the trace source   
    traceSource.Listeners.Clear();

    // add listener to trace source   
    XmlWriterTraceListener listener = new XmlWriterTraceListener("mylogfile".svclog");
    listener.TraceOutputOptions = TraceOptions.Timestamp | TraceOptions.Callstack;
    traceSource.Attributes["propagateActivity"] = "true";
    traceSource.Switch.ShouldTrace(TraceEventType.Verbose | TraceEventType.Start);
    traceSource.Listeners.Add(listener);

    // enable tracing   
    type.GetProperty("Level", privateStaticMember).SetValue(null, SourceLevels.All, null);

    Trace.AutoFlush = true;

这在某种程度上可以正常工作,主要问题是忽略了app.config文件的system.servicemodel部分中的消息记录设置.

This works fine up to a point, the main problem being that the messagelogging settings in the system.servicemodel section of the app.config file are being ignored.

有什么办法可以解决这个问题?

Is there anything that can be done to solve this problem?

推荐答案

我无法对您的所有代码进行注释,因为以前我没有以这种方式使用System.Diagnostics(以编程方式配置WCF通信跟踪),但是如果您对此行的意图:

I can't comment on all of your code because I have not used System.Diagnostics in this way before (programmatically configuring the WCF communication tracing), but if your intent on this line:

traceSource.Switch.ShouldTrace(TraceEventType.Verbose | TraceEventType.Start);

是要设置所需的跟踪级别,我认为您应该改用Switch.Level属性. ShouldTrace用于询问是否在给定输入标志的情况下给定的TraceSource 跟踪.

Is to set the level of tracing that you want, I think that you should be using the Switch.Level property instead. ShouldTrace is for asking if a given TraceSource would trace, given the input flags.

traceSource.Switch.Level = SourceLevels.Verbose | SourceLevels.ActivityTracing; 

请注意,根据此链接,可以配置看似合理的设置,但是活动ID可能未正确传播.请仔细阅读.它可能适用于您的情况,也可能不适用.

Note that according to this link, it is possible to configure apparently reasonable settings and yet the activity id might not be propogated correctly. Read it carefully. It may or not apply to your situation.

这篇关于代码中的WCF跟踪不遵循MessageLogging设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆