locked
Enabling/Disabling WCF Trace without changing the config files RRS feed

  • Question

  • User-1716690740 posted

    Hi , Is there a way to enable/disable the WCF trace/logging for a perticular end point without changing the web.config ?

    Tuesday, August 13, 2013 3:46 PM

Answers

All replies

  • User-837620913 posted

    With WCF 4 and later you can activate Event Tracing for Windows (ETW) without modifying the app. See this guide: http://msdn.microsoft.com/en-us/library/dd764466.aspx

    You can also write code to change the tracing level. Here is an example: http://wcfpro.wordpress.com/2010/11/21/how-to-add-wcf-traces-programmatically/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 13, 2013 4:55 PM
  • User-1635195291 posted

    Hi sk_aspnet,

    Please go through the links below:

    1.Create our normal WCF sources for logging in config file

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.diagnostics>
            <sources>
                <source name="System.ServiceModel" switchValue="Warning,ActivityTracing">
                    <listeners>
                        <add name="CustomListner"/>
                    </listeners>
                </source>
              <source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
                <listeners>
                  <add name="CustomListner" />
                </listeners>
              </source>
            </sources>
            <sharedListeners>
                <add  type="WCFTracing.CustomTraceListner, WCFTracing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                    name="CustomListner" />
            </sharedListeners>
          <trace autoflush="true" />
        </system.diagnostics>
        <system.serviceModel>
            <diagnostics>
                <messageLogging logEntireMessage="true" logKnownPii="true" logMalformedMessages="true"
                    logMessagesAtServiceLevel="true" />
                <endToEndTracing activityTracing="true" />
            </diagnostics>
        </system.serviceModel>
    </configuration>

    2. Create a custom listner class which is mentioned in the config. Please note the function "CheckIfAllowed". This actually returns a memory stream  in case logging is turned off. This ensures that the constructor doesn't fail and also no file is generated. Also, we are limiting buffer of memory stream to 2 bytes.And that's it. You can view the file using svctraceviewer conveniently.

    public class CustomTraceListner : XmlWriterTraceListener
        {
            public CustomTraceListner()
                : base(CheckIfAllowed())
            {
     
            }
            private static Stream CheckIfAllowed()
            {
                bool ifAllowed = true;
                if (ifAllowed)
                    return new FileStream(@"D:\temp.svclog", FileMode.OpenOrCreate, FileAccess.Write);
                 return new MemoryStream(2);
            }
        }
     

    Hope this helps.

    Thanks,

    Jatin

    Tuesday, August 13, 2013 9:39 PM
  • User-1716690740 posted

    Thanks for your post . I am hosting my wcf in IIS ; The turnoff and turnon switch is not working for me ;

    Below is the code

        

    namespace WCFTracing
    {
    public class CustomTraceListner : XmlWriterTraceListener
    {
    public CustomTraceListner()
    : base(CheckIfAllowed())
    {

    }

    private static Stream CheckIfAllowed()
    {
    bool ifAllowed = isEnabled(@"C:\POC\WCFCustomListner\configdb.xml", "enabletrace");
    if (ifAllowed)
    {
    return new FileStream(@"C:\POC\WCFCustomListner\Log\temp20.svclog", FileMode.OpenOrCreate);
    }
    else
    {
    return new MemoryStream(2);
    }
    }

    private static bool isEnabled(string path, string elementName)
    {
    try
    {
    string elementValue = string.Empty;
    bool returnVal = false;
    using (XmlTextReader reader = new XmlTextReader(path))
    {

    reader.ReadToFollowing(elementName);
    if (reader.Read())
    elementValue = reader.Value;

    }
    if (!string.IsNullOrEmpty(elementValue))
    {
    bool.TryParse(elementValue, out returnVal);
    }
    return returnVal;

    }
    catch (Exception ex)
    {
    return false;
    }
    }

    }

    }

    Thursday, August 15, 2013 2:11 PM
  • User-1716690740 posted

    Hay Darell,

    Thanks for your time and reply ;

    The switch is not working in IIS hosted WCF . Let me know

    Thursday, August 15, 2013 2:14 PM
  • User260886948 posted

    Hi,

    Have you tried the first solution post by @DarrellNorton? In my mind, it should work.

    And I see that you have post a same question in: http://social.msdn.microsoft.com/Forums/en-US/67126bfe-74a2-418a-b7f2-2bd96424ef65/how-to-enable-wcf-traces-programmatically .

    Please try to check the replies.

    Best Regards,
    Amy Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 16, 2013 1:45 AM