How to get an ILogger instance without Dependency Injection in Program.cs during Startup.
home / developersection / forums / how to get an ilogger instance without dependency injection in program.cs during startup.
How to get an ILogger instance without Dependency Injection in Program.cs during Startup.
Aryan Kumar
08-Nov-2023In some cases, you may want to obtain an ILogger instance without using dependency injection, such as in the Program.cs file during startup. You can do this by creating a logger manually using the LoggerFactory class. Here's how to obtain an ILogger instance without dependency injection in the Program.cs file:
Add the Required Using Statements:
At the top of your Program.cs file, make sure you have the necessary using statements to access the required classes:
Create an ILogger Instance:
In the Main method of your Program.cs file, you can create an ILogger instance manually using the LoggerFactory class:
In the code above:
This approach allows you to create and use an ILogger instance in the Program.cs file without relying on dependency injection. You can customize the logger configuration and logging provider to suit your application's needs.