0

What is the difference between ILogger<T> and ILogger? Below are two ways the Sample class using ILogger, which one is the best and why!

Method 1

public class Sample
{
  private readonly ILogger<Sample> _logger;

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

  public void DoSomething()
  {
    _logger.LogInformation("The answer is {number}", 42);
  }
}

Method 2

public class Sample
{
  private readonly ILogger _logger;

  public Sample(ILogger logger)
  {
    _logger = logger;
  }

  public void DoSomething()
  {
    _logger.LogInformation("The answer is {number}", 42);
  }
}
Steven
  • 166,672
  • 24
  • 332
  • 435
Rag Pudi
  • 33
  • 1
  • 7
  • 1
    This has already been discussed : https://stackoverflow.com/questions/51345161/should-i-take-ilogger-iloggert-iloggerfactory-or-iloggerprovider-for-a-libra – granadaCoder Dec 14 '21 at 22:43
  • 1
    Does this answer your question? [Should I take ILogger, ILogger, ILoggerFactory or ILoggerProvider for a library?](https://stackoverflow.com/questions/51345161/should-i-take-ilogger-iloggert-iloggerfactory-or-iloggerprovider-for-a-libra) – Gabriel Luci Dec 15 '21 at 00:27

1 Answers1

0

As per usage of ILogger in your .net core which is depends of your requirements.

ILogger

ILogger is a logger that your code can use to write log messages.

There are three core methods:

IsEnabled tests whether a log level is enabled on that logger;

Log is the core logging method that is used to write log messages;

BeginScope defines a logging scope.

There’s a bunch of logging extension methods that build on that core; the common methods like LogInformation are just wrappers around Log, with the appropriate arguments.

Internally, an ILogger has a “name” (also called a “category”). The idea is that each ILogger instance is used by a different component of the application.

ILogger is a base interface that provides the core logging functionality, but it is seldom used directly. There are some exceptions (e.g., Azure Functions will pass you an ILogger), but most of the time your code will log to an ILogger<T>.

ILogger

ILogger<T> is a logger that is named after a type T. All logs sent to an ILogger<T> (with the default implementation) will have a logger name/category of typeof(T).FullName. ILogger<T> is derived from ILogger and adds no new functionality.

If you’re using dependency injection, an instance of ILogger<T> is usually injected into your type T. So, each time you have a constructor that takes an ILogger<T>, you are defining a “component” for your application.

Majar usage of difference

If you want to log your data as Typed log (serilog, etc., ) you can use ILogger< T> or you want to use untyped logs you can simply use ILogger

ASP.NET Core apps, encourage you to use standard Serilog interfaces like Log, ILogger, and LogContext if they meet your needs.

Alternatively, your app can consume the ILogger interface from the framework via dependency injection. Serilog implements this interface, so the results are identical for practical purposes.

The ILoggerFactory logger factory instance is the boostrapper of the logging system: It is used to attach logger providers and create logger instances - either typed (ILogger<T>) or untyped (ILogger). These logger instances will log to all registered logger providers.

Refer Ilogger best practices & Usage of logging

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15