Serilog App Insights 通过 OperationId Azure Functions 关联日志

问题描述 投票:0回答:1

我已经设法通过 Serilog 将我的函数应用程序日志记录到 Azure,但它没有正确关联/分组日志。我尝试过使用像这样的加密器:

public class OperationIdEnrichment : ILogEventEnricher
{
    public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
    {
        logEvent.AddOrUpdateProperty(new LogEventProperty("OperationId", new ScalarValue(Guid.NewGuid())));
        logEvent.AddOrUpdateProperty(new LogEventProperty("ParentId", new ScalarValue(Guid.NewGuid())));
    }
}

创建了遥测转换器

public class OperationTelemetryConverter : TraceTelemetryConverter
{
    private const string OperationId = "OperationId";
    private const string ParentId = "ParentId";

    public override IEnumerable<ITelemetry> Convert(LogEvent logEvent, IFormatProvider formatProvider)
    {
        foreach (var telemetry in base.Convert(logEvent, formatProvider))
        {
            if (TryGetScalarProperty(logEvent, OperationId, out var operationId))
            {
                telemetry.Context.Operation.Id = operationId.ToString();
            }

            if (TryGetScalarProperty(logEvent, ParentId, out var parentId))
            {
                telemetry.Context.Operation.ParentId = parentId.ToString();
            }

            yield return telemetry;
        }
    }

    private bool TryGetScalarProperty(LogEvent logEvent, string propertyName, out object value)
    {
        var hasScalarValue = logEvent.Properties.TryGetValue(propertyName, out var someValue) && (someValue is ScalarValue);
            
        value = hasScalarValue ? ((ScalarValue)someValue).Value : default;

        return hasScalarValue;
    }
}

使用

将其连接起来
var config = TelemetryConfiguration.Active;
Log.Logger = new LoggerConfiguration()
     .MinimumLevel.Information()
     .Enrich.FromLogContext()
     .Enrich.With<OperationIdEnrichment>()
     .Enrich.WithExceptionDetails()
     .WriteTo.ApplicationInsights(config, new OperationTelemetryConverter(), LogEventLevel.Information)
     .CreateLogger();

var host = new HostBuilder()
    .UseSerilog()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices((hostContext, services) =>
    {
        services.AddLogging(x => x.AddSerilog());        
    })
    .Build();

host.Run();

enter image description here

我在这里缺少什么吗?

c# azure-functions azure-application-insights serilog
1个回答
0
投票

设法解决这个问题。我摆脱了浓缩器和遥测转换器,所以我的包裹现在看起来像

        <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.22.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.1" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.13.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
        <PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />
        <PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
        <PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
        <PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />

连接起来很简单

var config = TelemetryConfiguration.Active;
Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Information()
        .Enrich.FromLogContext()
    .WriteTo.ApplicationInsights(config, TelemetryConverter.Traces, LogEventLevel.Information)
    .CreateLogger();

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureServices(x =>
    {
        x.AddLogging(y => y.AddSerilog());
    })
    .UseSerilog()
    .Build();

host.Run();

结果是,我对

logger.Log()
的使用被附加到底层操作中。我没有自己设置它,这是我想避免做的事情。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.