[英]Disable logging of host lock lease renew calls in Azure 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.