Created
April 13, 2018 01:36
-
-
Save stephenpatten/2502caaec4c303fb3c26905044ad65c0 to your computer and use it in GitHub Desktop.
Serilog abstract init
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment