public override void Execute() { try { // save to db ExternalApiLog apicall = new ExternalApiLog { RequestId = this.Data.RequestId, Request = this.Data.Request, Response = this.Data.Response, Url = this.Data.Url, StatusCode = this.Data.StatusCode, ErrorCode = this.Data.ErrorCode, ErrorMessage = this.Data.ErrorMessage, CreateDate = DateTime.UtcNow, Source = this.Data.Source ?? ExternalAPISource.Other.DescriptionAttr(), Comments = this.Data.Comments, Customer = ObjectFactory.GetInstance <ICustomerRepository>().GetCustomerByRefNum(Data.CustomerRefNum) }; Log.Debug(apicall); this.dataRep.Save(apicall); } catch (Exception ex) { Log.Alert(ex, string.Format("Failed to SaveApiCall requestID: {0}", Data.RequestId)); } }
public void Configure(string cacheString = null) { //TIMEOUT var timeoutPolicy = Policy .TimeoutAsync <HttpResponseMessage>(60); //ECCEZIONI E RESPONSE var retryPolicy = Policy.HandleResult <HttpResponseMessage>(c => { if (c.StatusCode != HttpStatusCode.OK) { var exception = new ExternalApiLog() { Content = c.RequestMessage.Method.Method.Equals("GET") ? c.RequestMessage.RequestUri.Query : c.RequestMessage.Content.ReadAsStringAsync().Result, DataOraEsecuzione = DateTime.Now, Response = string.IsNullOrEmpty(c.Content.ReadAsStringAsync().Result) ? c.ReasonPhrase : c.Content.ReadAsStringAsync().Result, Servizio = c.RequestMessage.RequestUri.Host + c.RequestMessage.RequestUri.LocalPath, CodComando = _httpContext.HttpContext.Request.Headers["codiceSede"], IdOperatore = _httpContext.HttpContext.Request.Headers["IdUtente"] }; _writeLog.Save(exception); switch (c.StatusCode) { case HttpStatusCode.NotFound: throw new Exception(Costanti.ES.ServizioNonRaggiungibile); case HttpStatusCode.Forbidden: throw new Exception(Costanti.ES.AutorizzazioneNegata); case HttpStatusCode.UnprocessableEntity: throw new Exception(Costanti.ES.DatiMancanti); case HttpStatusCode.InternalServerError: throw new Exception(Costanti.ES.ErroreInternoAlServer); case HttpStatusCode.Created: throw new Exception(Costanti.ES.NonTuttiIDatiInviatiSonoStatiProcessati); case HttpStatusCode.UnsupportedMediaType: throw new Exception(Costanti.ES.OggettoNonValido); case 0: throw new Exception(c.ReasonPhrase); } } return(false); }).RetryAsync(3); //CACHE if (!string.IsNullOrEmpty(cacheString)) { var memoryCacheProvider = new MemoryCacheProvider(_memoryCache); var cachePolicy = Policy.CacheAsync(memoryCacheProvider.AsyncFor <HttpResponseMessage>(), TimeSpan.FromHours(8), c => cacheString); policies = Policy.WrapAsync(cachePolicy, retryPolicy, timeoutPolicy); } else { policies = Policy.WrapAsync(retryPolicy, timeoutPolicy); } }
public void Save(ExternalApiLog exception) { _dbContext.ExternalApiLog.InsertOne(exception); }