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

Aperta
#5,673 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
csharp

Direzione di ricerca

Inizia da src/Tools/dotnet-counters/CounterMonitor.cs alla riga 200 e riproduci nella Visual Studio Package Manager Console l’invocazione dotnet-counters monitor -n ConsoleApp --counters HatCo.Store riportata nell’issue. Usa l’esempio .NET 8 fornito per tracciare il percorso del riferimento null, quindi verifica che il monitoraggio del contatore HatCo.Store non termini più con un’eccezione non gestita.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug
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)
        );
    }
}
Lingua principale
C++
Stelle
1.3k
Fork
407
Merge medio
21h 5m
PR unite (30g)
78

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

  • Nessun Dockerfile né file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dotnet/diagnostics

Tutte le issue di dotnet/diagnostics

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.