private void LogToApplicationInsights(ExceptionLoggerContext context)
        {
            var customProperties = new Dictionary <string, string>();

            if (context.Request != null)
            {
                var correlationId = context.Request.GetCorrelationId();
                customProperties.Add("CorrelationId", correlationId.ToString());
            }

            var exceptionMessage = context.Exception.Message;

            customProperties.Add("ExceptionMessage", exceptionMessage);

            applicationInsightsTelemetry.TrackException(context.Exception, customProperties);
        }
Exemple #2
0
        public override Task RunAsync(CancellationToken cancellationToken)
        {
            return(Task.Factory.StartNew(async() =>
            {
                Exception globalEx = null;
                try
                {
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        IQueueMessage message = null;
                        bool executionSucceeded = false;
                        try
                        {
                            do
                            {
                                message = await _queueReader.GetMessageAsync();
                                if (message == null)
                                {
                                    break;
                                }

                                var context = new QueueTriggeringContext(message.InsertionTime);

                                var p = new List <object>()
                                {
                                    message.Value(_parameterType)
                                };

                                if (_hasSecondParameter)
                                {
                                    p.Add(_useTriggeringContext ? context : (object)message.InsertionTime);
                                }

                                var telemtryOperation = ApplicationInsightsTelemetry.StartRequestOperation($"{nameof(QueueTriggerBinding)} from {_queueName}");
                                try
                                {
                                    await Invoke(_serviceProvider, _method, p.ToArray());
                                }
                                catch (Exception ex)
                                {
                                    ApplicationInsightsTelemetry.MarkFailedOperation(telemtryOperation);
                                    ApplicationInsightsTelemetry.TrackException(ex);
                                    throw;
                                }
                                finally
                                {
                                    ApplicationInsightsTelemetry.StopOperation(telemtryOperation);
                                }
                                await ProcessCompletedMessage(message, context);
                                executionSucceeded = true;
                            } while (!cancellationToken.IsCancellationRequested);
                        }
                        catch (Exception ex)
                        {
                            await LogError("QueueTriggerBinding", "RunAsync", ex);
                            await ProcessFailedMessage(message);
                            executionSucceeded = false;
                        }
                        finally
                        {
                            await Task.Delay(_delayStrategy.GetNextDelay(executionSucceeded), cancellationToken);
                        }
                    }
                }
                catch (Exception ex)
                {
                    globalEx = ex;
                }
                finally
                {
                    var msg =
                        $"Process ended. Exception={globalEx?.Message + globalEx?.StackTrace}. Token.IsCancellationRequested={cancellationToken.IsCancellationRequested}";
                    await _log.WriteInfoAsync("QueueTriggerBinding", "RunAsync", _queueName, msg);
                }
            }, cancellationToken, TaskCreationOptions.LongRunning | TaskCreationOptions.DenyChildAttach, TaskScheduler.Default).Unwrap());
        }