gpt4 book ai didi

c# - Azure函数: Have a CorrelationId between Http Trigger and Blob Trigger to follow a request

转载 作者:行者123 更新时间:2023-12-03 04:00:17 26 4
gpt4 key购买 nike

我希望有一个 CorrelationId 能够在应用程序洞察中跟踪从 Http 触发器到 Blob 触发器的请求。

我正在 Http 触发器函数中创建 CorrelationId,并希望在 blob 触发器中跟踪相同的内容。

这是我的 Http 触发器函数:

[FunctionName(nameof(ReceiveEvent))]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
[Inject] ILoggingService loggingService,
[Inject] IProvideCorrelationIds correlationIds,
[Inject] IEventMapper eventMapper,
[Inject] IEventValidator eventValidator,
[Inject] IEventHandler<ResultDto, Messages.Events.Event> eventHandler)
{
var logger = new Logger(loggingService);

try
{
IActionResult actionResult = null;

correlationIds.CorrelationId = Guid.NewGuid().ToString();

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

logger.Info($"Event request received");

var @event = eventMapper.Map(requestBody);

if (eventValidator.Validate(req, @event, logger, ref actionResult))
{
var response = await eventHandler.HandleAsync(@event, logger);
actionResult = new OkObjectResult(response);
}
return actionResult;
}
catch (Exception ex)
{
logger.Error($"Exception while processing {nameof(ReceiveEvent)}", ex,
nameof(ReceiveEvent));

throw;
}
}

CorrelationId 提供商:

public class CorrelationIdProvider: IProvideCorrelationIds
{
private static readonly AsyncLocal<string> AsyncLocalCorrelationId = new AsyncLocal<string>();

public string CorrelationId
{
get => AsyncLocalCorrelationId.Value;
set => AsyncLocalCorrelationId.Value = value;
}
}

Blob 触发功能:

[FunctionName(nameof(ProcessEvent))]
public static async Task Run([BlobTrigger(BlobStorageContainer.Name + "/{name}",
Connection = "AzureWebJobsStorage")]
Stream eventBlob, string name,
[Inject] ILoggingService loggingService,
[Inject] IEventProcessorService eventProcessor,
[Inject] IBlobClient blobClient)
{
var logger = new Logger(loggingService);
try
{
logger.Info($"Starting blob job tracker for file name {name}",
nameof(ProcessEvent));

//correlationIds.CorrelationId = correlationId;

var eventContent = eventBlob.ReadAsString();

var result = await eventProcessor.HandleProcessor(eventContent, logger);

if (result)
{
await blobClient.DeleteBlobAsync(BlobStorageContainer.Name, name);
logger.Info($"Blob deleted successfully file name: {name}");
}
else
{
logger.Warning($"Unable to process blob job for file with name: {name}");
}
}
catch (Exception ex)
{
logger.Error($"Unable to process blob job for file with name: {name}", ex,
nameof(ProcessEvent));
}
}

要在 Blob 触发器中获取 CorrelationId,我需要进行哪些更改?

最佳答案

您可以将两个触发器放在同一个类中,使用下面的代码您可以从http触发器获取id:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace FunctionApp86
{
public class CorrelationIdProvider
{
public string id;
public string CorrelationId
{
get { return id; }
set { id = value; }
}
}

public static class Function1
{
public static string id;
[FunctionName("Function1")]
public static async Task<IActionResult> Run1(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
CorrelationIdProvider cp = new CorrelationIdProvider();
cp.CorrelationId = Guid.NewGuid().ToString();
log.LogInformation(cp.CorrelationId);
id = cp.CorrelationId;

return new OkObjectResult(cp.CorrelationId);
}
[FunctionName("Function2")]
public static async Task<IActionResult> Run2(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");

CorrelationIdProvider cp = new CorrelationIdProvider();
cp.CorrelationId = id;
log.LogInformation(cp.CorrelationId);

return new OkObjectResult(cp.CorrelationId);
}
}
}

我的第二个触发器是http触发器,你可以将其更改为blob触发器。:)

关于c# - Azure函数: Have a CorrelationId between Http Trigger and Blob Trigger to follow a request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62730575/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com