Fix Semantic Kernel logging when using Dependency Injection

Integrating Semantic Kernel into ASP.NET applications is straightforward, due to built-in support for Dependency Injection (DI). This makes well-known features such as configuration, logging, and hosting trivial to use for AI Agent development. However, while debugging an agent issue, I discovered that all logging information from the Semantic Kernel was missing! This article explains issue’s root cause and how to fix it.

The Original Setup

Here is a simplified version of the code I used to set up and use the Semantic Kernel in my ASP.NET application. Please note how IChatCompletionService and Kernel services are registered within DI container.

We use Agent to complete a chat based on user’s prompt. All dependencies are injected into the Agent constructor automatically.

The Problem

After detecting an unrelated issue in request processing, I turned to good old console logs to find the root cause. To my surprise, I noticed no logging information was generated by the Kernel.

No Logging

At first, I thought it was a configuration issue. But no matter how I configured the logging, nor what scope I set, the Kernel information was missing from the console. I even tried to set the logging level to Debug or Verbose, but nothing worked.

The Solution

It turned out that the issue was not in the Kernel itself, but in the way how underlying AzureOpenAIChatCompletionService was created. Even though the _kernel instance was used to call GetChatMessageContentAsync() method of IChatCompletionService, logging provider registered in DI container was not used. The solution was to provide it during registration of the AzureOpenAIChatCompletionService instance.

And whoila! Now we can see the debug information in the console!

Debug Logging

Conclusion

Even though the Semantic Kernel is designed to work with DI, it does not mean other parts of .NET AI stack are. However, in this case, the solution was simple. Just make sure to pass the logging factory to the IChatCompletionService implementation.

Deploy and use DeepSeek R1 with Azure and .NET

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×