Hi, I am trying to instrument c# APIs to Signoz wi...
# support
d
Hi, I am trying to instrument c# APIs to Signoz with some success. I have deployed Signoz to a K8s cluster and can login to the frontend. K8s pods logs are working. I have downloaded the example code from Gitub for .Net and am able to see the HTTP trace info from my app. I am not able to see any logs from manually calling this.logger.LogInformation("my log text").
Copy code
public IEnumerable<WeatherForecast> Get()
        {
            var scopeID = Guid.NewGuid().ToString("N");
            Debug.WriteLine("ScopeId: " + scopeID);
            using var scope = this.logger.BeginScope("{Id}", scopeID );

            // Making an http call here to serve as an example of
            // how dependency calls will be captured and treated
            // automatically as child of incoming request.
            var res = HttpClient.GetStringAsync("<http://google.com>").Result;
            var rng = new Random();
            var forecast = Enumerable.Range(1, 5).Select(index => new WeatherForecast
                {
                    Date = DateTime.Now.AddDays(index),
                    TemperatureC = rng.Next(-20, 55),
                    Summary = Summaries[rng.Next(Summaries.Length)],
                })
                .ToArray();

            this.logger.LogInformation(
                "WeatherForecasts generated {count}: {forecasts}",
                forecast.Length,
                forecast);
            this.logger.LogInformation("Dave Test 3", null);
            this.logger.LogCritical("TEstTEst");
               
            return forecast;}
If I do a free search or a fulltext contains 'TEstTEst' in the log query builder, no results are returned. Please point me in the right direction. Thanks
On further testing, I think the problem has something to do with the config in program.cs. The github repo has the old .net way of doing things and I have updated to the new program.cs methods myself. I exoect that I have only configured support for traces and not for direct logging. How would I go about that in the new .net way with only a program.cs and no startup.cs?using OpenTelemetry.Resources;
Copy code
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddOpenTelemetryTracing(tracerProviderBuilder =>
{
    tracerProviderBuilder
        
        .AddConsoleExporter()
        .AddSource("TestPOCAPI")
        .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(builder.Configuration["Otlp:ServiceName"]))
        .AddHttpClientInstrumentation()
        .AddAspNetCoreInstrumentation()
        .AddOtlpExporter(otlpOptions =>
        {
            otlpOptions.Endpoint = new Uri(builder.Configuration["Otlp:Endpoint"]);
        });
});

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at <https://aka.ms/aspnetcore/swashbuckle>
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
Ok I have updated my program.cs to use latest syntax etc. I have added logging explicity, but I cannot find how to add an otlpexporter to the logging config. Is it not implemented, or do I have something wrong in my understanding of how this works?
Copy code
using OpenTelemetry;               using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using OpenTelemetry.Logs;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.



var OtlpEndpoint =  builder.Configuration["Otlp:Endpoint"];

builder.Services.AddOpenTelemetry()
    .WithTracing(builder => builder
        .AddAspNetCoreInstrumentation()
        .AddHttpClientInstrumentation()
        .AddSqlClientInstrumentation()
        .AddConsoleExporter()
        .AddOtlpExporter(otlpOptions =>
        {
            otlpOptions.Endpoint = new Uri(OtlpEndpoint);
        })
        .AddSource("NextSignozPOCAPIExample")
        .SetResourceBuilder(
            ResourceBuilder.CreateDefault()
                .AddService(serviceName: "NextSignozPOCAPIExample")))
    .StartWithHost();


builder.Logging
     .AddOpenTelemetry(options =>
    {
        options.IncludeFormattedMessage = true;
        options.SetResourceBuilder(ResourceBuilder
            .CreateDefault().AddService("NextSignozPOCAPIExample"));
        options.AddConsoleExporter();
        
    });

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at <https://aka.ms/aspnetcore/swashbuckle>
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
p
@Dave Arnoldi How are you planning to send logs to the SigNoz collector? Are you planning to use any log forwarders like Fluentd, Logstash or directly through file? Take a look at this if you are planning to send logs directly via file - https://signoz.io/docs/userguide/collect_logs_from_file/
Also, check this section for logs provider for .NET - https://opentelemetry.io/docs/instrumentation/net/#status-and-releases
d
HI, Thanks so much for the answer. That was the missing piece. For some reason I had the impression that emitting the logs through the standard .Net logging API could push them directly to an otlp endpoint. It makes sense that they would need to go to a log file first. Thanks
p
Awesome! Are you able to see logs in SigNoz now?
d
Hi, no not yet. I am probably going to go with a Fluentd collector. I need to install it on my K8s cluster and implement in my APIs. I'll tackle that on Monday. Thanks again for your help.
p
👍