79435969

Date: 2025-02-13 11:25:50
Score: 0.5
Natty:
Report link

But this now means that every class in my system that needs to log, needs to get this LogService injected into it, which seems redundant.

I wouldnt count injecting logger to consumers as redundant or an issue unless injection is creating logger instances each time. Being redundant in this case (injecting ilogger to each consumer) actually may help to readability and tests.

Option 1 inject iloggable/ilogger to consumers; Add iloggable/ilogger to container/provider then get it from the provider via constructor when needed, which you think redundant and cumbersome :). If needed you can also implement logger factory/provider so multiple loggers can be used simultaneously. Asp.net for instance similarly use ILogger and it's usually injected to consumers with constructor DI.

Option2 use singleton; If you insist on a static option; Create a singleton logger service/wrapper with iloggable/ilogger injected then use the service from anywhere without di. Readability, tests may be bit worse off than having Ilogger injected to consumers.

Reasons:
  • Long answer (-1):
  • No code block (0.5):
  • Low reputation (1):
Posted by: devmet34