public override void Flush() { if (!string.IsNullOrEmpty(UnwrittenCharactersFromPreviousCall)) { string finalContentToWrite = UnwrittenCharactersFromPreviousCall; if (BodyEndRegex.IsMatch(UnwrittenCharactersFromPreviousCall)) { // apparently we did seem to match a </body> tag, which means we can replace the last match with our HTML snippet finalContentToWrite = BodyEndRegex.Replace(UnwrittenCharactersFromPreviousCall, HtmlSnippet, 1); } else { // there was no </body> tag found, so we write down a warning to the log Logger.Warn("Unable to locate '</body>' with content encoding '{0}' for request '{1}'. The response may be compressed or the markup may actually be missing a '</body>' tag. See {2} for information on troubleshooting this issue.", ContentEncoding.EncodingName, CurrentRequestRawUrl, TroubleshootingDocsUri); } // either way, if a replacement has been done or a warning has been written to the logs, the remaining unwritten characters must be written to the output stream WriteToOutputStream(finalContentToWrite); } OutputStream.Flush(); }