NLog
NLog copied to clipboard
Logging not working when using Azure SDK from a library project in .NET Core (Dependency Injection)
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:
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.
Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!
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)
Closing due to inactivity.