Esempio n. 1
0
        public static R LogStartEndAndElapsedTime <T, R>(this ILogger <T> logger,
                                                         EventIds startEventId,
                                                         EventIds completedEventId,
                                                         string messageFormat,
                                                         Func <R> func,
                                                         params object[] messageArguments)
        {
            logger.LogInformation(startEventId.ToEventId(), messageFormat, messageArguments);

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            try
            {
                return(func());
            }
            finally
            {
                stopwatch.Stop();
                logger.LogInformation(completedEventId.ToEventId(),
                                      messageFormat + " Elapsed {Elapsed}",
                                      messageArguments.Concat(new object[] { stopwatch.Elapsed }).ToArray());
            }
        }
Esempio n. 2
0
        public static async Task <TResult> LogStartEndAndElapsedTimeAsync <T, TResult>(this ILogger <T> logger,
                                                                                       EventIds startEventId,
                                                                                       EventIds completedEventId,
                                                                                       string messageFormat,
                                                                                       Func <Task <TResult> > func,
                                                                                       params object[] messageArguments)
        {
            logger.LogInformation(startEventId.ToEventId(), messageFormat, messageArguments);

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var result = await func();

            stopwatch.Stop();
            logger.LogInformation(completedEventId.ToEventId(),
                                  messageFormat + " Elapsed {Elapsed}",
                                  messageArguments.Concat(new object[] { stopwatch.Elapsed }).ToArray());

            return(result);
        }
Esempio n. 3
0
 public static IAsyncPolicy <HttpResponseMessage> GetRetryPolicy(ILogger logger, string requestType, EventIds eventId, int retryCount, double sleepDuration)
 {
     return(Policy
            .HandleResult <HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.ServiceUnavailable)
            .OrResult(r => r.StatusCode == HttpStatusCode.TooManyRequests)
            .OrResult(r => r.StatusCode == HttpStatusCode.InternalServerError && requestType == "File Share")
            .WaitAndRetryAsync(retryCount, (retryAttempt) =>
     {
         return TimeSpan.FromSeconds(Math.Pow(sleepDuration, (retryAttempt - 1)));
     }, async(response, timespan, retryAttempt, context) =>
     {
         var retryAfterHeader = response.Result.Headers.FirstOrDefault(h => h.Key.ToLowerInvariant() == "retry-after");
         var correlationId = response.Result.RequestMessage.Headers.FirstOrDefault(h => h.Key.ToLowerInvariant() == "x-correlation-id");
         int retryAfter = 0;
         if (response.Result.StatusCode == HttpStatusCode.TooManyRequests && retryAfterHeader.Value != null && retryAfterHeader.Value.Any())
         {
             retryAfter = int.Parse(retryAfterHeader.Value.First());
             await Task.Delay(TimeSpan.FromMilliseconds(retryAfter));
         }
         logger
         .LogInformation(eventId.ToEventId(), "Re-trying {requestType} service request with uri {RequestUri} and delay {delay}ms and retry attempt {retry} with _X-Correlation-ID:{correlationId} as previous request was responded with {StatusCode}.",
                         requestType, response.Result.RequestMessage.RequestUri, timespan.Add(TimeSpan.FromMilliseconds(retryAfter)).TotalMilliseconds, retryAttempt, correlationId.Value, response.Result.StatusCode);
     }));
 }