private static MemoryStream ToLoggedStream(Stream stream, int bufferSize) { // read the entire source stream into a memory stream MemoryStream memoryStream = new MemoryStream(); byte[] buffer = new byte[bufferSize]; int bytesRead = 0; while ((bytesRead = stream.Read(buffer, 0, bufferSize)) > 0) { memoryStream.Write(buffer, 0, bytesRead); } // load the memory stream as an XML document memoryStream.Position = 0; XmlDocument doc = new XmlDocument(); doc.Load(new XmlTextReader(memoryStream)); // write the XML document as formatted text to a second memory stream MemoryStream formatStream = new MemoryStream(); XmlTextWriter writer = new XmlTextWriter(formatStream, new System.Text.UTF8Encoding()); writer.Formatting = Formatting.Indented; writer.Indentation = 2; writer.IndentChar = ' '; doc.WriteTo(writer); writer.Flush(); // read the formatted XML into a string and log formatStream.Position = 0; StreamReader streamReader = new StreamReader(formatStream, new System.Text.UTF8Encoding()); AgsLogger.Log(streamReader.ReadToEnd(), "Response"); // position the memory stream buffer for subsequent XML reading memoryStream.Position = 0; return(memoryStream); }
public override void Flush() { base.Flush(); _logWriter.Flush(); // the caller does not call Close(), Dispose() or WriteEndDocument() to signal the end of // writing; however it appears to call Flush() just twice, so log on the second flush // TODO: check if this applies to longer requests if (++_flushCount == 2) { // read the formatted XML from the buffer into a string and log _memoryStream.Position = 0; StreamReader streamReader = new StreamReader(_memoryStream, new System.Text.UTF8Encoding()); AgsLogger.Log(streamReader.ReadToEnd(), "Request"); this.RequestXml = streamReader.ReadToEnd(); // reset the position in the buffer, just in case more data needs to be written _memoryStream.Position = _memoryStream.Length - 1; } }