Base solution for your next web application
Open Closed

ILogger Instance is not working in a specific situation #6992


User avatar
0
Leonardo.Willrich created

Hi,

The Logger is not working in some situations. I wonder if someone else has the same problem and what I am missing to make it works.

Let me start explaining where I am using the Logger instance. I have a couple of classes that are instanced in the startup program and are used to read notifications from the database to update changes in my clients.

In these classes, I am creating a Logger instance to be able to catch exceptions and write down in the log file, as per the log4net.config configuration file. Those classes are located in the MVC project.

Basically, I have a local private property: private ILogger Logger { get; set; }

In the create method I assign this property like that: Logger = NullLogger.Instance;

and then writing the log like that: Logger.Error("Log Test ", ex);

My class is declared like that: public class AvaQueueHubService : ITransientDependency

And finally, creating an instance of my class in the Statup.cs, method Configure(), like that: _AvaQueueHubService = new AvaQueueHubService();

I tried to create the instance like that as well:

_CallLogHubService = serviceProvider.GetService<AvaQueueHubService>();

Does someone have any idea what I am missing?

Best regards!


1 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    The NullLogger instance that you explicitly assigned seems to be working as expected.

    If you want property injection, then make the property public. Read: https://aspnetboilerplate.com/Pages/Documents/Dependency-Injection#property-injection-pattern

    You have to inject or resolve the requesting class (e.g. via GetService), not new it.

    You should do it in your module's PostInitialize method, instead of the Startup class.