我有一个 Azure Functions 项目,该项目已部署到用于开发/测试的 Function App,并且已成功将日志消息写入 Application Insights。我在同一个 Azure 订阅中将同一个项目发布到一个新的函数应用程序,现在这两个应用程序都没有将任何内容记录到 Application Insights。
该项目是使用 Visual Studio(15.8.7 with Azure Functions and Web Jobs Tools 15.9.02009.0)部署的。
我尝试向 host.json 添加一个记录器条目,以将函数类别日志级别显式设置为信息。
Azure 函数和 Web 作业工具扩展在 Visual Studio 中是最新的。
我还尝试删除 APPINSIGHTS_INSTRUMENTATIONKEY 应用程序设置,然后重新启动应用程序,这样当我单击“监视器”选项卡时,它会通过向导配置 Application Insights 的其中一项功能。但最后,它显示消息“您的应用程序离线或应用程序洞察 sdk 需要更新”。
任何想法可能是错误的?
尽管OP已经解决了他的问题,但我发现了日志突然消失的另一种可能性。有一个称为采样的功能。它基本上是说,如果有很多要记录的内容,它将选择随机(或半随机)记录的内容。并且默认情况下它是启用的。长话短说,为了配置它,必须调整host.json文件。
具体来说,为了完全禁用采样,必须添加以下内容:
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": false
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果使用隔离的 Azure Functions,则默认情况下会在 Application Insight 中捕获警告和更高级别。如果您想首先更改它,您需要删除 AI 添加的默认日志记录过滤器:在Program.cs配置过滤器中
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(s =>
{
s.AddApplicationInsightsTelemetryWorkerService();
s.ConfigureFunctionsApplicationInsights();
s.Configure<LoggerFilterOptions>(options =>
{
// The Application Insights SDK adds a default logging filter that instructs ILogger to capture only Warning and more severe logs. Application Insights requires an explicit override.
// Log levels can also be configured using appsettings.json. For more information, see https://learn.microsoft.com/en-us/azure/azure-monitor/app/worker-service#ilogger-logs
LoggerFilterRule toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
== "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (toRemove is not null)
{
options.Rules.Remove(toRemove);
}
});
})
.Build();
Run Code Online (Sandbox Code Playgroud)
之后,您可以轻松地在 host.json 中配置日志记录有关级别的更多信息
{
"logging": {
"logLevel": {
"default": "Information"
}
}
}
Run Code Online (Sandbox Code Playgroud)
未显示日志记录的另一个令人困惑的来源是将记录器注入到函数项目内的组件中。例如,您可以将 aLogger<T>注入函数中的某些内容
namespace ThingyFunctions{
public class Thingy
{
public Thingy(ILogger<Thingy> log)
{
this.log = log;
}
[FunctionName(nameof(Thingy)]
public async Task Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer)
{
log.LogInformation("Some message here");
}
}
}
Run Code Online (Sandbox Code Playgroud)
默认情况下,该日志消息将被过滤掉。您可以通过更改 host.json 中的日志级别来查看它
"logging": {
"logLevel": {
"default": "Information"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": false
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将捕获信息级别的所有内容。你也可以通过命名空间来做
"logging": {
"logLevel": {
"ThingyFunctions": "Information"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": false
}
}
}
Run Code Online (Sandbox Code Playgroud)
我相信你也可以通过函数名来做
"logging": {
"logLevel": {
"Function.Thingy": "Information"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": false
}
}
}
Run Code Online (Sandbox Code Playgroud)
唔。看起来矩阵中出现了故障。我不确定矩阵的哪一部分 - Azure 门户/App Insights 等。
不过,监控现在正在发挥作用。
看起来它在我删除 APPINSIGHTS_INSTRUMENTATIONKEY 并重新配置它时开始工作,但直到一个多小时后条目才显示在门户中。
| 归档时间: |
|
| 查看次数: |
3252 次 |
| 最近记录: |