public async Task <T> Execute <T>(Func <Task <T> > func, IDictionary <string, object> logData = null) { T retval; var isError = false; logData = logData ?? new Dictionary <string, object>(); var metric = new MetricWatcher(Constants.MetricWcfClient, new MetricWatcherOption { ManualStartStop = true, LoggingOption = LogOptionEnum.FullLog, LogMessage = logData }); try { metric.Start(); retval = await func(); } catch (Exception ex) { isError = true; logData.Add("WCFException", ex); throw; } finally { metric.Stop(isError); } return(retval); }
/// <summary> /// /// </summary> /// <param name="logData">additional logging to metricwatcher</param> /// <returns></returns> public async Task <DbDataReader> ExecuteReaderAsync(IDictionary <string, object> logData = null) { var isError = false; var metric = new MetricWatcher(Constants.MetricDataClient, new MetricWatcherOption { ManualStartStop = true, LoggingOption = LogOptionEnum.FullLog, LogMessage = BuildLogMessage(logData) }); try { metric.Start(); return(await Command.ExecuteReaderAsync()); } catch (Exception ex) { isError = true; Log.Error(new LogObject(CommonEventType.SqlDatabaseEx.GetDescription(), BuildLogMessage(logData)), ex); throw; } finally { metric.Stop(isError); } }
public async Task <TResponse> PutAsync <TResponse, TRequest>(Uri uri, TRequest data, MediaTypeFormatter mediaTypeFormatter, LogOptionEnum logOption = LogOptionEnum.FullLog, IDictionary <string, object> logMessage = null) { logMessage = logMessage ?? new Dictionary <string, object>(); logMessage.Add("RemoteRoute", uri.AbsoluteUri); logMessage.Add("Verb", "PUT"); var metric = new MetricWatcher(Constants.MetricClient, new MetricWatcherOption { ManualStartStop = true, LogMessage = logMessage }); var isException = false; var client = await SetupClient(); TResponse responseData; try { metric.Start(); var content = new ObjectContent <TRequest>(data, mediaTypeFormatter); var response = await client.PutAsync(uri, content); EnsureSuccessStatusCode(response); responseData = await response.Content.ReadAsAsync <TResponse>(new[] { mediaTypeFormatter }); } catch (Exception) { isException = true; throw; } finally { metric.Stop(isException); } return(responseData); }
private async Task <CaseResponse> UpdateAsync(SalesforceEventTypeEnum eventType, string sObject, string caseId, object record, ICaseClientProxy client = null, IDictionary <string, object> logData = null) { ProxySuccessResponse response = null; CaseResponse caseResponse = null; var isError = false; var metric = new MetricWatcher(eventType.ToString(), new MetricWatcherOption { ManualStartStop = true, LoggingOption = LogOptionEnum.FullLog, LogMessage = logData }); try { metric.Start(); if (client == null) { client = await CaseService.GetUserNamePasswordForceClientAsync(ClientEnum.Header); } response = await client.UpdateAsync(sObject, caseId, record); } catch (Exception ex) { isError = true; if (ex is ForceException) { throw new ExternalErrorException(ex.GetExceptionMessages(), new LogObject(eventType.ToString(), logData), ex); } throw; } finally { if (response != null) { response.Id = caseId; caseResponse = await HandleCaseResponse(client, response, isError, metric); } metric.Stop(isError); } return(caseResponse); }
//protected virtual async Task<string> GetCustomerId(string accountKey) //{ // ProxySuccessResponse response = null; // var isError = false; // var metric = new MetricWatcher(SalesforceEventTypeEnum.CreateCustomer.ToString(), // new MetricWatcherOption // { // ManualStartStop = true, // LoggingOption = LogOptionEnum.FullLog, // LogMessage = new Dictionary<string, object> // { // {"AccountKey", accountKey}, // {"EventType", SalesforceEventTypeEnum.CreateCustomer} // } // }); // try // { // metric.Start(); // if (string.IsNullOrWhiteSpace(accountKey)) // { // return ""; // } // var customer = // await QueryObjectAsync<Customer__c>(new Dictionary<string, object> {{ "AccountKey__c", accountKey}}, "Id"); // if (customer != null && customer.Records.Count > 0) // { // return customer.Records[0].Id; // } // var customerNew = new Customer__c() // { // Name = accountKey, // AccountKey__c = accountKey // }; // var client = await CaseService.GetUserNamePasswordForceClientAsync(ClientEnum.Default); // response = await client.CreateAsync("Customer__c", customerNew); // if (response != null && response.Success) // { // return response.Id; // } // } // catch (Exception ex) // { // isError = true; // if (ex is ForceException) // { // throw new ExternalErrorException(ex.GetExceptionMessages(), // new LogObject(SalesforceEventTypeEnum.CreateCustomer.ToString(), // new Dictionary<string, object> // { // {"AccountKey", accountKey}, // {"EventType", SalesforceEventTypeEnum.CreateCustomer} // })); // } // } // finally // { // if (response != null // && !isError) // { // metric.Options.LogMessage = metric.Options.LogMessage.Merge(new Dictionary<string, object> // { // {"Id", response.Id}, // {"EventType", SalesforceEventTypeEnum.CreateCustomer}, // {"Error", response.Errors} // }); // } // metric.Stop(isError); // } // return ""; //} #endregion #region Get / Create Customer Object public async Task <Account> GetPersonAccount(PersonAccountInput input, ICaseClientProxy client = null) { ProxySuccessResponse response = null; var isError = false; var logData = new Dictionary <string, object> { { "AccountIdentifier", input.AccountIdentifier }, { "EventType", SalesforceEventTypeEnum.CreatePersonAccount } }; var metric = new MetricWatcher(SalesforceEventTypeEnum.CreatePersonAccount.ToString(), new MetricWatcherOption { ManualStartStop = true, LoggingOption = LogOptionEnum.FullLog, LogMessage = logData }); try { metric.Start(); if (client == null) { client = await CaseService.GetUserNamePasswordForceClientAsync(ClientEnum.Default); } if (string.IsNullOrWhiteSpace(input.AccountIdentifier)) { return(null); } var account = await QueryObjectAsync <Account>( new Dictionary <string, object> { { "Account_Identifier__c", input.AccountIdentifier } }, "Id", "PersonContactId"); if (account != null && account.Records.Count > 0) { return(account.Records[0]); } if (string.IsNullOrEmpty(input.LastName)) { return(null); } var waveRecordTypeId = await GetRecordTypeId("WaveAccount"); var personAccount = new Account { LastName = input.LastName, FirstName = input.FirstName, RecordTypeId = waveRecordTypeId, Account_Identifier__c = input.AccountIdentifier, Product__c = input.Product, Brand__c = input.Brand }; response = await client.CreateAsync("Account", personAccount); if (response != null && response.Success) { var newAccount = await QueryObjectAsync <Account>(new Dictionary <string, object> { { "ID", response.Id } }, "PersonContactId"); var contactId = string.Empty; if (newAccount != null && newAccount.Records.Count > 0) { contactId = newAccount.Records[0].PersonContactId; } return(new Account { Id = response.Id, PersonContactId = contactId }); } } catch (Exception ex) { isError = true; var log = new LogObject(SalesforceEventTypeEnum.CreatePersonAccount.ToString(), logData); if (ex is ForceException) { throw new ExternalErrorException(ex.GetExceptionMessages(), log, ex); } throw new GdErrorException("Unable to get Person Account data", log, ErrorCodeEnum.ExternalError, ex); } finally { if (response != null && isError) { metric.Options.LogMessage = metric.Options.LogMessage.Merge(new Dictionary <string, object> { { "Id", response.Id }, { "EventType", SalesforceEventTypeEnum.CreatePersonAccount }, { "Error", response.Errors } }); } metric.Stop(isError); } return(null); }