-
Notifications
You must be signed in to change notification settings - Fork 1.4k
How to create Logger for sub classes
Rolf Kristensen edited this page Mar 25, 2021
·
5 revisions
When we wish to expose the logger into sub classes the following pattern could be used.
class BaseClass
{
protected BaseClass()
{
Log = LogManager.GetLogger(GetType().ToString());
}
protected Logger Log { get; private set; }
}
class ExactClass : BaseClass
{
public ExactClass() : base() { }
...
}
Alternative solution could be this:
class BaseClass
{
protected virtual Logger Log { get { return _logger; } }
private static Logger _logger = LogManager.GetCurrentClassLogger();
}
class ExactClass : BaseClass
{
protected override Logger Log { get { return _logger; } }
private static Logger _logger = LogManager.GetCurrentClassLogger();
}
- Troubleshooting Guide - See available NLog Targets and Layouts: https://nlog-project.org/config
- Getting started
- How to use structured logging
- Troubleshooting
- FAQ
- Articles about NLog
-
All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json