Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Initialize the logger in the constructor:
- class SomethingAbstract
- {
- protected ILogger Log { get; }
- protected SomethingAbstract()
- {
- Log = Log.ForContext(GetType());
- }
- }
- Or another variation, if you want to ensure messages from the base class are tagged with that type:
- abstract class SomethingAbstract
- {
- readonly ILogger _log = Log.ForContext<SomethingAbstract>();
- // Here's your base class implementation, using _log for logging
- }
- abstract class SomethingToDeriveFrom : SomethingAbstract
- {
- protected ILogger Log { get; }
- protected SomethingToDeriveFrom()
- {
- Log = Log.ForContext(GetType());
- }
- }
- // Derived classes inherit from SomethingToDeriveFrom and use Log for logging
Add Comment
Please, Sign In to add comment