【问题标题】:Debug and Trace logs from Azure Function (.net Core 3.1) not visible in Application InsightsAzure Function (.net Core 3.1) 的调试和跟踪日志在 Application Insights 中不可见
【发布时间】:2021-11-10 06:18:05
【问题描述】:

我正在尝试通过 ILogger 将调试和跟踪日志获取到 Application Insights,但不幸的是它没有按照我的方式进行。

我制作了最简单的演示,并将 INFO、WARN 和 ERR 日志发送到 Application Insights - 但不低于 INFO。

我在这里找到了几篇关于同一主题的帖子,但似乎都没有解决我的问题。

任何帮助表示赞赏:)

谢谢

来自 AI 交易搜索

演示功能

public static class AIDemoConfig
{
    [FunctionName("AIDemo")]
    public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
    {   
        log.LogTrace("TRACE: C# HTTP trigger function processed a request.");
        log.LogDebug("DEBUG: C# HTTP trigger function processed a request.");
        log.LogInformation("INFO: C# HTTP trigger function processed a request.");
        log.LogWarning("WARN: C# HTTP trigger function processed a request.");
        log.LogError("ERROR: C# HTTP trigger function processed a request.");

        string responseMessage = "Hello Log World";

        return new OkObjectResult(responseMessage);
    }
}

启动

public class Startup : FunctionsStartup
{
    public IConfiguration Configuration { get; }
    public Startup() { }

    public override void Configure(IFunctionsHostBuilder builder)
    {
        //Add ApplicationInsights
        string applicationInsightsInstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
        if (!string.IsNullOrEmpty(applicationInsightsInstrumentationKey))
        {
            builder.Services.AddLogging(builder =>
            {
                builder.AddApplicationInsights(applicationInsightsInstrumentationKey);
                builder.SetMinimumLevel(LogLevel.Trace);
                builder.AddFilter<ApplicationInsightsLoggerProvider>(typeof(AIDemoConfig).FullName, LogLevel.Trace);
                builder.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Error);
            });

            builder.Services.AddApplicationInsightsTelemetry();
        }

        builder.Services.AddMvcCore();
        builder.Services.AddOptions();
    }
}

appsettings.json

{
  "Values": {
    "APPINSIGHTS_INSTRUMENTATIONKEY": "4e0bfb46-68a4-4622-942a-0f80920a82a9"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Warning",
      "Microsoft": "Information"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug"
      }
    }
  }
}

【问题讨论】:

    标签: c# azure-functions trace ilogger


    【解决方案1】:

    恐怕你还需要检查host.json文件,因为this section介绍了如何设置日志级别,如下所示:

    对于 Host.Results 或 Function 的日志,仅记录 Error 或 a 更高级别。

    {
      "logging": {
        "fileLoggingMode": "always",
        "logLevel": {
          "default": "Information",
          "Host.Results": "Error",
          "Function": "Error",
          "Host.Aggregator": "Trace"
        }
      }
    }
    

    【讨论】:

    • 谢谢 - 成功了! :) 现在将所有日志都发送到 AI - 但是,副作用似乎是功能控制台日志记录已停止(仅获取“Executing AIDemo...”和“Executed AIDemo...”)。此外,限制特定功能的日志级别不起作用,例如“Function.AIDemo”:“信息”,- 但那是另一个问题;)
    • 感谢您的回复先生,我很高兴看到它对您有用 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 2022-08-18
    • 2022-01-11
    • 1970-01-01
    相关资源
    最近更新 更多