【发布时间】:2022-01-11 22:40:05
【问题描述】:
我有以下 Azure 函数:
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using System.Threading.Tasks;
internal class ExpServiceFuncApp
{
[FunctionName("ExpServiceFuncApp")]
public static void Run([TimerTrigger("0 */10 * * * *")] TimerInfo myTimer, ILogger log)
{
//using var channel = new InMemoryChannel();
try
{
IServiceCollection services = new ServiceCollection();
//services.Configure<TelemetryConfiguration>(config => config.TelemetryChannel = channel);
//services.AddLogging(builder =>
//{
// builder.AddApplicationInsights(KeyVaultConfig.GetSecretValue("InstrumentationKey"));
//})
services.AddSingleton<IDataRec, DataRec.DataRec>()
.AddSingleton<IDataProc, DataProc.DataProc>()
.AddSingleton<IDClient, KustoDClient>()
.AddSingleton<IAzureBlobClient, AzureBlobClient>()
.AddSingleton<IAzureKustoClient, AzureKustoClient>()
.AddSingleton<IQueriesSettings, QueriesSettings>()
.AddSingleton<IServiceSettings, ServiceSettings>()
.AddTransient<IRawData, RawData>();
IServiceProvider serviceProvider = services.BuildServiceProvider();
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
ILogger logger = loggerFactory.CreateLogger<ExportServiceFunctionApp>();
logger?.LogInformation("Initiate.");
var dataProc = serviceProvider.GetRequiredService<IDataProc>();
logger?.LogInformation("Start Data Process.");
dataProc.Start();
Console.ReadLine();
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}
catch (Exception ex)
{
}
//finally
//{
// channel.Flush();
// Task.Delay(5000).Wait();
//}
}
}
正如你在这里看到的,我在这个类中有一些日志信息,而且我还有其他日志正在被注入到这里的其他类中使用。此外,我将机密存储在 Azure Key Vault 中。这里的问题是,当我将应用程序发布到 azure 时,应用程序洞察力没有跟踪任何这些日志(代码中提到的 log.Information 或 log.Debug 都没有)。您还可以看到,我注释了一些行,我认为这可能有助于跟踪 ApplicationInsights 上的日志,但仍然没有成功。我在 Application Insights 上收到的信息如下:
host.json 如下:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
},
"logLevel": {
"Default": "Information",
"Microsoft": "Error"
}
}
}
}
我真的不知道代码中是否缺少某些内容或错误。老实说,我不知道触发器是否仅自行运行而不每 10 分钟执行一次代码。我真的尝试了几种方法来跟踪这些日志,但它仍然不起作用。谁能给我一个解决这个问题的方法? 请注意,当我将程序作为控制台应用程序运行并在控制台上显示结果时,一切正常。所以我现在从上面的代码中猜测如何将应用程序洞察力与 azure 函数集成的问题。
【问题讨论】:
-
您是否在 Azure 门户中将应用程序洞察帐户添加到您的函数应用?
-
您是否尝试过在函数内部使用 go 的 Monitor 功能?即:点击Function app -> 点击functions -> 然后你的函数必须在那里列出,点击相关的function -> 然后点击Monitor.
-
@HariKrishnaRajoli-MT 是的,我在 azure 门户中的函数应用程序中添加了应用程序洞察。我是在visual studio中配置的,配置在Function App的应用程序设置中。
-
@Dhanushka Rukshan 我试过了。它仍然没有工作。我只收到上图中显示的值。
-
您没有为应用程序洞察设置正确的日志级别。有关正确配置,请参阅 stackoverflow.com/questions/69416109/…。
标签: c# .net azure azure-functions azure-application-insights