© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
15 replies
Hugh

❔ Creating a class that takes a Logger for both the class and its base class

I'm trying to figure out the best way to use Loggers in my app.

All of the documentation that I've seen says to use Dependency Injection for this, and do something like:

public class MyClass
{
  private readonly ILogger<MyClass> _logger;
  public MyClass(ILogger<MyClass> logger)
  {
    _logger = logger;
  }
}
public class MyClass
{
  private readonly ILogger<MyClass> _logger;
  public MyClass(ILogger<MyClass> logger)
  {
    _logger = logger;
  }
}


However, what I can't figure out is how I would do this when I have a
MyBaseClass
MyBaseClass
and a
MyClass
MyClass
which is a subclass of
MyBaseClass
MyBaseClass
. I can't pass the same object from one constructor to the other.

Thanks
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Creating a method that takes a generic method and its variadic parameters as arguments and call it i
C#CC# / help
11mo ago
❔ base class/abstract for record
C#CC# / help
3y ago
✅ Creating a typewriter function that takes two optional arguments
C#CC# / help
3y ago
✅ Linking a base class that does something (and has children) to a manager
C#CC# / help
7mo ago