首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于Azure Serverless无服务器函数的ILogger接收器

用于Azure Serverless无服务器函数的ILogger接收器
EN

Stack Overflow用户
提问于 2022-11-06 15:05:55
回答 1查看 53关注 0票数 0

我将ILogger注入到Azure函数中,在host.json中有以下内容:

代码语言:javascript
复制
{
    "version": "2.0",
    "logging": {
      "applicationInsights": {
        "samplingSettings": {
          "isEnabled": true,
          "maxTelemetryItemsPerSecond": 20
        }
      }
    }
}

然后,我登录如下:

代码语言:javascript
复制
public class EnrollmentFunctions
{
    private readonly ILogger<AttestationFunctions> logger;

    public EnrollmentFunctions(ILogger<AttestationFunctions> log)
    {
        logger = log;
    }

    public async Task<IActionResult> Enroll(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "enroll")] HttpRequest req)
    {
         logger.LogInformation("Requesting attestation of device name {deviceName}.", req.Query["deviceName"]);
    }
}

然而,没有日志记录如下-欣赏关于我做错了什么以及我会在哪里看到我的日志的指针请。

EN

回答 1

Stack Overflow用户

发布于 2022-11-08 13:03:17

如下所述,为了取得预期的结果,我遵循了以下步骤:

我在使用Visual的函数项目中使用了广义日志记录,并将TelemetryClientILogger都包含在类Genlog.cs

Genlog.cs

代码语言:javascript
复制
  public class Genlog
    {
        public Genlog(ILogger<Genlog> logger, TelemetryClient telemetry)
        {
            Logger = logger ?? throw new ArgumentNullException(nameof(logger));
            Telemetry = telemetry ?? throw new ArgumentNullException(nameof(telemetry));
        }

        public void testlog(string mylog)
        {
            Logger.LogInformation("TestCustomInformationLog:" + mylog);
            Telemetry.TrackTrace("TestCustomTraceLog: " + mylog, Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information);
            Telemetry.TrackException(new Exception(""));            
        }

        public ILogger<Genlog> Logger { get; }
        public TelemetryClient Telemetry { get; }
    }

Startup.cs

代码语言:javascript
复制
#Adding configuring service of Genlog in startup.cs
public override void Configure(IFunctionsHostBuilder builder)
{
    builder.Services.AddScoped<Genlog>();
}

Function.cs

代码语言:javascript
复制
  public class Function1
    {
        private readonly Genlog genlog;
        public Function1(Genlog genlog)
        {
            this.genlog = genlog ?? throw new ArgumentNullException(nameof(genlog));
        }

        [FunctionName("Function1")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            # Using Both Ilogger & Telemetry logger in same class.
            genlog.testlog("C# HTTP trigger function processed a request.");
            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";
            genlog.testlog(responseMessage);
            return new OkObjectResult(responseMessage);
        }
    }

结果

发布到Azure 以检查和检索日志的:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74337017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档