/// <summary>
/// A method used to log the HTTP transport information.
/// </summary>
/// <param name="wopiRequest">A parameter represents the request instance of a WOPI operation. It must not be null.</param>
/// <param name="requestBody">A parameter represents the request body of a WOPI operation.</param>
/// <param name="wopiResponse">A parameter represents the response of a WOPI operation. It must not be null if the "isResponse" parameter is true.</param>
/// <param name="operationName">A parameter represents the WOPI operation name.</param>
/// <param name="isResponse">A parameter represents the HTTP transport information recorded by this method whether belong to the response of a WOPI operation.</param>
private void LogHttpTransportInfo(HttpWebRequest wopiRequest, byte[] requestBody, WOPIHttpResponse wopiResponse, WOPIOperationName operationName, bool isResponse = true)
{
#region validate parameters
if (isResponse)
{
if (null == wopiResponse)
{
throw new ArgumentNullException("wopiResponse");
}
// For log response, requires the request URL.
if (null == wopiRequest)
{
throw new ArgumentNullException("wopiRequest");
}
}
else
{
if (null == wopiRequest)
{
throw new ArgumentNullException("wopiRequest");
}
}
#endregion
#region headers
// Build headers information
StringBuilder headerInfoBuilder = new StringBuilder();
WebHeaderCollection headers = isResponse ? wopiResponse.Headers : wopiRequest.Headers;
if (null != headers && 0 != headers.Count)
{
foreach (string oheaderNameItem in headers.AllKeys)
{
string headerValueString = string.Format(
"[{0}]:({1})",
oheaderNameItem,
headers[oheaderNameItem]);
headerInfoBuilder.AppendLine(headerValueString);
}
}
#endregion
#region body information
// Build body information
byte[] httpBodyOfResponse = null;
if (isResponse && 0 != wopiResponse.ContentLength)
{
httpBodyOfResponse = WOPIResponseHelper.GetContentFromResponse(wopiResponse);
}
byte[] httpBody = isResponse ? httpBodyOfResponse : requestBody;
StringBuilder bodyInfoBuilder = new StringBuilder();
if (null != httpBody && 0 != httpBody.Length)
{
switch (operationName)
{
case WOPIOperationName.CheckFileInfo:
case WOPIOperationName.ReadSecureStore:
case WOPIOperationName.CheckFolderInfo:
case WOPIOperationName.EnumerateChildren:
{
if (isResponse)
{
// log response body by JSON format
bodyInfoBuilder.AppendLine(Encoding.UTF8.GetString(httpBody));
}
break;
}
case WOPIOperationName.PutRelativeFile:
{
if (isResponse)
{
// log the body by JSON format
bodyInfoBuilder.AppendLine(Encoding.UTF8.GetString(httpBody));
}
else
{
// log the body as bytes string value
bodyInfoBuilder.AppendLine(WOPIResponseHelper.GetBytesStringValue(httpBody));
}
break;
}
case WOPIOperationName.GetFile:
{
if (isResponse)
{
// log the body as bytes string value
bodyInfoBuilder.AppendLine(WOPIResponseHelper.GetBytesStringValue(httpBody));
}
break;
}
case WOPIOperationName.PutFile:
{
if (!isResponse)
{
// log the body as bytes string value
bodyInfoBuilder.AppendLine(WOPIResponseHelper.GetBytesStringValue(httpBody));
}
break;
}
}
}
#endregion
string credentialInfo = string.Format(
"User:[{0}] Domain:[{1}]",
this.defaultUserName,
this.defaultDomain);
string logTitle = string.Format(
"{0} HTTP {1}{2} for [{3}] operation:",
isResponse ? "Receive" : "Sending",
isResponse ? "Response" : "Request",
isResponse ? string.Empty : " with " + credentialInfo,
operationName);
StringBuilder logBuilder = new StringBuilder();
logBuilder.AppendLine(logTitle);
string urlInfor = string.Format("Request URL:[{0}]", wopiRequest.RequestUri.AbsoluteUri);
logBuilder.AppendLine(urlInfor);
string httpMethodValue = string.Format("HTTP method:[{0}]", this.GetHttpMethodForWOPIOperation(operationName));
logBuilder.AppendLine(httpMethodValue);
if (isResponse)
{
string httpStatusCodeValue = string.Format("HTTP status code:[{0}]", wopiResponse.StatusCode);
logBuilder.AppendLine(httpStatusCodeValue);
}
string headerInfo = string.Format("Headers:\r\n{0}", 0 == headerInfoBuilder.Length ? "None" : headerInfoBuilder.ToString());
logBuilder.AppendLine(headerInfo);
string bodyInfo = string.Format("Body:\r\n{0}", 0 == bodyInfoBuilder.Length ? "None" : bodyInfoBuilder.ToString());
logBuilder.AppendLine(bodyInfo);
this.Site.Log.Add(LogEntryKind.Debug, logBuilder.ToString());
}