我们创建了几个 Azure 函数,每个函数在将异常记录到 Application Insights 时,都会将堆栈跟踪记录到单独的日志条目中,如下所示:
我的 Serilog 配置:
}).ConfigureLogging((hostingContext, logging) =>
{
var sp = logging.Services.BuildServiceProvider();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Worker", LogEventLevel.Warning)
.MinimumLevel.Override("Host", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Error)
.MinimumLevel.Override("Function", LogEventLevel.Debug)
.MinimumLevel.Override("Azure.Storage", LogEventLevel.Error)
.MinimumLevel.Override("Azure.Core", LogEventLevel.Error)
.MinimumLevel.Override("Azure.Identity", LogEventLevel.Error)
.Enrich.WithProperty("Application", "Flight Data Functions")
.Enrich.FromLogContext()
.WriteTo.Console(LogEventLevel.Warning)
.WriteTo.ApplicationInsights(sp.GetRequiredService<TelemetryClient>(), TelemetryConverter.Traces, LogEventLevel.Warning)
.CreateLogger();
logging.AddSerilog(Log.Logger, true);
})
主机.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
},
"enableLiveMetricsFilters": true
}
},
"extensions": {
"http": {
"routePrefix": ""
}
}
}
我们正在使用 Serilog:
<PackageReference Update="Serilog" Version="4.0.1" />
<PackageReference Update="serilog.extensions.logging" Version="8.0.0" />
<PackageReference Update="Serilog.AspNetCore" Version="8.0.2" />
<PackageReference Update="Serilog.Enrichers.Environment" Version="3.0.1" />
<PackageReference Update="Serilog.Enrichers.Process" Version="3.0.0" />
<PackageReference Update="Serilog.Enrichers.Thread" Version="4.0.0" />
<PackageReference Update="Serilog.Settings.Configuration" Version="8.0.2" />
<PackageReference Update="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
<PackageReference Update="Serilog.Sinks.Async" Version="2.0.0" />
<PackageReference Update="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Update="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Update="Serilog.Sinks.Seq" Version="8.0.0" />
要使用 Serilog 启用日志记录,将堆栈跟踪的每一行记录为 Application Insights 和 Azure Functions 中的单独日志条目,请执行以下步骤:
添加以下内容以将整个堆栈跟踪格式化为单个日志条目。
try
{
// Your function code that may throw an sample exception .
}
catch (Exception ex)
{
string fullExceptionDetails = $"{ex.GetType()}: {ex.Message}{Environment.NewLine}{ex.StackTrace}";
Log.Fatal(fullExceptionDetails);
}
请参阅此文章配置Serilog以登录AzureFunctions。
在 Serilog 配置中,使用自定义转换器来控制发送到 Application Insights 的日志。更新 Serilog 配置如下:
.ConfigureLogging((hostingContext, logging) =>
{
var sp = logging.Services.BuildServiceProvider();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Function", LogEventLevel.Debug)
.Enrich.WithProperty("Application", "Flight Data Functions")
.Enrich.FromLogContext()
.WriteTo.Console(LogEventLevel.Warning)
.WriteTo.ApplicationInsights(
sp.GetRequiredService<TelemetryClient>(),
new CustomTelemetryConverter(),
LogEventLevel.Warning)
.CreateLogger();
logging.AddSerilog();
})
输出: