来自 Azure Function (.net Core 3.1) 的调试和跟踪日志在 Application Insights 中不可见
Debug and Trace logs from Azure Function (.net Core 3.1) not visible in Application Insights
我正在尝试通过 ILogger 将调试和跟踪日志获取到 Application Insights,但不幸的是,它不符合我的要求。
我已经完成了最简单的演示,并将 INFO、WARN 和 ERR 日志发送到 Application Insights - 但不低于 INFO。
我在这里发现了几个关于同一主题的帖子,但是 none 这似乎解决了我的问题。
感谢任何帮助:)
谢谢
来自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"
}
}
}
}
恐怕您还需要检查 host.json
文件,因为 this section 介绍了如何设置日志级别,如下所示:
For logs of Host.Results or Function, only log events at Error or a
higher level.
{
"logging": {
"fileLoggingMode": "always",
"logLevel": {
"default": "Information",
"Host.Results": "Error",
"Function": "Error",
"Host.Aggregator": "Trace"
}
}
}
我正在尝试通过 ILogger 将调试和跟踪日志获取到 Application Insights,但不幸的是,它不符合我的要求。
我已经完成了最简单的演示,并将 INFO、WARN 和 ERR 日志发送到 Application Insights - 但不低于 INFO。
我在这里发现了几个关于同一主题的帖子,但是 none 这似乎解决了我的问题。
感谢任何帮助:)
谢谢
来自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"
}
}
}
}
恐怕您还需要检查 host.json
文件,因为 this section 介绍了如何设置日志级别,如下所示:
For logs of Host.Results or Function, only log events at Error or a higher level.
{
"logging": {
"fileLoggingMode": "always",
"logLevel": {
"default": "Information",
"Host.Results": "Error",
"Function": "Error",
"Host.Aggregator": "Trace"
}
}
}