我在 dotnet-isolated 模式下使用 AzureFunctionsVersion-4 和 dotnet-8!
我在 dotnet 中使用默认日志记录提供程序。
我的日志记录有问题!问题是,我使用 HttpClient 对外部服务进行 HTTP 调用,并且那里有一些日志,如下所示:
Executing '[FunctionName]' (Reason='New queue message detected on 'queueName'.', Id=e29a273f-4181-4f59-a523-374639efc066)
Start processing HTTP request GET [Endpoint]
Sending HTTP request GET [Endpoint]
Received HTTP response headers after *ms - 200
End processing HTTP request after *ms - 200
Executed '[FunctionName]' (Succeeded, Id=e29a273f-4181-4f59-a523-374639efc066, Duration=*ms)
我尽一切努力禁用这些日志,但没有成功!
这是host.json中的配置:
{
"version": "2.0",
"logging": {
"logLevel": {
"Microsoft": "Warning"
}
},
"extensions": {
"queues": {
"batchSize": 5
}
}
}
在 appsettings.json 中:
"Logging": {
"LogLevel": {
"Default": "Warning",
"FunctionName": "Information"
}
}
我试图在
host.json中为“执行/已执行”日志添加
"Function": "Warning"
,但它已禁用整个功能中的所有其他日志!
这是功能代码:
using System.IO.Compression;
using System.Text;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace FunctionName.Sample.Functions;
public class TestFunction
{
private readonly ILogger<TestFunction> _logger;
private readonly IHttpService _httpService;
public TestFunction(
ILogger<TestFunction> logger,
IHttpService httpService
)
{
_logger = logger;
_httpService = httpService;
}
[Function(nameof(TestFunction))]
public async Task RunAsync([QueueTrigger("%QueueName%")] byte[] bytes, CancellationToken cancellationToken)
{
try
{
_logger.LogInformation("Logs...");
var result = await _httpService.Call(cancellationToken);
_logger.LogInformation($"C# Queue trigger function processed.");
}
catch (Exception ex)
{
throw;
}
}
}
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices((builder, services) =>
{
// ...
var configuration = builder.Configuration;
// ...
})
.ConfigureAppConfiguration((context, builder) =>
{
builder.ConfigureApp(context); // Add ENV variables here
})
.Build();
host.Run();
这里的重点是同样的配置在dotnet-6项目中可以正常工作!
大家有什么意见吗?
我已经使用运行时堆栈 dotnetisolated 8.0 创建了队列触发函数。
.ConfigureLogging(logging =>
{
logging.Services.Configure<LoggerFilterOptions>(options =>
{
LoggerFilterRule defaultRule = options.Rules.FirstOrDefault(rule => rule.ProviderName
== "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (defaultRule is not null)
{
options.Rules.Remove(defaultRule);
}
});
})
功能代码:
namespace FunctionApp1
{
public class Function1
{
private readonly ILogger<Function1> _logger;
public Function1(ILogger<Function1> logger)
{
_logger = logger;
}
[Function("Function1")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
_logger.LogInformation("This is Information log");
_logger.LogWarning("This is Warning log");
_logger.LogError("This is Error log");
_logger.LogCritical("This is Critical log");
return new OkObjectResult("Welcome to Azure Functions!");
}
}
}
Warning
严重级别的数据。您需要在下面的program.cs中的部分服务配置中禁用它:程序.cs:
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
})
.ConfigureLogging(logging =>
{
logging.Services.Configure<LoggerFilterOptions>(options =>
{
LoggerFilterRule defaultRule = options.Rules.FirstOrDefault(rule => rule.ProviderName
== "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (defaultRule is not null)
{
options.Rules.Remove(defaultRule);
}
});
})
.Build();
host.Run();
在独立函数中,函数的日志记录级别和默认值分别位于 host.json 中。检查下面:
主机.json:
{
"version": "2.0",
"logging": {
"logLevel": {
"Microsoft": "Warning",
"Function": "Information"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": false,
"excludedTypes": "Request"
},
"enableLiveMetricsFilters": true
}
},
}
我已成功将该功能部署到azure门户中。然后被触发并获取日志。检查下面:
输出: