# 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
LevelMessageLoggerPropertiesCallsiteException
InfoInitializing HomeViewModelSignifyHR.Areas.Portal.ViewModels.HomeViewModel SignifyHR.Areas.Portal.ViewModels.HomeViewModel..ctor
InfoInitializing HomeViewModelLMSActiveTab=TrainingRequirementsSignifyHR.Areas.Portal.ViewModels.HomeViewModel..ctor
InfoTesting EventPropertiesSignifyHR.Areas.Portal.ViewModels.HomeViewModelKey=ValueSignifyHR.Areas.Portal.ViewModels.HomeViewModel..ctor
InfoTesting dynamic EventProperty Key="PropertyValue"SignifyHR.Areas.Portal.ViewModels.HomeViewModelPropertyName=PropertyValue|Key=ValueSignifyHR.Areas.Portal.ViewModels.HomeViewModel..ctor
WarnExceptionSignifyHR.Areas.Portal.ViewModels.HomeViewModelKey=ValueSignifyHR.Areas.Portal.ViewModels.HomeViewModel..ctorSystem.Exception: Example Exception
##### Log Levels
LevelTypical Use
FatalSomething bad happened; application is going down
ErrorSomething failed; application may or may not continue
WarnSomething unexpected; application will continue
InfoNormal behavior like mail sent, user updated profile etc.
DebugFor debugging; executed query, user authenticated, session expired
TraceFor 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 --> ```