我有一个在 Azure 中运行的独立 .NET 7 Azure Function App。在 Azure 门户中创建时,Function App 将连接到 Application Insights。
目前使用Serilog将所有日志推送到Datadog。但它仍然吸收了大量 Application Insights 的痕迹。
我更喜欢 Datadog 来分析应用程序日志,但也认为 Application Insights 有一些有用的可视化,例如函数调用、调用失败等
是否可以继续使用 Datadog 和 Application Insights,但停止将跟踪日志提取到 Application Insights?
我同意@Ziya Mert Karakas
但是,我使用了另一种简单的解决方法。
您可以在 host.json 中使用 "logLevel" 发送应用程序洞察中跟踪表中的日志。如果默认情况下不使用日志级别,则会设置为使用 Information 级别。
在我的host.json中,我已将其设置为“警告”,这样我将获得警告级别和更高的日志。
host.json
:
{
"version": "2.0",
"logging": {
"logLevel": {
"default": "Warning"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
},
"enableLiveMetricsFilters": true
}
}
}
我创建了一个简单的计时器触发器来将日志发送到 Datadog 和应用程序洞察。
Function.cs
:
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Serilog;
using System;
public static class Function1
{
[Function("MyFunction")]
public static void Run(
[TimerTrigger("*/30 * * * * *")] FunctionContext context)
{
// Log using Serilog
Log.Information("C# Timer trigger function executed at: {time}", DateTime.Now);
Log.Information("Time in UTC: {time}", DateTime.UtcNow);
Log.Information("This is Datadog LOG");
// Log using the ILogger provided by Azure Functions
var logger = context.GetLogger("Function1");
logger.LogInformation("This is an Azure Functions log message.");
logger.LogWarning("This is a Warning message");
logger.LogError("this is a error message");
}
}
program.cs
:
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Sinks.Datadog.Logs;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
// Configure Serilog for Datadog
Log.Logger = new LoggerConfiguration()
.WriteTo.DatadogLogs(apiKey: "xxxxxxxxxxxx")
.CreateLogger();
})
.Build();
host.Run();
Output
:
不使用:loglevel
其他跟踪日志也可用。
与:
logLevel
只有等于或更高严重性警告级别的日志可用。
作为参考,请检查此 MS Doc。