title: WCF logging/tracing and activity id propagation using log4net or NLog
body: <p>I have seen many other questions on logging.  Best practices.  What logging platform is best. Etc.  Here are some links from here on SO with very good discussions on the topic:</p>
  
  <p><a href="https://stackoverflow.com/questions/576185/logging-best-practices">logging best practices</a></p>
  
  <p><a href="https://stackoverflow.com/questions/576456/log4net-versus-tracesource">log4net vs TraceSource</a></p>
  
  <p><a href="https://stackoverflow.com/questions/98080/what-is-the-best-logging-solution-for-a-c-net-3-5-project">best logging solution for .NET 3.5 project</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1324321/net-3-5-logging">.NET 3.5 logging</a></p>
  
  <p>BEGIN EDIT:</p>
  
  <p>Having typed this long post, I guess that the main thing that I am trying to figure out is how tightly coupled WCF logging/tracing and activity id propagation are to System.Diagnostics and TraceSources.  Can you get "good" WCF logging/tracing and activity id propagation using a third party logging platform like log4net or NLog.  If you do this, how do you do it?  </p>
  
  <p>See the bottom of this post for a few questions about ServiceTraceViewer,</p>
  
  <p>END EDIT.</p>
  
  <p>The subject of my question is not discussed in much detail in any of these posts.  I am interested in what people are doing regarding logging and WCF.  If you are working on a project that includes WCF services and you have logging in your project, do you make any special efforts to use WCF-specific logging capabilities.  In particular, do you try to incorporate things like Activity Tracing, activity propagation, and End to End tracing?  Such as is outlined in <a href="http://msdn.microsoft.com/en-us/library/aa751917.aspx" rel="nofollow noreferrer">this</a> article from MSDN.  <a href="http://msdn.microsoft.com/en-us/library/aa738759.aspx" rel="nofollow noreferrer">Here</a> is another article from MSDN on propagating activities.</p>
  
  <p>The articles do a pretty job of explaining how to do activity tracing, activity propagation, and end to end tracing using System.Diagnostics TraceSources.  It shows how to configure WCF to "turn on" these options via the app.config/web.config file.  WCF uses TraceSources internally to log the results of the communication.</p>
  
  <p>Here is some sample code (from the second MSDN article linked above) that shows more or less how to achieve activity propagation via System.Diagnostics and TraceSources:</p>
  
  <pre><code>TraceSource ts = new TraceSource("myUserTraceSource");
  Guid oldID = Trace.CorrelationManager.ActivityId;
  Guid traceID = Guid.NewGuid();
  ts.TraceTransfer(0, "transfer", traceID);
  Trace.CorrelationManager.ActivityId = traceID; // Trace is static
  ts.TraceEvent(TraceEventType.Start, 0, "Add request");
  
  double value1 = 100.00D;
  double value2 = 15.99D;
  ts.TraceInformation("Client sends message to Add " + value1 + ", " + value2);
  double result = client.Add(value1, value2);
  ts.TraceInformation("Client receives Add response '" + result + "'");
  
  ts.TraceTransfer(0, "transfer", oldID);
  ts.TraceEvent(TraceEventType.Stop, 0, "Add request");
  Trace.CorrelationManager.ActivityId = oldID;
  </code></pre>
  
  <p>Here is a way that you can tell, from within a service, whether or not WCF has propagated an activity:</p>
  
  <pre><code>// Check if an activity was set in scope by WCF, i.e., if it was 
  // propagated from the client. If not, i.e., ambient activity is 
  // equal to Guid.Empty, create a new one.
  if(Trace.CorrelationManager.ActivityId == Guid.Empty)
  {
      Guid newGuid = Guid.NewGuid();
      Trace.CorrelationManager.ActivityId = newGuid;
  }
  // Emit your Start trace.
  ts.TraceEvent(TraceEventType.Start, 0, "Add Activity");
  
  // Emit the processing traces for that request.
  serviceTs.TraceInformation("Service receives Add " 
                          + n1 + ", " + n2);
  // double result = n1 + n2;
  serviceTs.TraceInformation("Service sends Add result" + result);
  
  // Emit the Stop trace and exit the method scope.
  ts.TraceEvent(TraceEventType.Stop, 0, "Add Activity");
  // return result;
  </code></pre>
  
  <p>From all of the examples that I have seen, activity propagation is achieved configuring (typically via app.config) the System.Service model TraceSource and setting its propagateActivity property to "true".  Activities are actually propagated by setting the activity id (guid) on Trace.CorrelationManager.ActivityId.  Can WCF logging and activity propagation be used effectively if you are using log4net or NLog?</p>
  
  <p>My project will be using WCF very heavily.  We are currently trying to settle on our logging solution.  I think that I have a pretty good understanding of how WCF logging and activity propagation works with System.Diagnostics and TraceSources.  I would like to understand better how/if something similar can be achieved with logging platforms like log4net and NLog.  </p>
  
  <p>Do they provide some "native" support?  It seems a little more likely that they provide some infrastructure so that the activity propagation could be achieved "manually".  Maybe something like this:</p>
  
  <pre><code>//Inside client code:
  ILog logger = LogManager.GetLogger("client");
  Guid oldActivity = Trace.CorrelationManager.ActivityId;
  if (oldActivity == Guid.Empty)
  {
    Trace.CorrelationManager.ActivityId = Guid.NewGuid();
  }
  
  using (LogManager.NDC.Push(Trace.CorrelationManager.ActivityId))
  {
    log.Info("Before calling WCF Service");
  
    wcfService.Method();
  
    log.Info("After calling WCF Service");
  }
  Trace.CorrelationManager.ActivityId = oldActivity;
  </code></pre>
  
  <p>If the log4net/NLog logging format is configured to log the top of the NDC stack, then each message logged by the client (while the activity is in scope) will be "tagged" with the activity id.  Assuming that the WCF service is implemented similarly, then all messages logged during the service call will also be logged (albeit, probably in a separate file) and tagged with the same activity id.  So, it will be possible to correlate the logging messages in the "service" log file to the corresponding messages in the "client" log.</p>
  
  <p>So, if you use WCF and you have logging, here are some questions:</p>
  
  <ol>
  <li>Do you use activity propagation?</li>
  <li>Do you use TraceSources for logging?</li>
  <li>Do you use some other logging platform (e.g. log4net, NLog)?</li>
  <li>If you use another logging platform, how do you do activity propagation?</li>
  <li>Do you use a mixture of third party logging (log4net/NLog - for most logging) and <code>System.Diagnostics.TraceSource</code> (for WCF service boundary logging)?</li>
  </ol>
  
  <p>What about ServiceTraceViewer?  Do you use it?  Most examples I have seen show the output being generated by System.Diagnostics via TraceSources and the XmlTraceListener.  Can it consume output from log4net, NLog, etc?  Does it work "best" with TraceSource-based logging?  If so, is it "good enough" to have just a little bit of TraceSource-based logging at the WCF service boundaries (capturing some app context as well as the WCF communication info) for viewing in ServiceTraceViewer?  I have used ServiceTraceViewer briefly, just as part of my ongoing WCF learning process.  </p>
  
  <p>If you got this far, thanks for reading.  Maybe I am overthinking the whole integration of logging, WCF activity propagation, and the ability to view logs in ServiceTraceViewer.  It seems like an important consideration in the choice of logging platform and/or logging strategies, but I don't have enough experience with these logging platforms or WCF to know for sure.</p>