Beispiel #1
0
        public decimal CalculateMargin(decimal lineCost, decimal linePrice, ICounterMeasure counter)
        {
            counter.Increment();
            var margin = (linePrice - lineCost) / linePrice;

            return(margin);
        }
        public async Task <TResponse> Handle(CommandEnvelope <TCommand, TResponse> message,
                                             RequestHandlerDelegate <TResponse> next)
        {
            var context = message.Context;

            try
            {
                return(await next.Invoke());
            }
            catch (Exception ex)
            {
                _counter.Increment();
                var level = ex is UserException ? LogEventLevel.Warning : LogEventLevel.Error;
                _logger.Write(level, ex, "[{TraceId}-{UserId}] {Message}",
                              context.Metadata.TraceId,
                              context.UserId == null ? "Anonymous" : context.UserId.ToString(),
                              ex.Message);
                throw;
            }
        }
        public async Task <TResponse> Handle(CommandEnvelope <TCommand, TResponse> message,
                                             RequestHandlerDelegate <TResponse> next)
        {
            var context = message.Context;
            var diagnosticProperties = new ILogEventEnricher[]
            {
                new PropertyEnricher("TraceId", context.Metadata.TraceId),
                new PropertyEnricher("UserId", context.UserId),
                new PropertyEnricher("ConnectionId", context.Connection.Id),
                new PropertyEnricher("CommandType", message.Command.GetType().Name)
            };

            using (LogContext.Push(diagnosticProperties))
            {
                try
                {
                    _received.Increment();
                    _logger.Debug("[Handler:{TraceId}:{UserId}] Received {CommandType}: {@Command}",
                                  context.Metadata.TraceId,
                                  context.UserId,
                                  message.Command.GetType().Name,
                                  message.Command);
                    TResponse response;
                    using (_logger.BeginTimedOperation(OperationDescription))
                    {
                        response = await next.Invoke();
                    }
                    _logger.Debug("[Handler:{TraceId}:{UserId}] Handled {CommandType} successfully: {@Response}",
                                  context.Metadata.TraceId,
                                  context.UserId,
                                  message.Command.GetType().Name,
                                  response);
                    return(response);
                }
                finally
                {
                    _processed.Increment();
                }
            }
        }
        public async Task Handle(IBotNotification <IClientEvent> notification)
        {
            var context = notification.Context;
            var @event  = notification.Event;

            _processed = _processed ??
                         context.Logger.CountOperation(ProcessedCounterName, resolution: ProcessedCounterResolution);
            var diagnosticProperties = new ILogEventEnricher[]
            {
                new PropertyEnricher("ConnectionId", context.Connection.Id),
                new PropertyEnricher("BotId", context.BotId),
                new PropertyEnricher("UserId", context.UserId),
                new PropertyEnricher("EventType", notification.Event.GetType().Name)
            };

            using (LogContext.Push(diagnosticProperties))
            {
                context.Logger.Debug("[Bot:{BotId}:{UserId}] Started to handle {EventType}.",
                                     context.BotId,
                                     context.UserId,
                                     @event.GetType().Name);
                try
                {
                    using (context.Logger.BeginTimedOperation(OperationDescription))
                    {
                        await _next.Handle(notification);
                    }
                }
                finally
                {
                    _processed.Increment();
                    context.Logger.Debug("[Bot:{BotId}:{UserId}] Finished to handle {EventType}.",
                                         context.BotId,
                                         context.UserId,
                                         @event.GetType().Name);
                }
            }
        }
Beispiel #5
0
        private void Handle(IClientEvent @event)
        {
            EnsureIsNotDisposed();
            if (@event == null)
            {
                _context.Logger.Error("[Bot:{BotId}:{UserId}] Received null event.", _context.BotId, _context.UserId);
                return;
            }
            if (_state == BotState.Stopped)
            {
                throw new InvalidOperationException("Bot is stopped.");
            }
            _received.Increment();
            _queueSize.Write();
            _context.Logger.Debug("[Bot:{BotId}:{UserId}]: Received {EventType} {@Event}",
                                  _context.BotId,
                                  _context.UserId,
                                  @event.GetType().Name,
                                  @event);
            if (_cancellationToken.IsCancellationRequested)
            {
                _context.Logger.Warning("[Bot:{BotId}:{UserId}] Bot execution cancelled.");
                Stop(new TaskCanceledException("Bot execution was cancelled."));
                return;
            }
            var posted = _processor.Post(@event);

            if (!posted)
            {
                _context.Logger.Warning("[Bot:{BotId}:{UserId}] Failed to accept {EventType}. Queue length exceeded.",
                                        _context.BotId,
                                        _context.UserId,
                                        @event.GetType().Name);
                Stop(new InvalidOperationException("Bot queue is too large."));
            }
        }
Beispiel #6
0
 public decimal CalculateLineCost(short qty, decimal unitCost, ICounterMeasure counter)
 {
     Thread.Sleep(500);
     counter.Increment();
     return(qty * unitCost);
 }
 public decimal CalculateMargin(decimal lineCost, decimal linePrice, ICounterMeasure counter)
 {
     counter.Increment();
     var margin = (linePrice - lineCost) / linePrice;
     return margin;
 }
 public decimal CalculateLinePrice(short qty, decimal unitPrice, ICounterMeasure counter)
 {
     Thread.Sleep(500);
     counter.Increment();
     return qty * unitPrice;
 }