.net Azure Function应用在Application Insights中记录自定义属性

2ekbmq32  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(151)

我正在使用.net 6与ServiceBusTrigger Azure Function应用程序。我正在从我的函数应用程序内部使用Microsoft.Extensions.Logging.ILogger将Trace消息记录到Application Insights。这很好。例如。
第一个月
但我想得到一些自定义属性也包括在内。
System.Diagnostics.Activity.Current.AddBaggage()但Current始终为null。
所以我尝试了BeginScope这样,这也不起作用。

object customProps = new
                {
                    OrderNumber = "something",
                    TransactionType = transactionType,
                };

                using (_logger.BeginScope(new Dictionary<string, object> { 
                    ["CustomerId"] = 12345)
                {
                    _logger.LogInformation("abc ");
                    _logger.LogInformation("def  {properties}", customProps);
                }

字符串
然后我尝试在LogInformation方法上使用第二个parm,这也不起作用。
_logger.LogInformation("xyz {properties}", customProps);
消息被记录,它只是没有任何自定义属性。
有没有人有任何想法可以尝试?我现在没有使用任何应用程序洞察nuget包。只是默认的ILogger。我也尝试了dotnet-isolated和non-isolated。

  • 兰迪
g9icjywg

g9icjywg1#

我用运行时堆栈dotnet创建了服务总线队列触发器函数。
我已经创建了服务总线队列,并从服务总线资源管理器发送了一条消息。

功能编码:

using System;
using System.Collections.Generic;
using Azure.Messaging.ServiceBus;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace FunctionApp8121
{
    public class Function1
    {
        private readonly ILogger<Function1> _logger;
        private readonly IConfiguration _config;
        private readonly TelemetryClient _telemetryClient;

        [Obsolete]
        public Function1(ILogger<Function1> logger, IConfiguration config)
        {
            _logger = logger;
            _config = config;
            string instrumentationKey = _config["ApplicationInsights:InstrumentationKey"];
            _telemetryClient = new TelemetryClient(new TelemetryConfiguration(instrumentationKey));
        }

        [Function(nameof(Function1))]
        [Obsolete]
        public void Run([ServiceBusTrigger("firstqueue", Connection = "servicebusconnection")] ServiceBusReceivedMessage message)
        {
            _logger.LogInformation("Message ID: {id}", message.MessageId);
            _logger.LogInformation("Message Body: {body}", message.Body);
            _logger.LogInformation("Message Content-Type: {contentType}", message.ContentType);

            // Log custom events to Application Insights
            _telemetryClient.TrackEvent("MessageProcessed", new Dictionary<string, string>
            {
                { "MessageID", message.MessageId },
                { "MessageType", message.ContentType },
            });

            // Log custom properties
            var properties = new Dictionary<string, string>
            {
                { "FirstName", "pavan" },
                { "LastName", "kumar" },
            };

            _telemetryClient.TrackTrace("User Details", SeverityLevel.Information, properties);

            // Track custom metric
            _telemetryClient.TrackMetric("CustomMetric", 42);

            // Automatically flush telemetry data to Application Insights (no need for manual Flush)
        }
    }
}

字符串

local.settings.json:

{
    "IsEncrypted": false,
  "Values": {
    "servicebusconnection": "your-servicebus connection string",
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "ApplicationInsights:InstrumentationKey": "your instrumentation key"

  }
}

输出:

我在下面的应用程序洞察检查日志:



我能够得到自定义属性。检查下面:

输出:


相关问题