dotnet / dotnet/diagnostics

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

Open
#5,673 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
1.3k
Forks
404
Avg merge
2d 5h
Merged PRs (30d)
35

Description

### Description

I followed the documentation on learn.microsoft.com:
- https://learn.microsoft.com/en-us/dotnet/core/diagnostics/metrics-instrumentation

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.<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

```csharp
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();

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();
return serviceCollection.BuildServiceProvider();
}
}

public class HatCoMetrics
{
private readonly Counter _hatsSold;

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

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

_hatsSold.Add(quantity,
new KeyValuePair("product.color", color),
new KeyValuePair("product.size", size)
);
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.