Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging not working when using Azure SDK from a library project in .NET Core (Dependency Injection) #5517

Closed
elfico opened this issue May 5, 2024 · 3 comments
Labels

Comments

@elfico
Copy link

elfico commented May 5, 2024

Hello team,

I am not quite sure if this is the right place to report this.

I am working on a .NET console app that uses Dependency Injection. The app also uses a Common library that contains abstractions that are shared with all apps in the project.

The Common library uses AzureBlob storage and Azure Service Bus. It contains a static method that, when called, injects these libraries.

Here is the code for the static method:

 public static void AddCommonServices(this IServiceCollection services, IConfiguration Configuration)
 {
     services.AddAzureClients(builder =>
     {
         builder.AddBlobServiceClient(Configuration.GetSection(StorageAccountConfig.ConfigName)["ConnectionString"]);
         builder.AddServiceBusClient(Configuration.GetSection(ServiceBusConfig.ConfigName)["ConnectionString"])
        .ConfigureOptions(opt =>
        {
            opt.TransportType = ServiceBusTransportType.AmqpWebSockets; 
        });
     });

     services.AddLogging();
     services.AddHttpClient();
 }

This static method is then called in the Program.cs of the main app:

private static ServiceProvider BuildDI(IConfiguration configuration)
{
    //create a serviceProvider
    IServiceCollection services = new ServiceCollection();

    services.AddCommonServices(configuration); // static method in CommonServices called here
  
    //add the main program to run
    services.AddSingleton<IRunner, Runner>();

    //add logging

    services.AddLogging(loggingBuilder =>
    {
        loggingBuilder.ClearProviders();
        loggingBuilder.SetMinimumLevel(LogLevel.Trace);
        loggingBuilder.AddNLog(configuration);
    });

    return services.BuildServiceProvider();
}

When I run the main project and use the Log:

private readonly ILogger<Runner> _logger;

public Runner(ILogger<Runner> logger)
{
    _logger = logger;
}

public void DoTask()
{
     _logger.LogInformation("Hello world");
}

The log is not written to the file.

Interestingly, the log folder shows the internal.log files but doesn't show the nlog-all-xxxx-xx-xx.log file.

If I comment out the part of the static file that adds the Azure SDK:

    //services.AddAzureClients(builder =>
    //{
    //    builder.AddBlobServiceClient(Configuration.GetSection(StorageAccountConfig.ConfigName)["ConnectionString"]);
    //    builder.AddServiceBusClient(Configuration.GetSection(ServiceBusConfig.ConfigName)["ConnectionString"])
    //   .ConfigureOptions(opt =>
    //   {
    //       opt.TransportType = ServiceBusTransportType.AmqpWebSockets; //set the transport type to AmqpWebSockets so that the ServiceBusClient uses the port 443
    //   });
    //});

Then the logging works fine.

When I inspect the _logger when running, this is what I see:

Untitled

NLog version: 5.3.2

NLog Extension Version: 5.3.10

Platform: .NET8

Current NLog config (xml or C#, if relevant)

<nlog>
  <targets>
  </targets>
  <rules>
  </rules>
</nlog>
  • What is the current result?
    Only internal.log is written.

  • What is the expected result?
    Both log files should be written.

  • Did you checked the Internal log?
    Yes

  • Please post full exception details (message, stacktrace, inner exceptions)
    N/A

  • Are there any workarounds? Yes

  • Is there a version in which it did work?
    N/A

  • Can you help us by writing an unit test?
    Not sure what to test.

Copy link

welcome bot commented May 5, 2024

Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!

@snakefoot
Copy link
Contributor

snakefoot commented May 9, 2024

Sounds like a question for StackOverflow.com, but always a good idea to check the output from NLog InternalLogger when troubleshooting logging with NLog.

Maybe paste the output from NLog InternalLogger at internalLogLevel="Debug" (Remember to remove any senstive information)

@snakefoot
Copy link
Contributor

Closing due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants