ASP.NET Core has built-in support for logging, but the documentation states that logging should be done by requesting an ILogger
via dependency injection, i.e. by adding it as an argument to the Controller
constructor.
Polluting method signatures or constructors throughout my code with ILogger
arguments feels like the wrong solution for this cross-cutting concern. In Android the Log
class is static, making it trivial to log from any part of the code.
What would be a good way to do logging from other places than the controller?
ILoggerFactory is a factory interface that we can use to create instances of the ILogger type and register logging providers. It acts as a wrapper for all the logger providers registered to it and a logger it creates can write to all the logger providers at once.
Add log4net in config file config and enter the following details. Add a class Log. cs in the Utilities folder. Now, in the constructor of this class, instantiate logs for monitoring and debugger loggers.
Regarding using logging in any component:
Adding logging to a component in your application is done by requesting either an
ILoggerFactory
or anILogger<T>
via Dependency Injection. If anILoggerFactory
is requested, a logger must be created using itsCreateLogger
method.
If your CustomClass
is a data container (DTO class), it should not know about logging, but just contain data.
For other classes with names like "Service" , "Provider", "Handler" and so on, the best practices is to resolve instances using dependency injection. In general, you should use DI wherever it is possible, as it’s a technique for achieving loose coupling between objects and their collaborators or dependencies. For more information, the following question might be interesting: Should I use Dependency Injection or static factories?
So simply add ILogger<CustomClass>
to its constructor (actually the same way, as you do for controllers), as .NET Core supports only constructor injection by default:
public class CustomClass { private readonly ILogger _logger; public CustomClass(ILogger<CustomClass> logger) { _logger = logger; } }
ASP.NET Core’s built-in dependency injection container will automatically resolved transient dependencies. So if your controller has class A
as a dependency that uses class B
where you want to log something, your code can be something like this:
public class MyController { public MyController(ClassA classA) { ... } } public class ClassA { public ClassA(ClassB classB) { ... } } public class ClassB { private readonly ILogger _logger; public ClassB(ILogger<ClassB> logger) { _logger = logger; } public void DoSomethingWithLogging() { // use _logger } }
Note that you also need to register the dependencies in Startup
using IServiceCollection.Add…
methods:
public void ConfigureServices(IServiceCollection services) { // ... services.AddTransient<ClassB>(); services.AddTransient<ClassA>(); }
Built-in logging support means that .NET Core out of the box knows about and is using built-in abstractions for logging. This is mainly done by ILoggerFactory
and ILoggerProvider
interfaces.
/// <summary> /// Represents a type used to configure the logging system and create instances of <see cref="ILogger"/> from /// the registered <see cref="ILoggerProvider"/>s. /// </summary> public interface ILoggerFactory : IDisposable // <summary> /// Represents a type that can create instances of <see cref="ILogger"/>. /// </summary> public interface ILoggerProvider : IDisposable
Using your own ILoggerProvider
implementation, you can add your own logger that can do whatever you want. You can check the NLog logger implementation as working example.
And thanks to ILoggerFactory
, you can simply configure your Logger for project-specific purposes:
To configure logging in your ASP.NET Core application, you should resolve
ILoggerFactory
in theConfigure
method of yourStartup
class. ASP.NET Core will automatically provide an instance ofILoggerFactory
using Dependency Injection when you add a parameter of this type to theConfigure
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With