Skip to content

Unhandled exception: System.NullReferenceException: Object reference not set to an instance of an object. #5673

@HHobeck

Description

@HHobeck

Description

I followed the documentation on learn.microsoft.com:

and have trouble to use the dotnet-counters monitor tool in the visual studio package manger console.

I'm getting an Unhandled exception: System.NullReferenceException: Object reference not set to an instance of an object.

PM> dotnet-counters monitor -n ConsoleApp --counters HatCo.Store
dotnet-counters : Unhandled exception: System.NullReferenceException: Object reference not set to an instance of an object.
At line:1 char:1
+ dotnet-counters monitor -n ConsoleApp --counters HatCo.Store
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Unhandled excep...e of an object.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
   at Microsoft.Diagnostics.Tools.Counters.CounterMonitor.Monitor(CancellationToken ct, String counters, Int32 processId, Int32 
refreshInterval, String name, String diagnosticPort, Boolean resumeRuntime, Int32 maxHistograms, Int32 maxTimeSeries, TimeSpan duration, 
Boolean showDeltas, String dsrouter) in /_/src/Tools/dotnet-counters/CounterMonitor.cs:line 200
   at System.CommandLine.Command.<>c__DisplayClass32_0.<<SetAction>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Invocation.InvocationPipeline.InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)

PM> dotnet-counters --version
9.0.652701+240cb1ce5bc30594c515206764241d7982e384af

I appreciate any help.

Configuration

Start the following source code in .NET 8.0 and run dotnet-counters in Visual Studio 2022 package manager console

using System.Diagnostics.Metrics;
using Microsoft.Extensions.DependencyInjection;

class Program
{
    static void Main(string[] arguments)
    {
        Console.WriteLine("Press any key to exit");

        using ServiceProvider serviceProvider = CreateServiceProvider();

        var hatCoMetrics = serviceProvider.GetRequiredService<HatCoMetrics>();

        Random random = new();
        while (!Console.KeyAvailable)
        {
            // Pretend our store has a transaction, every 100ms, that sells two size 12 red hats, and one size 19 blue hat.
            Thread.Sleep(random.Next() % 5000);
            hatCoMetrics.HatsSold(random.Next() % 6, "red", 12);
            hatCoMetrics.HatsSold(random.Next() % 6, "blue", 19);
        }
    }

    // Setup a new service provider. This example creates the collection explicitly but you might leverage
    // a host or some other application setup code to do this as well.
    private static ServiceProvider CreateServiceProvider()
    {
        ServiceCollection serviceCollection = new();
        serviceCollection.AddMetrics();
        serviceCollection.AddSingleton<HatCoMetrics>();
        return serviceCollection.BuildServiceProvider();
    }
}

public class HatCoMetrics
{
    private readonly Counter<int> _hatsSold;

    public HatCoMetrics(IMeterFactory meterFactory)
    {
        Meter meter = meterFactory.Create("HatCo.Store");
        _hatsSold = meter.CreateCounter<int>("hatco.store.hats_sold");
    }

    public void HatsSold(int quantity, string color, ushort size)
    {
        if (quantity == 0) return;

        _hatsSold.Add(quantity,
            new KeyValuePair<string, object?>("product.color", color),
            new KeyValuePair<string, object?>("product.size", size)
        );
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions