Azure Function Logging - A Simple Example

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ธ.ค. 2024

ความคิดเห็น • 1

  • @paulbeck1
    @paulbeck1  ปีที่แล้ว

    #r "Newtonsoft.Json"
    using System.Net;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Primitives;
    using Newtonsoft.Json;
    public static async Task Run(HttpRequest req, ILogger log)
    {
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    int eventId = data?.eventId;
    string errType = data?.errType;
    string errMsg = data?.errMsg;
    string correlationId = data?.correlationId;
    string workflowId = data?.workflowId;
    string workflowUrl = data?.workflowUrl;
    string flowDisplayName = data?.flowDisplayName;
    var custProps = new Dictionary()
    {
    { "CorrelationId", correlationId},
    { "WorkflowId", workflowId},
    { "WorkflowUrl", workflowUrl},
    { "WorkflowDisplayName", flowDisplayName}
    };
    using (log.BeginScope(custProps))
    {
    if (errType=="Debug")
    {
    log.Log(LogLevel.Debug, eventId, $"{errMsg}");
    }
    else if (errType=="Critical")
    {
    log.Log(LogLevel.Critical, eventId, $"{errMsg}");
    }
    else if (errType=="Warning")
    {
    log.Log(LogLevel.Warning, eventId, $"{errMsg}");
    }
    else if (errType=="Trace")
    {
    log.Log(LogLevel.Trace, eventId, $"{errMsg}");
    }
    else if (errType=="Error")
    {
    log.Log(LogLevel.Error, eventId, $"{errMsg}");
    }
    else
    {
    log.LogInformation($"Event is {eventId}, type is {errType}, and msg is {errMsg}");
    }
    };
    string responseMessage = $"This HTTP triggered function executed successfully. {errType} - {errMsg}";
    return new OkObjectResult(responseMessage);
    }