Search code examples
azure.net-coreazure-application-insights

How to add custom Namespace to Telemetry events


According to this post, I should be able to attach a custom namespace property to the baseData object. But the TelemetryClient.TrackEvent method and its overloads do not seem to provide a way to specify the namespace property. Now, all my custom events are categorized under the azure.applicationinsights namespace.


Solution

  • For metrics, you should use GetMetric and TrackValue methods to send the custom metrics. The TrackEvent method is actually for custom events.

    To specify the custom namespace property, you should use the code below:

    // Note, add "using Microsoft.ApplicationInsights.Metrics;" to use MetricIdentifier
    
    MetricIdentifier id = new MetricIdentifier("Custom Metric Namespace","ComputerSold", "FormFactor", "GraphicsCard", "MemorySpeed", "BatteryCapacity", "StorageCapacity");
    Metric computersSold  = _telemetryClient.GetMetric(id);
    computersSold.TrackValue(110,"Laptop", "Nvidia", "DDR4", "39Wh", "1TB");
    

    The related doc is here.

    Another way is to use the TrackMetric method, but it is not recommended now(see here for the reason). Anyway, if using TrackMetric method, the code looks like below:

            MetricTelemetry metric = new MetricTelemetry();
            metric.MetricNamespace = "xxxxx";
            client.TrackMetric(metric);