Log RestClient requests similarly to WebClient (e.g, "debug http all 6" logs the entire request and response)

0.8.0.3
Oren Hurvitz 2014-07-13 19:19:58 +03:00 committed by Justin Clark-Casey
parent 37fcc827e2
commit 9a2dc6a0d7
2 changed files with 31 additions and 7 deletions

View File

@ -347,6 +347,10 @@ namespace OpenSim.Framework.Communications
if (auth != null) if (auth != null)
auth.AddAuthorization(_request.Headers); auth.AddAuthorization(_request.Headers);
int reqnum = WebUtil.RequestNumber++;
if (WebUtil.DebugLevel >= 3)
m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} REST {1} to {2}", reqnum, _request.Method, _request.RequestUri);
// IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request); // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
try try
{ {
@ -393,6 +397,9 @@ namespace OpenSim.Framework.Communications
_resource.Seek(0, SeekOrigin.Begin); _resource.Seek(0, SeekOrigin.Begin);
} }
if (WebUtil.DebugLevel >= 5)
WebUtil.LogResponseDetail(reqnum, _resource);
return _resource; return _resource;
} }
} }
@ -409,16 +416,18 @@ namespace OpenSim.Framework.Communications
if (auth != null) if (auth != null)
auth.AddAuthorization(_request.Headers); auth.AddAuthorization(_request.Headers);
// m_log.DebugFormat("[REST]: Request Length {0}", _request.ContentLength);
// m_log.DebugFormat("[REST]: Sending Web Request {0}", buildUri());
src.Seek(0, SeekOrigin.Begin); src.Seek(0, SeekOrigin.Begin);
// m_log.Debug("[REST]: Seek is ok");
int reqnum = WebUtil.RequestNumber++;
if (WebUtil.DebugLevel >= 3)
m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} REST {1} to {2}", reqnum, _request.Method, _request.RequestUri);
if (WebUtil.DebugLevel >= 5)
WebUtil.LogOutgoingDetail(string.Format("SEND {0}: ", reqnum), src);
Stream dst = _request.GetRequestStream(); Stream dst = _request.GetRequestStream();
// m_log.Debug("[REST]: GetRequestStream is ok");
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int length = src.Read(buf, 0, 1024); int length = src.Read(buf, 0, 1024);
// m_log.Debug("[REST]: First Read is ok");
while (length > 0) while (length > 0)
{ {
dst.Write(buf, 0, length); dst.Write(buf, 0, length);
@ -433,14 +442,29 @@ namespace OpenSim.Framework.Communications
{ {
m_log.WarnFormat("[REST]: Request {0} {1} failed with status {2} and message {3}", m_log.WarnFormat("[REST]: Request {0} {1} failed with status {2} and message {3}",
RequestMethod, _request.RequestUri, e.Status, e.Message); RequestMethod, _request.RequestUri, e.Status, e.Message);
return null;
} }
catch (Exception e) catch (Exception e)
{ {
m_log.WarnFormat( m_log.WarnFormat(
"[REST]: Request {0} {1} failed with exception {2} {3}", "[REST]: Request {0} {1} failed with exception {2} {3}",
RequestMethod, _request.RequestUri, e.Message, e.StackTrace); RequestMethod, _request.RequestUri, e.Message, e.StackTrace);
return null;
} }
if (WebUtil.DebugLevel >= 5)
{
using (Stream responseStream = _response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
string responseStr = reader.ReadToEnd();
WebUtil.LogResponseDetail(reqnum, responseStr);
}
}
}
_response.Close();
// IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request); // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);

View File

@ -69,7 +69,7 @@ namespace OpenSim.Framework
/// <summary> /// <summary>
/// Request number for diagnostic purposes. /// Request number for diagnostic purposes.
/// </summary> /// </summary>
public static int RequestNumber { get; internal set; } public static int RequestNumber { get; set; }
/// <summary> /// <summary>
/// Control where OSD requests should be serialized per endpoint. /// Control where OSD requests should be serialized per endpoint.