title: C# Stream Pipe (Stream Spy)
body: <p>I have an input stream and a stream reader component. This works fine but now I want to log all the traffic (save a copy in a file). 
  So I need to spy on a stream. A solution I am thinking of is a stream pipe (pipeline) or a stream wrapper that takes a stream as input and then gives me a first look at the traffic. Something like this:</p>
  
  <pre><code>    void Init(System.Net.Sockets.NetworkStream stream)
      {
          System.IO.Stream wrappedStream = new MyWrapper(stream);
          wrappedStream.ReadSpy = MyMethod;
          XmlReader reader = XmlReader.Create(wrappedStream);
      }
  
      // This will get called after some bytes have been read from a stream,
      // but before they get passed to the XmlReader
      byte[] MyMethod(byte[] buffer)
      {
          m_Writer.Write(buffer); // write to a file 
          return buffer; // Give to XmlReader 
      }
  </code></pre>