.NET Core 6 Azure Function实时遥测无法正常工作问题排查
问题:Azure Functions实时遥测无数据显示,提示应用离线或SDK版本过旧
我使用.NET Core 6创建了一个基础的HTTP触发Azure Function,集成了Application Insights并启用了实时遥测(Live Telemetry)。已将代码部署到Azure Functions资源,能在Application Insights中查看日志,但查看实时遥测时无数据显示,提示“Not available: your app is offline or using an older SDK”。
NuGet包:
Microsoft.Extensions.Logging.ApplicationInsights
相关代码及配置如下:
host.json
{ "version": "2.0", "logging": { "fileLoggingMode": "always", "applicationInsights": { "enableLiveMetrics": true, "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" } }, "logLevel": { "default": "Information", "Host": "Error", "Function": "Error", "Host.Aggregator": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "extensions": { "http": { "routePrefix": "api" } } }
Startup.cs
using Microsoft.ApplicationInsights.Extensibility; using Microsoft.Azure.Functions.Extensions.DependencyInjection; using Microsoft.Azure.WebJobs.Host.Bindings; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using OSH_Function; using System; [assembly: FunctionsStartup(typeof(Startup))] namespace Function1 { public class Startup : FunctionsStartup { public IConfiguration configuration { get; set; } public override void Configure(IFunctionsHostBuilder builder) { //Load App Settings configuration = BuildConfiguration(builder.GetContext().ApplicationRootPath); builder.Services.AddSingleton(new TelemetryConfiguration { ConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING")}); } private IConfiguration BuildConfiguration(string applicationRootPath) { var config = new ConfigurationBuilder() .SetBasePath(applicationRootPath) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddJsonFile("settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); return config; } } }
本地设置(Local Settings)
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "", "FUNCTIONS_WORKER_RUNTIME": "dotnet", "APPINSIGHTS_INSTRUMENTATIONKEY": "<<KEY>>", "APPLICATIONINSIGHTS_CONNECTION_STRING": "<<STRING>>" } }
函数代码
using System; using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Newtonsoft.Json; namespace FunctionApp1 { public static class Function2 { [FunctionName("Function2")] public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); log.LogTrace("ILogger: xxxxxxxxxxxxxxxxxxxxxxxxx"); log.LogDebug("ILogger: debug message from azure function"); log.LogInformation("ILogger: information message from azure function"); log.LogWarning("ILogger: warning message from azure function"); log.Log(LogLevel.Error, "ILogger: error message from azure function"); string name = req.Query["name"]; string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); name = name ?? data?.name; string responseMessage = string.IsNullOrEmpty(name) ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response." : $"Hello, {name}. This HTTP triggered function executed successfully."; return new OkObjectResult(responseMessage); } } }
Azure门户中的Application Insights日志:
请问我还遗漏了什么配置或步骤?
内容的提问来源于stack exchange,提问作者PavanKumar GVVS
相关产品推荐
相关产品推荐

