How to Get an ILogger in a Class with Constructor Injection in C#
A class that does real work should be able to log what it does. In .NET, it doesn't create a logger itself: it asks for an ILogger<T> in its constructor, and dependency injection passes one in, already set up with the app's logging.
Syntax
class <ClassName>
{
private readonly ILogger<<ClassName>> _logger;
public <ClassName>(ILogger<<ClassName>> logger)
{
_logger = logger;
}
}
| Part | What it does |
|---|---|
ILogger<<ClassName>> | A logger whose category is the class's name, so every line it writes says where it came from |
| The constructor parameter | Tells dependency injection that the class needs a logger. You don't pass it yourself |
_logger = logger; | Keeps it in a readonly field for the class's methods to use |
Microsoft's .NET logging overview: "If your application uses Dependency Injection (DI) or a host such as ASP.NET's WebApplication or Generic Host, use ILoggerFactory and ILogger objects from the DI container rather than creating them directly." And: "The DI container locates an ILogger with the correct category and supplies it as the constructor argument."
The Example
An OrderService logs a line each time an order is placed. The program uses the .NET generic host, which sets up dependency injection and logging to the console, registers the service, gets it back and places one order.
Prerequisites
- The .NET 10 SDK or newer, which runs a single
.csfile withdotnet run. This guide uses .NET SDK 10.0.404.
Step 1: Write the Code
Create a file named main.cs in an empty folder:

#:package Microsoft.Extensions.Hosting@10.0.15
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddTransient<OrderService>();
using IHost host = builder.Build();
OrderService orders = host.Services.GetRequiredService<OrderService>();
orders.Place("Tea");
class OrderService
{
private readonly ILogger<OrderService> _logger;
public OrderService(ILogger<OrderService> logger)
{
_logger = logger;
}
public void Place(string item)
{
_logger.LogInformation("Placed an order for {Item}.", item);
}
}
#:packageadds theMicrosoft.Extensions.HostingNuGet package to this one-file program. The first run downloads it.Host.CreateApplicationBuilder(args)sets up dependency injection and logging, with the console as one of the outputs.AddTransient<OrderService>()registers the class, so the host knows how to create it.GetRequiredService<OrderService>()asks for one. The host sees that the constructor needs anILogger<OrderService>and passes it in.
Step 2: Run It
dotnet run main.cs
info: OrderService[0]
Placed an order for Tea.
![A zsh terminal where dotnet run main.cs prints info: OrderService[0] and, indented below it, Placed an order for Tea.](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/csharp/2026/cs-ilogger-injection-zsh.webp)
infois the log level ofLogInformation.OrderServiceis the category, taken fromILogger<OrderService>.Placed an order for Tea.is the message, with{Item}filled in.
The program never created the logger: the host did, and handed it to the constructor.
The command is the same in zsh, bash and PowerShell.
The Class Must Be Registered
Without the AddTransient<OrderService>() line, the host doesn't know the class:
Unhandled exception. System.InvalidOperationException: No service for type 'OrderService' has been registered.
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Program.<Main>$(String[] args) in /Users/you/demo/main.cs:line 10
Not with new
Creating the class yourself with new OrderService() doesn't compile, because nothing passes the logger:
/Users/you/demo/main.cs(4,27): error CS7036: There is no argument given that corresponds to the required parameter 'logger' of 'OrderService.OrderService(ILogger<OrderService>)'
The build failed. Fix the build errors and run again.
Let the host create the class, as in Step 1, and it creates the logger along with it.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Forgetting to register the class | InvalidOperationException: No service for type 'OrderService' has been registered. | builder.Services.AddTransient<OrderService>(); |
Creating the class with new | error CS7036: There is no argument given that corresponds to the required parameter 'logger' | Get it from the host's services |
Writing ILogger<SomethingElse> | The log lines carry the wrong category | Use the class's own name |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
No service for type '...' has been registered. | The class wasn't added to builder.Services | Register it before Build() |
CS7036 ... required parameter 'logger' | The class was created with new | Let dependency injection create it |
FAQ
Why ILogger of the class, and not just ILogger?
The type in the brackets becomes the category, which the console shows before each message: OrderService above. In a bigger app, that's how you know which class wrote a line, and how you set log levels per class.
Does this work in ASP.NET Core?
Yes. The guide's own words name both hosts: "ASP.NET's WebApplication or Generic Host". In ASP.NET Core, WebApplication.CreateBuilder sets up the same dependency injection and logging.
Conclusion
Ask for ILogger<YourClass> in the class's constructor, keep it in a readonly field, register the class in builder.Services, and let the host create it. Each line the class logs then carries its name as the category.
Additional Resources
- Logging in C# and .NET, on Microsoft Learn.
- Dependency injection in .NET, on Microsoft Learn.
