Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Loop Inside a dotnet using stament

I have this code

public static void AddMeter(List<string> meter)
{
    using MeterProvider meterProvider = Sdk.CreateMeterProviderBuilder()
            .AddMeter("Meter.Errors")
            .AddMeter("Meter.Prompts")
            .AddPrometheusHttpListener(options => options.UriPrefixes = new string[] { "http://*:9184/" })
            .Build();
}

And What I am trying to do is adding meters (.AddMeter()) by iteration.

so something like this;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

using MeterProvider meterProvider = Sdk.CreateMeterProviderBuilder()
        foreach (var meter in meters)
        {
            .AddMeter(meter)
        }
        .AddPrometheusHttpListener(options => options.UriPrefixes = new string[] { "http://*:9184/" })
        .Build();

What would be the correct synax for this?

>Solution :

Do you mean something like this?:

var builder = Sdk.CreateMeterProviderBuilder();
foreach (var meter in meters)
{
    builder = builder.AddMeter(meter);
}
using MeterProvider meterProvider = builder
    .AddPrometheusHttpListener(options => options.UriPrefixes = new string[] { "http://*:9184/" })
    .Build();

Basically you just store the result of Sdk.CreateMeterProviderBuilder() in a variable, call .AddMeter() in a loop updating that variable, and then call the rest of what you need on the result of that variable. There’s nothing particularly special about the syntax of a fluid API like this, each method is simply returning the same updated object which can be stored in a variable like any other.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading