Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stephenpatten/2502caaec4c303fb3c26905044ad65c0 to your computer and use it in GitHub Desktop.
Save stephenpatten/2502caaec4c303fb3c26905044ad65c0 to your computer and use it in GitHub Desktop.
Serilog abstract init
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