コード例 #1
0
        /// <summary>
        /// Start cosmosdb process.
        /// </summary>
        /// <param name="serverlessLogger">Logger to save log.</param>
        /// <param name="logger">Logger to be injected in <paramref name="serverlessLogger"/>.</param>
        /// <param name="patternLog">Pattern log.</param>
        /// <param name="documents">Documents to be processed.</param>
        /// <param name="func">Process with business rules to be processed.</param>
        /// <returns>Task executed.</returns>
        public static async Task StartDocumentProcessAsync(
            IServerlessLogger serverlessLogger,
            ILogger logger,
            string patternLog,
            IReadOnlyList <Document> documents,
            Func <Document, Task> func)
        {
            try
            {
                serverlessLogger.InjectLogger(logger);

                if (documents.Count == 0)
                {
                    serverlessLogger.LogWarning($"{patternLog} No document to process.");
                    return;
                }

                serverlessLogger.LogInformation($"{patternLog} Start processing...");

                var documentCount = documents.Count;

                for (int documentIndex = 0; documentIndex < documentCount; documentIndex++)
                {
                    var document = documents[documentIndex];
                    serverlessLogger.LogInformation($"{patternLog} Processing document index: {documentIndex}, id: {document.Id} and resourceId: {document.ResourceId}!");
                    await func(document).ConfigureAwait(false);

                    serverlessLogger.LogInformation($"{patternLog} Document index {documentIndex} processed!");
                }

                serverlessLogger.LogInformation($"{patternLog} Processing completed!");
            }
            catch (Exception ex)
            {
                serverlessLogger.LogError(ex, $"{patternLog} Processing failed!");
                throw;
            }
        }
        /// <summary>
        /// Start event hub process.
        /// </summary>
        /// <param name="serverlessLogger">Logger to save log.</param>
        /// <param name="logger">Logger to be injected in <paramref name="serverlessLogger"/>.</param>
        /// <param name="patternLog">Pattern log.</param>
        /// <param name="events">Events to be processed.</param>
        /// <param name="func">Process with business rules to be processed.</param>
        /// <returns>Task executed.</returns>
        public static async Task StartEventProcessAsync(
            IServerlessLogger serverlessLogger,
            ILogger logger,
            string patternLog,
            EventData[] events,
            Func <EventData, Task> func)
        {
            try
            {
                serverlessLogger.InjectLogger(logger);

                if (events.Length == 0)
                {
                    serverlessLogger.LogWarning($"{patternLog} No event to process.");
                    return;
                }

                serverlessLogger.LogInformation($"{patternLog} Start processing...");

                var eventCount = events.Length;

                for (int eventIndex = 0; eventIndex < eventCount; eventIndex++)
                {
                    serverlessLogger.LogInformation($"{patternLog} Processing event index {eventIndex}...");
                    await func(events[eventIndex]).ConfigureAwait(false);

                    serverlessLogger.LogInformation($"{patternLog} Event index {eventIndex} processed!");
                }

                serverlessLogger.LogInformation($"{patternLog} Processing completed!");
            }
            catch (Exception ex)
            {
                serverlessLogger.LogError(ex, $"{patternLog} Processing failed!");
                throw;
            }
        }
コード例 #3
0
        public override async Task InvokeAsync(IHttpFunctionContext context)
        {
            logger.LogInformation($"[{nameof(CreateCustomerFunctionMiddleware)}] HTTP function processing a request.");

            var requestModel = await req.ExtractRequestBody <CreateCustomerRequestModel>()
                               .ConfigureAwait(false);

            if (!ValidateRequest(requestModel))
            {
                context.Response = badRequestPresenter.ResponseModel;
                return;
            }

            var command = mapper.Map <CreateCustomerCommand>(requestModel);

            var result = await mediator.Send(command)
                         .ConfigureAwait(false);

            presenter.Populate(result);

            context.Response = presenter.ResponseModel;

            logger.LogInformation($"[{nameof(CreateCustomerFunctionMiddleware)}] HTTP function processed a request.");
        }
コード例 #4
0
        private void CreateLog(Message response, DateTimeOffset endProcess, bool isFault)
        {
            try
            {
                var xrdr         = response.GetReaderAtBodyContents();
                var responseText = xrdr.ReadOuterXml();

                var log = new
                {
                    type           = "Third Party",
                    date           = DateTimeOffset.UtcNow,
                    correlationId  = transactionFlow.CorrelationId.ToString(),
                    startTimestamp = startProcess,
                    endTimestamp   = endProcess,
                    duration       = Convert.ToDecimal(Math.Round((endProcess - startProcess).TotalMilliseconds, 2, MidpointRounding.AwayFromZero)),
                    authToken      = transactionFlow.Token,
                    culture        = transactionFlow.Culture,
                    url,
                    machine     = transactionFlow.Machine,
                    hostIp      = transactionFlow.HostIp,
                    localIp     = transactionFlow.LocalIp,
                    serviceType = "wcf",
                    request     = requestText,
                    response    = responseText,
                    method,
                    state            = response.State.ToString(),
                    hasError         = isFault,
                    customerDocument = transactionFlow.CustomerDocument
                };

                Task.Factory.StartNew(() =>
                {
                    var logJson = Serialize(log);
                    logger.LogInformation(
                        logJson,
                        url,
                        requestText,
                        responseText,
                        transactionFlow);
                });
            }
            catch (Exception ex)
            {
                logger.LogError($"Fail to log third party wcf: {ex.ToString()}");
            }
        }
コード例 #5
0
        /// <inheritdoc/>
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            HttpResponseMessage response = null;
            var requestText  = string.Empty;
            var responseText = string.Empty;

            if (request != null && request.Content != null)
            {
                requestText = await request.Content.ReadAsStringAsync().ConfigureAwait(false);
            }

            try
            {
                var startProcess = DateTimeOffset.UtcNow;
                response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

                var endProcess = DateTimeOffset.UtcNow;

                if (response.Content != null)
                {
                    responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                }

                var log = CreateLog(
                    startProcess,
                    endProcess,
                    request,
                    response,
                    requestText,
                    responseText);

                await Task.Run(() =>
                {
                    serverlessLogger.LogInformation(Serialize(log));
                }).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                serverlessLogger.LogError(ex, $"{nameof(ServerlessLogger)}.{nameof(LoggingHandler)} -> Fail to log Third Party: {ex.Message}");
            }

            return(response);
        }
        /// <summary>
        /// End event process logging notification messages.
        /// </summary>
        /// <typeparam name="TResult">Type of result data.</typeparam>
        /// <param name="result">Result data. If the object is null, throw exception. Otherwise, the event was processed successfully.</param>
        /// <param name="correlationId">Correlation id.</param>
        /// <param name="notificationUseCase">Use case to extract notifications.</param>
        /// <param name="serverlessLogger">Logger to save log.</param>
        /// <param name="patternLog">Pattern log.</param>
        public static void EndEventProcess <TResult>(
            TResult result,
            string correlationId,
            DomainNotificationUseCase notificationUseCase,
            IServerlessLogger serverlessLogger,
            string patternLog)
            where TResult : IResult
        {
            if (result == null)
            {
                if (notificationUseCase.HasNotifications())
                {
                    var errors = GetAllMessageErrors(correlationId, notificationUseCase, patternLog);

                    throw new Exception(errors);
                }

                throw new Exception($"{patternLog} Event item with {nameof(correlationId)}: {correlationId} processing failed.");
            }

            serverlessLogger.LogInformation($"{patternLog} Event item with {nameof(correlationId)}: {correlationId} successfully processed!");
        }