繁体   English   中英

Azure Function 记录主机与 Function

[英]Azure Function Logging Host vs. Function

我在多项目解决方案中设置了标准的 DI 日志记录设置,但在 Function 日志记录中,仅显示了来自参数的 ILogger 实例,而不是来自从 Function 调用的服务的 ILogger<T> 日志。但是 ILogger<T> 日志确实如此显示在主机日志中。 您能否从服务中获取 ILogger<T> 日志以显示在 Function 日志中?

TestFunction.cs

private readonly ILogger<TestFunction> _logger;
private readonly ITestService _testService;

public TestFunction(ILogger<TestFunction> logger, ITestService testService)
{
   _logger = logger;
   _testService = testService;
}

[FunctionName("TestFunction")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
{
   log.LogInformation("I am from the log,");
   _logger.LogInformation("I am from the _logger");

   await _testService.TestLogs();

   log.LogInformation("Ending from the log,");
   _logger.LogInformation("Ending from the _logger");

   return new OkObjectResult("Fin");
}

TestService.cs

public interface IMessageQueueService
{ 
    Task TestLogs();
}

public class TestService : ITestService
{

   private readonly ILogger<TestService> _logger;

   public TestService(ILogger<TestService> logger) 
   {
       _logger = logger;
   }
 
   public async Task TestLogs()
   {
      _logger.LogInformation("I am _logger in a service");
   }
}

Function 日志显示如下:

2022-11-30T15:17:07.988 [Information] Executing 'TestFunction' (Reason='This function was programmatically called via the host APIs.', Id=cc28beea-f9ec-4642-9946-cf5a588df320)
2022-11-30T15:17:07.988 [Information] I am from the log,
2022-11-30T15:17:07.989 [Information] Ending from the log,
2022-11-30T15:17:07.996 [Information] Executed 'TestFunction' (Succeeded, Id=cc28beea-f9ec-4642-9946-cf5a588df320, Duration=17ms)

主机日志显示:

2022-11-30T15:46:41.713 [Information] Executing 'TestFunction' (Reason='This function was programmatically called via the host APIs.', Id=5f571267-3edb-4c99-8d15-5c7dee5e6446)
2022-11-30T15:46:41.714 [Information] I am from the log,
2022-11-30T15:46:41.714 [Information] I am from the _logger
2022-11-30T15:46:41.714 [Information] I am _logger in a service
2022-11-30T15:46:41.714 [Information] Ending from the log,
2022-11-30T15:46:41.714 [Information] Ending from the _logger
2022-11-30T15:46:41.714 [Information] Executed 'TestFunction' (Succeeded, Id=5f571267-3edb-4c99-8d15-5c7dee5e6446, Duration=8ms)

我怎样才能让所有这些都显示在 Function 日志中?

  • 好吧,一旦你在 `` 文件中指定了日志级别,你也将能够登录 function 主机。

  • 要设置日志级别,您将在host.json中指定命名空间和日志级别,即信息、警告等。

我的host.json

{
    "version": "2.0",
  "logging": {
    "logLevel": {
      "FunctionApp17": "Information"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  }
}

这里我的命名空间是FunctionApp17

我的 function 触发器:

namespace FunctionApp17
{
    public  class Function1
    {
        private readonly ITestServices testServices;
        private readonly ILogger<Function1> loggingThroughDependencyInjection;

        public Function1(ITestServices testServices,ILogger<Function1>loggingThroughDependencyInjection)
        {
            this.testServices = testServices;
            this.loggingThroughDependencyInjection = loggingThroughDependencyInjection;
        }

        [FunctionName("Function1")]
        public  async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger loggingThroughPassedParameter)
        {
            loggingThroughPassedParameter.LogInformation("C# HTTP trigger function processed a request.");
            loggingThroughDependencyInjection.LogInformation("Logging using Dependency Injection ");
            testServices.test();

            return new OkObjectResult("Triggered the http function");
        }
    }
}

测试服务:

namespace FunctionApp17
{
    public interface ITestServices
    {
        void test();
    }

    public class TestServices : ITestServices
    {
        private readonly ILogger<TestServices> loggingThroughDependencyInjection;

        public TestServices(ILogger<TestServices> loggingThroughDependencyInjection)
        {
            this.loggingThroughDependencyInjection = loggingThroughDependencyInjection;
        }
        public void test()
        {
            loggingThroughDependencyInjection.LogInformation("Logging using Depency Injection in TestService class");
        }
    }
}

启动.cs


[assembly: FunctionsStartup(typeof(FunctionApp17.Startups))]
namespace FunctionApp17
{
    public class Startups : FunctionsStartup

    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddTransient<ITestServices,TestServices>();
            builder.Services.AddLogging();
        }
    }
}

output:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM