You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8隔离模式Azure Function:App Insights缺失operation_Name日志

解决.NET 8 Isolated Azure Function App Insights缺失operation_Name的问题

全局修复方案:自定义TelemetryInitializer

创建一个实现ITelemetryInitializer的类,从函数执行上下文中提取函数名称,自动注入到operation_Name字段,无需逐个修改日志代码:

using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.Functions.Worker;

public class FunctionOperationNameInitializer : ITelemetryInitializer
{
    private readonly FunctionContext _functionContext;

    public FunctionOperationNameInitializer(FunctionContext functionContext)
    {
        _functionContext = functionContext;
    }

    public void Initialize(ITelemetry telemetry)
    {
        if (string.IsNullOrEmpty(telemetry.Context.Operation.Name))
        {
            // 从函数上下文提取当前函数名称
            var functionName = _functionContext.FunctionDefinition.Name;
            telemetry.Context.Operation.Name = functionName;
            
            // 同时将operation_Name写入属性字段,确保查询兼容性
            if (telemetry is ISupportProperties propTelemetry)
            {
                propTelemetry.Properties["operation_Name"] = functionName;
            }
        }
    }
}

在HostBuilder的服务配置中注册这个初始化器:

.ConfigureServices(services =>
{
    services.AddApplicationInsightsTelemetryWorkerService();
    services.ConfigureFunctionsApplicationInsights();
    // 注册自定义TelemetryInitializer
    services.AddScoped<ITelemetryInitializer, FunctionOperationNameInitializer>();
    services.AddHttpClient();
    [...]
})

局部手动添加方案:为ILogger日志附加operation_Name

如果不需要全局处理,可在单个函数内手动为日志添加operation_Name属性:

方式1:单条日志直接附加属性

public class MyFunction
{
    private readonly ILogger<MyFunction> _logger;

    public MyFunction(ILogger<MyFunction> logger)
    {
        _logger = logger;
    }

    [Function("MyFunction")]
    public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
    {
        _logger.LogInformation("函数启动", new { operation_Name = "MyFunction" });
        
        // 业务逻辑
        var response = req.CreateResponse(HttpStatusCode.OK);
        await response.WriteStringAsync("Hello World");
        
        _logger.LogInformation("函数执行完成", new { operation_Name = "MyFunction" });
        return response;
    }
}

方式2:用BeginScope批量附加属性

如果函数内有多条日志,可通过BeginScope一次性为所有日志添加属性:

[Function("MyFunction")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
{
    using (_logger.BeginScope(new Dictionary<string, object> { { "operation_Name", "MyFunction" } }))
    {
        _logger.LogInformation("函数启动");
        // 业务逻辑
        var response = req.CreateResponse(HttpStatusCode.OK);
        await response.WriteStringAsync("Hello World");
        
        _logger.LogInformation("函数执行完成");
        return response;
    }
}

额外说明

该问题属于.NET Isolated Worker的已知未修复问题,上述方案可作为临时替代方案满足日志查询需求。

内容的提问来源于stack exchange,提问作者Wildly Stunted

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 18:05:17