166

Update:

I have been trying to turn on WCF tracing, but still no success... Below is my lastest update.

Do I need a permission to write to the below location?

  <system.diagnostics>
    <trace autoflush="true" />
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="sdt"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData= "@\\myservername\folder1\traces.svclog" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

I am using .NET Framework 3.5.

What is the step-by-step instruction to turn on the WCF tracking for debugging purposes?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406

4 Answers4

235

The following configuration taken from MSDN can be applied to enable tracing on your WCF service.

<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true" >
        <listeners>
             <add name="xml"/>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
            <add name="xml"/>
        </listeners>
      </source>
      <source name="myUserTraceSource"
              switchValue="Information, ActivityTracing">
        <listeners>
            <add name="xml"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
        <add name="xml"
             type="System.Diagnostics.XmlWriterTraceListener"
             initializeData="Error.svclog" />
    </sharedListeners>
  </system.diagnostics>
</configuration>

To view the log file, you can use "C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\SvcTraceViewer.exe".

If "SvcTraceViewer.exe" is not on your system, you can download it from the "Microsoft Windows SDK for Windows 7 and .NET Framework 4" package here:

Windows SDK Download

You don't have to install the entire thing, just the ".NET Development / Tools" part.

When/if it bombs out during installation with a non-sensical error, Petopas' answer to Windows 7 SDK Installation Failure solved my issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rohan West
  • 9,262
  • 3
  • 37
  • 64
  • 19
    where do i see the file is genereated `Error.svclog` ? – Nick Kahn Nov 24 '10 at 21:16
  • i have implemented in the web.config on my dev box but i dont see that it generate any `svclog` file is there any other setting needs to be taken care? – Nick Kahn Nov 24 '10 at 21:22
  • 4
    The file will be generated in the same directory as your assembly. Most likely your bin directory. You may need recycle the worker process if you services are hosted in IIS. – Rohan West Nov 24 '10 at 21:22
  • my wcf servcies is hosted on IIS on the dev box and i am using those servcies from my local machine, so i do have access to the folder/files are in dev box and i looked into bin folder and other foloers but could not find the log file... – Nick Kahn Nov 24 '10 at 21:30
  • 9
    Make sure that your Application Pool - Identity has permission to write to that folder. I usually have a dedicated account assigned to my application pool, this way i can grant read\write access to that specific user – Rohan West Nov 24 '10 at 21:39
  • Will this `Tracing` will work in `Release Mode`. ? If yes.. how can we disable it ? – Sreekumar P Aug 23 '12 at 06:52
  • 2
    In the development environment, the Error file (initializeData="Error.svclog") is stored inside the solution project. Changing it to other locations did not work. – LCJ Sep 14 '12 at 04:48
  • 1
    I recomand you to use the powerfull "Microsoft Service Configuration Editor" (embeded with visual studio in my case). You cannot do everything, but it helps a lot in understanding WCF configuration, including log configuration – Charles HETIER Jun 05 '13 at 08:26
  • 6
    I was able to get it to log to a folder by using this: initializeData="C:\wcflogs\wcf_svclog.svclog" /> – Adrian Carr Aug 02 '13 at 12:51
  • @RohanWest - Actually - in my case the file was generated in the ROOT folder of my application (hosted on IIS), not the bin folder. It seems that it is relative to the web.config path, not the assembly path. – BornToCode Jul 22 '15 at 15:04
  • What is myUserTraceSource? – dko Oct 23 '15 at 17:56
  • see how to configure it using a UI: http://stackoverflow.com/a/34283667/187650 :-) – juFo Dec 15 '15 at 07:58
  • 1
    When i add the tag under i get an error saying The content type text/html of the response message does not match the content type of the binding (application/soap+xml; – user1152145 Mar 24 '16 at 14:45
  • @user1152145, the solution to that problem is to paste it at the end of the file, just above . – Tawab Wakil May 20 '19 at 17:16
37

In your web.config (on the server) add

<system.diagnostics>
 <sources>
  <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
   <listeners>
    <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\logs\Traces.svclog"/>
   </listeners>
  </source>
 </sources>
</system.diagnostics>
Christoph
  • 4,251
  • 3
  • 24
  • 38
  • 1
    i have added like this since i dont have access to the dev except the folder `initializeData="\\servername\drive$\Project\WCFTraces.svclog"/>` and i dont see that file is generated after i try to access the services. – Nick Kahn Nov 24 '10 at 21:12
  • 6
    By default it is buffered (may be able to change this). You can force it to flush by recycling the app pool. Also make sure the app pool identity can write to the location. – Christoph Jul 07 '11 at 18:50
21

Go to your Microsoft SDKs directory. A path like this:

C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools

Open the WCF Configuration Editor (Microsoft Service Configuration Editor) from that directory:

SvcConfigEditor.exe

(another option to open this tool is by navigating in Visual Studio 2017 to "Tools" > "WCF Service Configuration Editor")

wcf configuration editor

Open your .config file or create a new one using the editor and navigate to Diagnostics.

There you can click the "Enable MessageLogging".

enable messagelogging

More info: https://msdn.microsoft.com/en-us/library/ms732009(v=vs.110).aspx

With the trace viewer from the same directory you can open the trace log files:

SvcTraceViewer.exe

You can also enable tracing using WMI. More info: https://msdn.microsoft.com/en-us/library/ms730064(v=vs.110).aspx

juFo
  • 17,849
  • 10
  • 105
  • 142
1

Instead of you manual adding the tracing enabling bit into web.config you can also try using the WCF configuration editor which comes with VS SDK to enable tracing

https://stackoverflow.com/a/16715631/2218571

Community
  • 1
  • 1
CSharped
  • 1,247
  • 4
  • 20
  • 49