Revision 5120f3cb trunk/Pithos.Network/RestClient.cs

b/trunk/Pithos.Network/RestClient.cs
11 11
using System.Net;
12 12
using System.Runtime.Serialization;
13 13
using System.Threading.Tasks;
14
using log4net;
14 15

  
15 16
namespace Pithos.Network
16 17
{
......
47 48
            }            
48 49
        }
49 50

  
51
        private static readonly ILog Log = LogManager.GetLogger("RestClient");
52

  
53

  
50 54
        [ContractInvariantMethod]
51 55
        private void Invariants()
52 56
        {
......
104 108

  
105 109
        protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
106 110
        {
107
            var response = (HttpWebResponse) base.GetWebResponse(request, result);            
108
            StatusCode=response.StatusCode;
109
            StatusDescription=response.StatusDescription;
110
            return response;
111
            return ProcessResponse(()=>base.GetWebResponse(request, result));
111 112
        }
112 113

  
113
        private readonly List<HttpStatusCode> _allowedStatusCodes=new List<HttpStatusCode>{HttpStatusCode.NotModified};
114
        public List<HttpStatusCode> AllowedStatusCodes
114
        protected override WebResponse GetWebResponse(WebRequest request)
115 115
        {
116
            get
117
            {
118
                return _allowedStatusCodes;
119
            }            
116
            return ProcessResponse(() => base.GetWebResponse(request));
120 117
        }
121 118

  
122
        protected override WebResponse GetWebResponse(WebRequest request)
119
        private WebResponse ProcessResponse(Func<WebResponse> getResponse)
123 120
        {
124 121
            try
125
            {                
126
                var response = (HttpWebResponse)base.GetWebResponse(request);
122
            {
123
                var response = (HttpWebResponse)getResponse();
127 124
                StatusCode = response.StatusCode;
128
                LastModified=response.LastModified;                
129
                StatusDescription = response.StatusDescription;                
125
                LastModified = response.LastModified;
126
                StatusDescription = response.StatusDescription;
130 127
                return response;
131 128
            }
132 129
            catch (WebException exc)
133
            {                
134
                if (exc.Response!=null)
130
            {
131
                if (exc.Response != null)
135 132
                {
136 133
                    var response = (exc.Response as HttpWebResponse);
137 134
                    if (AllowedStatusCodes.Contains(response.StatusCode))
......
145 142
                    if (exc.Response.ContentLength > 0)
146 143
                    {
147 144
                        string content = GetContent(exc.Response);
148
                        Trace.TraceError(content);
145
                        Log.ErrorFormat(content);
149 146
                    }
150 147
                }
151 148
                throw;
152 149
            }
153 150
        }
154 151

  
152
        private readonly List<HttpStatusCode> _allowedStatusCodes=new List<HttpStatusCode>{HttpStatusCode.NotModified};
153
        public List<HttpStatusCode> AllowedStatusCodes
154
        {
155
            get
156
            {
157
                return _allowedStatusCodes;
158
            }            
159
        }
160

  
155 161
        public DateTime LastModified { get; private set; }
156 162

  
157 163
        private static string GetContent(WebResponse webResponse)
......
263 269
                var exc = ex.InnerException;
264 270
                if (exc is RetryException)
265 271
                {
266
                    Trace.TraceError("[{0}] RETRY FAILED for {1} after {2} retries",method,address,retries);
272
                    Log.ErrorFormat("[{0}] RETRY FAILED for {1} after {2} retries",method,address,retries);
267 273
                }
268 274
                else
269 275
                {
270
                    Trace.TraceError("[{0}] FAILED for {1} with \n{2}", method, address, exc);
276
                    Log.ErrorFormat("[{0}] FAILED for {1} with \n{2}", method, address, exc);
271 277
                }
272 278
                throw;
273 279

  
274 280
            }
275 281
            catch(Exception ex)
276 282
            {
277
                Trace.TraceError("[{0}] FAILED for {1} with \n{2}", method, address, ex);
283
                Log.ErrorFormat("[{0}] FAILED for {1} with \n{2}", method, address, ex);
278 284
                throw;
279 285
            }
280 286
        }
281 287
        
282 288
        private static void TraceStart(string method, string actualAddress)
283 289
        {
284
            Trace.WriteLine(String.Format("[{0}] {1} {2}", method, DateTime.Now, actualAddress));
290
            Log.InfoFormat("[{0}] {1} {2}", method, DateTime.Now, actualAddress);
285 291
        }
286 292

  
287 293
        private string GetActualAddress(string address)
......
396 402
                                TimedOut = true;
397 403
                                if (retryCount == 0)
398 404
                                {                                    
399
                                    Trace.TraceError("[ERROR] Timed out too many times. \n{0}\n",e);
405
                                    Log.ErrorFormat("[ERROR] Timed out too many times. \n{0}\n",e);
400 406
                                    tcs.SetException(new RetryException("Timed out too many times.", e));                                    
401 407
                                }
402 408
                                else
403 409
                                {
404
                                    Trace.TraceError(
410
                                    Log.ErrorFormat(
405 411
                                        "[RETRY] Timed out after {0} ms. Will retry {1} more times\n{2}", Timeout,
406 412
                                        retryCount, e);
407 413
                                    Retry(original, retryCount - 1, tcs);

Also available in: Unified diff