Hello,
Looking for a way to have the derived class context be used when logging in the derived class. Is this possible?
Thank you,
Stephen
Yes, I think so, if you 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
Very satisfied with the quick response, Thanks Nick!