コード例 #1
0
        /// <summary>
        /// Extension method to initialise the ScopedData instance from an incoming http Request.
        /// </summary>
        /// <param name="data">Reference to ScopedData instance.</param>
        /// <param name="req">The incoming http request.</param>
        /// <returns>boolean indicating whether any of the values have been set.</returns>
        public static bool InitialiseFromRequestHeaders(this ScopedData data, HttpRequest req)
        {
            bool updated = false;

            // If we have a correlation id in the header use it. This is preferred as we can still
            // log the creation of the BlobInfo object.
            if (req.Headers.ContainsKey(GeneralConstants.CorrelationIdHeaderName))
            {
                StringValues correlationId = req.Headers[GeneralConstants.CorrelationIdHeaderName];

                // We don't currently support a non-guid correlation id
                if (!Guid.TryParse(correlationId, out Guid corrId))
                {
                    throw new NotSupportedException($"The {GeneralConstants.CorrelationIdHeaderName} header is not a valid Guid");
                }

                data.AddCustomData(GeneralConstants.CorrelationIdKey, corrId.ToString());
                updated = true;
            }

            return(updated);
        }
        [ExcludeFromCodeCoverage] //This function just calls other services which are tested so there is no logic here that can be tested.
        public async Task RunAsync([EventGridTrigger] EventGridEvent eventGridEvent, ILogger log)
        {
            try
            {
                // Get data from event grid event
                dynamic eventGridEventData = JsonConvert.DeserializeObject(eventGridEvent.Data.ToString());
                Uri     uri = new Uri(eventGridEventData.url.ToString());

                // Generate correlation id and set Telemetry
                Guid       correlation = _blobInfoFactoryService.CreateCorrelationId();
                ScopedData scopedData  = new ScopedData();
                scopedData.AddCustomData(GeneralConstants.CorrelationIdKey, correlation.ToString()).Apply();
                log.LogInformation("Started: {functionName} {uri}, {name}", _functionName, uri, uri.Segments.Last());

                // Exclude empty files and folders
                if (!string.IsNullOrEmpty(uri.AbsoluteUri))
                {
                    // Generate BlobInfo
                    BlobInfo blobInfo = _blobInfoFactoryService.CreateBlobInfo(uri, uri.Segments.Last(), correlation);
                    log.LogInformation("{functionName}: created BlobInfo", _functionName);

                    // Send service bus message
                    await _serviceBusService.SubmitBlobInfoToServiceBus(blobInfo);

                    log.LogInformation("{functionName}: submitted BlobInfo to Service Bus", _functionName);
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex, "{functionName}: Failed to submit BlobInfo to Service Bus for {eventData}", _functionName, eventGridEvent.Data.ToString());
                throw;
            }
            finally
            {
                log.LogInformation("{functionName}: Completed {eventData}", _functionName, eventGridEvent.Data.ToString());
            }
        }