當 Azure 函式中未啟用除錯級別時,我試圖避免執行 log.debug。當我執行下面的代碼時,控制元件進入 IF 塊內。請解釋為什么無論 host.json 檔案中的配置如何都會執行此操作。
我嘗試使用 ILogger logger2 創建另一個物件,但仍然是一樣的。
[FunctionName("Function3")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
log.LogCritical("*****From Function3******");
log.LogTrace("LogTrace");
if (log.IsEnabled(LogLevel.Debug))
{
//Control should not come inside this as the debug level is not enabled
log.LogDebug("Debug message inside debug level");//Not printed as expected.
log.LogInformation("Inside Debug level"); //Not printed as expected
log.LogError("LogError Inside Debug level"); //Printed because of the setting in host.json
}
log.LogDebug("LogDebug");
log.LogInformation("LogInformation Blue");
log.LogWarning("LogWarning Yellow");
log.LogError("LogError Red");
log.LogCritical("LogCritical White");
return new OkObjectResult("Okay!");
}
主機.JSON
{
"version": "2.0",
"logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning",
"Function.Function3.User": "Error",
"Function.PaymentFeeder.User": "Warning",
"Function": "Warning"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
}
}
更新:使用 Visual Studio 2022 和targeting.Net 6 包括截圖

參考的包:
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.30" />
<PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
謝謝
uj5u.com熱心網友回復:
我們檢查您在host.json和functions中使用的內容。
我們遵循相同的方法,這里是解決方法:
如果您想避免在未啟用除錯級別時執行log.debug ,請執行以下操作:
如果我們想避免特定功能,您可以像這樣提及 “Function.Function1.User”:“Error”,它是針對特定功能使用特定日志的。此處的示例 對于 Funciton1,我們啟用了錯誤日志,因此在這里它不會進入if 塊if (log.IsEnabled(LogLevel.Debug))

這是在Function1中使用除錯級別日志的解決方法
這里我們提到Function1應該使用Debug log ,所以它會進入if block if (log.IsEnabled(LogLevel.Debug))

有關LogLevel配置的更多資訊,請參閱此
對 Function1使用除錯 logLevel

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/419200.html
標籤:
