Skip to main content

Extensions and Tools

Logging

Using NLog

public class HomeViewModel : PortalMasterModel
{
    private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
    private static readonly Logger _lmsLogger = LogManager.GetLogger("LMS");
 
    public HomeViewModel()
    {
        // Using the class name as the name of the logger
        _logger.Info("Initializing HomeViewModel");
 
        // Using a new logger instance with a property automatically added
        _lmsLogger.WithProperty("ActiveTab", activeTab).Info("Initializing HomeViewModel");
 
        // Setting a property permanently on a logger
        _logger.SetProperty("Key", "Value");
        _logger.Info("Testing EventProperties");
 
        // Adding properties automatically in the message
        _logger.Info("Testing dynamic EventProperty Key={PropertyName}", "PropertyValue");
 
        // Logging an exception
        _logger.WarnException("Exception", new System.Exception("Example Exception"));
    }
}
Example of log levels
Level Message Logger Properties Callsite Exception
Info Initializing HomeViewModel SignifyHR.Areas.Portal.ViewModels.HomeViewModel   SignifyHR.Areas.Portal.ViewModels.HomeViewModel..ctor  
Info Initializing HomeViewModel LMS ActiveTab=TrainingRequirements SignifyHR.Areas.Portal.ViewModels.HomeViewModel..ctor  
Info Testing EventProperties SignifyHR.Areas.Portal.ViewModels.HomeViewModel Key=Value SignifyHR.Areas.Portal.ViewModels.HomeViewModel..ctor  
Info Testing dynamic EventProperty Key="PropertyValue" SignifyHR.Areas.Portal.ViewModels.HomeViewModel PropertyName=PropertyValue|Key=Value SignifyHR.Areas.Portal.ViewModels.HomeViewModel..ctor  
Warn Exception SignifyHR.Areas.Portal.ViewModels.HomeViewModel Key=Value SignifyHR.Areas.Portal.ViewModels.HomeViewModel..ctor System.Exception: Example Exception
Log Levels
Level Typical Use
Fatal Something bad happened; application is going down
Error Something failed; application may or may not continue
Warn Something unexpected; application will continue
Info Normal behavior like mail sent, user updated profile etc.
Debug For debugging; executed query, user authenticated, session expired
Trace For trace debugging; begin method X, end method X
Rules

A rule is a logger element with the following attributes:

  • name – logger name filter - may include wildcard characters (* and ?)
  • minlevel – minimal level to log
  • maxlevel – maximum level to log
  • level – single level to log
  • levels - comma separated list of levels to log
  • writeTo – comma separated list of targets to write to
  • final – no rules are processed after a final rule matches
  • enabled - set to false to disable the rule without deleting it
  • ruleName - rule identifier to allow rule lookup with Configuration.FindRuleByName and Configuration.RemoveRuleByName.
NLog.config
!--
    Create a rule to only log entries from the LMS HomeViewModel logger,
    using the logDatabase target, and applicable on log levels Info and above
-->
<logger name="SignifyHR.Areas.Portal.ViewModels.HomeViewModel" minlevel="Info" writeTo="logDatabase" enabled="true" final="true" />