我有两个 Azure Functions:一个使用持久编排,另一个使用队列触发器。这两个函数都利用 ILogger 通过 log.LogInformation 和 log.LogWarning 进行日志记录,并且还具有 Console.WriteLine 语句用于输出。
当我部署并触发这两个函数应用程序时,我可以在 Azure 门户的“监控”->“日志流”部分中看到日志信息和控制台值。但是,当我尝试通过 Application Insights 导出这些日志时,遇到了问题。
在 Application Insights -> Logs 中,我运行以下查询来检索日志:
union isfuzzy=true
availabilityResults,
requests,
exceptions,
pageViews,
traces,
customEvents,
dependencies
| order by timestamp desc
我获得了日志详细信息列表,但只有 Console.WriteLine 消息可用。 log.LogInformation 消息丢失。
我的 host.json 文件配置了以下日志级别:
{
"version": "2.0",
"logging": {
"logLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
尽管如此,log.LogInformation 条目并未出现在 Application Insights 中。以下是我的 Program.cs 配置,用于在 .NET 隔离进程中设置 Application Insights:
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Text.Json;
using System.Text.Json.Serialization;
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.Configure<JsonSerializerOptions>(options =>
{
options.Converters.Add(new JsonStringEnumConverter());
options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; // Ignore null values during serialization
});
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
})
.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true);
config.AddEnvironmentVariables();
})
.Build();
host.Run();
示例功能
namespace serviceBus.QueueTrigger
{
public class ServiceBusQueueTrigger
{
private readonly ILogger<ServiceBusQueueTrigger> logger;
public ServiceBusQueueTrigger(ILoggerFactory loggerFactory)
{
logger = loggerFactory.CreateLogger<ServiceBusQueueTrigger>();
}
[Function(nameof(ServiceBusQueueTrigger))]
public async Task Run(
[ServiceBusTrigger("baseloadqueue", Connection = "serviceBusConnectionString")]
ServiceBusReceivedMessage message,
ServiceBusMessageActions messageActions)
{
logger.LogInformation("queue triggered");
}
}
}
我期待任何解决方案来获取我的函数应用程序的日志文件 现在我正在尝试导出应用程序洞察力以获取日志文件 获取我们项目的日志文件的任何其他方式对我来说也可以。
在 Program.cs 中添加遥测日志记录配置。
使用下面修改后的代码将 ILogger 消息记录到应用程序洞察中。
程序.cs:
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices((context, config_services) =>
{
IConfiguration config = context.Configuration;
config_services.AddLogging(loggingBuilder =>
{
var con_strg = config["kpconnstring"];
var kplogconfig = new LoggerConfiguration()
.MinimumLevel.Information()
.Enrich.FromLogContext();
kplogconfig.WriteTo.ApplicationInsights(con_strg, TelemetryConverter.Traces);
loggingBuilder.AddSerilog(kplogconfig.CreateLogger());
});
config_services.AddApplicationInsightsTelemetryWorkerService();
config_services.ConfigureFunctionsApplicationInsights();
})
.ConfigureAppConfiguration((context, configBuilder) =>
{
configBuilder.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true);
})
.Build();
host.Run();
主机.json:
{
"version": "2.0",
"logging": {
"fileLoggingMode": "always",
"logLevel": {
"default": "Information"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": false,
"excludedTypes": "Request"
},
"enableLiveMetricsFilters": true
}
}
}
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"SBconnection": "<Service_Bus_connection_string>",
"Application_insights": "<Application_insights_connection_string>"
}
}
功能代码:
private readonly ILogger<Function1> _logger;
public Function1(ILogger<Function1> logger)
{
_logger = logger;
}
[Function(nameof(Function1))]
public async Task Run(
[ServiceBusTrigger("queue1", Connection = "demo")]
ServiceBusReceivedMessage message,
ServiceBusMessageActions messageActions)
{
_logger.LogInformation("Message ID: {id}", message.MessageId);
_logger.LogInformation("Message Body: {body}", message.Body);
_logger.LogInformation("Message Content-Type: {contentType}", message.ContentType);
await messageActions.CompleteMessageAsync(message);
}
Ilogger
日志将作为跟踪存储在应用程序洞察中。
union isfuzzy=true
availabilityResults,
requests,
exceptions,
pageViews,
traces,
customEvents,
dependencies
| where timestamp > datetime("2024-07-21T10:52:11.299Z") and timestamp < datetime("2024-07-22T10:52:11.299Z")
| order by timestamp desc
| take 100