Guest User

Untitled

a guest
Apr 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. Initialize the logger in the constructor:
  2.  
  3. class SomethingAbstract
  4. {
  5. protected ILogger Log { get; }
  6.  
  7. protected SomethingAbstract()
  8. {
  9. Log = Log.ForContext(GetType());
  10. }
  11. }
  12.  
  13. Or another variation, if you want to ensure messages from the base class are tagged with that type:
  14.  
  15. abstract class SomethingAbstract
  16. {
  17. readonly ILogger _log = Log.ForContext<SomethingAbstract>();
  18.  
  19. // Here's your base class implementation, using _log for logging
  20. }
  21.  
  22. abstract class SomethingToDeriveFrom : SomethingAbstract
  23. {
  24. protected ILogger Log { get; }
  25.  
  26. protected SomethingToDeriveFrom()
  27. {
  28. Log = Log.ForContext(GetType());
  29. }
  30. }
  31.  
  32. // Derived classes inherit from SomethingToDeriveFrom and use Log for logging
Add Comment
Please, Sign In to add comment