private void ParseNLogElement(NLogXmlElement nlogElement, string baseDirectory) { InternalLogger.Trace("ParseNLogElement"); nlogElement.AssertName("nlog"); if (nlogElement.GetOptionalBooleanAttribute("useInvariantCulture", false)) { this.DefaultCultureInfo = CultureInfo.InvariantCulture; } this.AutoReload = nlogElement.GetOptionalBooleanAttribute("autoReload", false); LogManager.ThrowExceptions = nlogElement.GetOptionalBooleanAttribute("throwExceptions", LogManager.ThrowExceptions); InternalLogger.LogToConsole = nlogElement.GetOptionalBooleanAttribute("internalLogToConsole", InternalLogger.LogToConsole); #if !NET_CF InternalLogger.LogToConsoleError = nlogElement.GetOptionalBooleanAttribute("internalLogToConsoleError", InternalLogger.LogToConsoleError); #endif InternalLogger.LogFile = nlogElement.GetOptionalAttribute("internalLogFile", InternalLogger.LogFile); InternalLogger.LogLevel = LogLevel.FromString(nlogElement.GetOptionalAttribute("internalLogLevel", InternalLogger.LogLevel.Name)); LogManager.GlobalThreshold = LogLevel.FromString(nlogElement.GetOptionalAttribute("globalThreshold", LogManager.GlobalThreshold.Name)); foreach (var el in nlogElement.Children) { switch (el.LocalName.ToUpper(CultureInfo.InvariantCulture)) { case "EXTENSIONS": this.ParseExtensionsElement(el, baseDirectory); break; case "INCLUDE": this.ParseIncludeElement(el, baseDirectory); break; case "APPENDERS": case "TARGETS": this.ParseTargetsElement(el); break; case "VARIABLE": this.ParseVariableElement(el); break; case "RULES": this.ParseRulesElement(el, this.LoggingRules); break; case "TIME": this.ParseTimeElement(el); break; default: InternalLogger.Warn("Skipping unknown node: {0}", el.LocalName); break; } } }
private bool SetLayoutFromElement(object o, NLogXmlElement layoutElement) { PropertyInfo targetPropertyInfo; string name = layoutElement.LocalName; // if property exists if (PropertyHelper.TryGetPropertyInfo(o, name, out targetPropertyInfo)) { // and is a Layout if (typeof(Layout).IsAssignableFrom(targetPropertyInfo.PropertyType)) { string layoutTypeName = StripOptionalNamespacePrefix(layoutElement.GetOptionalAttribute("type", null)); // and 'type' attribute has been specified if (layoutTypeName != null) { // configure it from current element Layout layout = this.ConfigurationItemFactory.Layouts.CreateInstance(this.ExpandSimpleVariables(layoutTypeName)); this.ConfigureObjectFromAttributes(layout, layoutElement, true); this.ConfigureObjectFromElement(layout, layoutElement); targetPropertyInfo.SetValue(o, layout, null); return(true); } } } return(false); }
/// <summary> /// Parse {Logger} xml element /// </summary> /// <param name="loggerElement"></param> /// <param name="rulesCollection">Rules are added to this parameter.</param> private void ParseLoggerElement(NLogXmlElement loggerElement, IList <LoggingRule> rulesCollection) { loggerElement.AssertName("logger"); var namePattern = loggerElement.GetOptionalAttribute("name", "*"); var enabled = loggerElement.GetOptionalBooleanAttribute("enabled", true); if (!enabled) { InternalLogger.Debug("The logger named '{0}' are disabled"); return; } var rule = new LoggingRule(); string appendTo = loggerElement.GetOptionalAttribute("appendTo", null); if (appendTo == null) { appendTo = loggerElement.GetOptionalAttribute("writeTo", null); } rule.LoggerNamePattern = namePattern; if (appendTo != null) { foreach (string t in appendTo.Split(',')) { string targetName = t.Trim(); Target target = FindTargetByName(targetName); if (target != null) { rule.Targets.Add(target); } else { throw new NLogConfigurationException("Target " + targetName + " not found."); } } } rule.Final = loggerElement.GetOptionalBooleanAttribute("final", false); string levelString; if (loggerElement.AttributeValues.TryGetValue("level", out levelString)) { LogLevel level = LogLevel.FromString(levelString); rule.EnableLoggingForLevel(level); } else if (loggerElement.AttributeValues.TryGetValue("levels", out levelString)) { levelString = CleanSpaces(levelString); string[] tokens = levelString.Split(','); foreach (string token in tokens) { if (!string.IsNullOrEmpty(token)) { LogLevel level = LogLevel.FromString(token); rule.EnableLoggingForLevel(level); } } } else { int minLevel = 0; int maxLevel = LogLevel.MaxLevel.Ordinal; string minLevelString; string maxLevelString; if (loggerElement.AttributeValues.TryGetValue("minLevel", out minLevelString)) { minLevel = LogLevel.FromString(minLevelString).Ordinal; } if (loggerElement.AttributeValues.TryGetValue("maxLevel", out maxLevelString)) { maxLevel = LogLevel.FromString(maxLevelString).Ordinal; } for (int i = minLevel; i <= maxLevel; ++i) { rule.EnableLoggingForLevel(LogLevel.FromOrdinal(i)); } } var children = loggerElement.Children.ToList(); foreach (var child in children) { switch (child.LocalName.ToUpper(CultureInfo.InvariantCulture)) { case "FILTERS": this.ParseFilters(rule, child); break; case "LOGGER": this.ParseLoggerElement(child, rule.ChildRules); break; } } rulesCollection.Add(rule); }
/// <summary> /// Parse {NLog} xml element. /// </summary> /// <param name="nlogElement"></param> /// <param name="filePath">path to config file.</param> /// <param name="autoReloadDefault">The default value for the autoReload option.</param> private void ParseNLogElement(NLogXmlElement nlogElement, string filePath, bool autoReloadDefault) { InternalLogger.Trace("ParseNLogElement"); nlogElement.AssertName("nlog"); if (nlogElement.GetOptionalBooleanAttribute("useInvariantCulture", false)) { this.DefaultCultureInfo = CultureInfo.InvariantCulture; } //check loglevel as first, as other properties could write (indirect) to the internal log. InternalLogger.LogLevel = LogLevel.FromString(nlogElement.GetOptionalAttribute("internalLogLevel", InternalLogger.LogLevel.Name)); #pragma warning disable 618 this.ExceptionLoggingOldStyle = nlogElement.GetOptionalBooleanAttribute("exceptionLoggingOldStyle", false); #pragma warning restore 618 bool autoReload = nlogElement.GetOptionalBooleanAttribute("autoReload", autoReloadDefault); if (filePath != null) { this.fileMustAutoReloadLookup[GetFileLookupKey(filePath)] = autoReload; } logFactory.ThrowExceptions = nlogElement.GetOptionalBooleanAttribute("throwExceptions", logFactory.ThrowExceptions); logFactory.ThrowConfigExceptions = nlogElement.GetOptionalBooleanAttribute("throwConfigExceptions", logFactory.ThrowConfigExceptions); InternalLogger.LogToConsole = nlogElement.GetOptionalBooleanAttribute("internalLogToConsole", InternalLogger.LogToConsole); InternalLogger.LogToConsoleError = nlogElement.GetOptionalBooleanAttribute("internalLogToConsoleError", InternalLogger.LogToConsoleError); InternalLogger.LogFile = nlogElement.GetOptionalAttribute("internalLogFile", InternalLogger.LogFile); #if !SILVERLIGHT && !__IOS__ && !__ANDROID__ InternalLogger.LogToTrace = nlogElement.GetOptionalBooleanAttribute("internalLogToTrace", InternalLogger.LogToTrace); #endif InternalLogger.IncludeTimestamp = nlogElement.GetOptionalBooleanAttribute("internalLogIncludeTimestamp", InternalLogger.IncludeTimestamp); logFactory.GlobalThreshold = LogLevel.FromString(nlogElement.GetOptionalAttribute("globalThreshold", logFactory.GlobalThreshold.Name)); var children = nlogElement.Children.ToList(); //first load the extensions, as the can be used in other elements (targets etc) var extensionsChilds = children.Where(child => child.LocalName.Equals("EXTENSIONS", StringComparison.InvariantCultureIgnoreCase)).ToList(); foreach (var extensionsChild in extensionsChilds) { this.ParseExtensionsElement(extensionsChild, Path.GetDirectoryName(filePath)); } //parse all other direct elements foreach (var child in children) { switch (child.LocalName.ToUpper(CultureInfo.InvariantCulture)) { case "EXTENSIONS": //already parsed break; case "INCLUDE": this.ParseIncludeElement(child, Path.GetDirectoryName(filePath), autoReloadDefault: autoReload); break; case "APPENDERS": case "TARGETS": this.ParseTargetsElement(child); break; case "VARIABLE": this.ParseVariableElement(child); break; case "RULES": this.ParseRulesElement(child, this.LoggingRules); break; case "TIME": this.ParseTimeElement(child); break; case "POOLING": this.ParsePoolingElement(child); break; default: InternalLogger.Warn("Skipping unknown node: {0}", child.LocalName); break; } } }
/// <summary> /// Parse {Logger} xml element /// </summary> /// <param name="loggerElement"></param> /// <param name="rulesCollection">Rules are added to this parameter.</param> private void ParseLoggerElement(NLogXmlElement loggerElement, IList<LoggingRule> rulesCollection) { loggerElement.AssertName("logger"); var namePattern = loggerElement.GetOptionalAttribute("name", "*"); var enabled = loggerElement.GetOptionalBooleanAttribute("enabled", true); if (!enabled) { InternalLogger.Debug("The logger named '{0}' are disabled"); return; } var rule = new LoggingRule(); string appendTo = loggerElement.GetOptionalAttribute("appendTo", null); if (appendTo == null) { appendTo = loggerElement.GetOptionalAttribute("writeTo", null); } rule.LoggerNamePattern = namePattern; if (appendTo != null) { foreach (string t in appendTo.Split(',')) { string targetName = t.Trim(); Target target = FindTargetByName(targetName); if (target != null) { rule.Targets.Add(target); } else { throw new NLogConfigurationException("Target " + targetName + " not found."); } } } rule.Final = loggerElement.GetOptionalBooleanAttribute("final", false); string levelString; if (loggerElement.AttributeValues.TryGetValue("level", out levelString)) { LogLevel level = LogLevel.FromString(levelString); rule.EnableLoggingForLevel(level); } else if (loggerElement.AttributeValues.TryGetValue("levels", out levelString)) { levelString = CleanSpaces(levelString); string[] tokens = levelString.Split(','); foreach (string s in tokens) { if (!string.IsNullOrEmpty(s)) { LogLevel level = LogLevel.FromString(s); rule.EnableLoggingForLevel(level); } } } else { int minLevel = 0; int maxLevel = LogLevel.MaxLevel.Ordinal; string minLevelString; string maxLevelString; if (loggerElement.AttributeValues.TryGetValue("minLevel", out minLevelString)) { minLevel = LogLevel.FromString(minLevelString).Ordinal; } if (loggerElement.AttributeValues.TryGetValue("maxLevel", out maxLevelString)) { maxLevel = LogLevel.FromString(maxLevelString).Ordinal; } for (int i = minLevel; i <= maxLevel; ++i) { rule.EnableLoggingForLevel(LogLevel.FromOrdinal(i)); } } foreach (var child in loggerElement.Children) { switch (child.LocalName.ToUpper(CultureInfo.InvariantCulture)) { case "FILTERS": this.ParseFilters(rule, child); break; case "LOGGER": this.ParseLoggerElement(child, rule.ChildRules); break; } } rulesCollection.Add(rule); }
/// <summary> /// Parse {NLog} xml element. /// </summary> /// <param name="nlogElement"></param> /// <param name="filePath">path to config file.</param> /// <param name="autoReloadDefault">The default value for the autoReload option.</param> private void ParseNLogElement(NLogXmlElement nlogElement, string filePath, bool autoReloadDefault) { InternalLogger.Trace("ParseNLogElement"); nlogElement.AssertName("nlog"); if (nlogElement.GetOptionalBooleanAttribute("useInvariantCulture", false)) { this.DefaultCultureInfo = CultureInfo.InvariantCulture; } #pragma warning disable 618 this.ExceptionLoggingOldStyle = nlogElement.GetOptionalBooleanAttribute("exceptionLoggingOldStyle", false); #pragma warning restore 618 bool autoReload = nlogElement.GetOptionalBooleanAttribute("autoReload", autoReloadDefault); if (filePath != null) this.fileMustAutoReloadLookup[GetFileLookupKey(filePath)] = autoReload; LogManager.ThrowExceptions = nlogElement.GetOptionalBooleanAttribute("throwExceptions", LogManager.ThrowExceptions); InternalLogger.LogToConsole = nlogElement.GetOptionalBooleanAttribute("internalLogToConsole", InternalLogger.LogToConsole); InternalLogger.LogToConsoleError = nlogElement.GetOptionalBooleanAttribute("internalLogToConsoleError", InternalLogger.LogToConsoleError); InternalLogger.LogFile = nlogElement.GetOptionalAttribute("internalLogFile", InternalLogger.LogFile); InternalLogger.LogLevel = LogLevel.FromString(nlogElement.GetOptionalAttribute("internalLogLevel", InternalLogger.LogLevel.Name)); LogManager.GlobalThreshold = LogLevel.FromString(nlogElement.GetOptionalAttribute("globalThreshold", LogManager.GlobalThreshold.Name)); var children = nlogElement.Children; //first load the extensions, as the can be used in other elements (targets etc) var extensionsChilds = children.Where(child => child.LocalName.Equals("EXTENSIONS", StringComparison.InvariantCultureIgnoreCase)); foreach (var extensionsChild in extensionsChilds) { this.ParseExtensionsElement(extensionsChild, Path.GetDirectoryName(filePath)); } //parse all other direct elements foreach (var child in children) { switch (child.LocalName.ToUpper(CultureInfo.InvariantCulture)) { case "EXTENSIONS": //already parsed break; case "INCLUDE": this.ParseIncludeElement(child, Path.GetDirectoryName(filePath), autoReloadDefault: autoReload); break; case "APPENDERS": case "TARGETS": this.ParseTargetsElement(child); break; case "VARIABLE": this.ParseVariableElement(child); break; case "RULES": this.ParseRulesElement(child, this.LoggingRules); break; case "TIME": this.ParseTimeElement(child); break; default: InternalLogger.Warn("Skipping unknown node: {0}", child.LocalName); break; } } }
private bool SetLayoutFromElement(object o, NLogXmlElement layoutElement) { PropertyInfo targetPropertyInfo; string name = layoutElement.LocalName; // if property exists if (PropertyHelper.TryGetPropertyInfo(o, name, out targetPropertyInfo)) { // and is a Layout if (typeof(Layout).IsAssignableFrom(targetPropertyInfo.PropertyType)) { string layoutTypeName = StripOptionalNamespacePrefix(layoutElement.GetOptionalAttribute("type", null)); // and 'type' attribute has been specified if (layoutTypeName != null) { // configure it from current element Layout layout = this.ConfigurationItemFactory.Layouts.CreateInstance(this.ExpandSimpleVariables(layoutTypeName)); this.ConfigureObjectFromAttributes(layout, layoutElement, true); this.ConfigureObjectFromElement(layout, layoutElement); targetPropertyInfo.SetValue(o, layout, null); return true; } } } return false; }
private void ParseNLogElement(NLogXmlElement nlogElement, string baseDirectory) { InternalLogger.Trace("ParseNLogElement"); nlogElement.AssertName("nlog"); this.AutoReload = nlogElement.GetOptionalBooleanAttribute("autoReload", false); LogManager.ThrowExceptions = nlogElement.GetOptionalBooleanAttribute("throwExceptions", LogManager.ThrowExceptions); InternalLogger.LogToConsole = nlogElement.GetOptionalBooleanAttribute("internalLogToConsole", InternalLogger.LogToConsole); #if !NET_CF InternalLogger.LogToConsoleError = nlogElement.GetOptionalBooleanAttribute("internalLogToConsoleError", InternalLogger.LogToConsoleError); #endif InternalLogger.LogFile = nlogElement.GetOptionalAttribute("internalLogFile", InternalLogger.LogFile); InternalLogger.LogLevel = LogLevel.FromString(nlogElement.GetOptionalAttribute("internalLogLevel", InternalLogger.LogLevel.Name)); LogManager.GlobalThreshold = LogLevel.FromString(nlogElement.GetOptionalAttribute("globalThreshold", LogManager.GlobalThreshold.Name)); foreach (var el in nlogElement.Children) { switch (el.LocalName.ToUpper(CultureInfo.InvariantCulture)) { case "EXTENSIONS": this.ParseExtensionsElement(el, baseDirectory); break; case "INCLUDE": this.ParseIncludeElement(el, baseDirectory); break; case "APPENDERS": case "TARGETS": this.ParseTargetsElement(el); break; case "VARIABLE": this.ParseVariableElement(el); break; case "RULES": this.ParseRulesElement(el, this.LoggingRules); break; default: InternalLogger.Warn("Skipping unknown node: {0}", el.LocalName); break; } } }
private Layout TryCreateLayoutInstance(NLogXmlElement element, Type type) { // Check if it is a Layout if (!typeof(Layout).IsAssignableFrom(type)) return null; string layoutTypeName = StripOptionalNamespacePrefix(element.GetOptionalAttribute("type", null)); // Check if the 'type' attribute has been specified if (layoutTypeName == null) return null; return this.ConfigurationItemFactory.Layouts.CreateInstance(this.ExpandSimpleVariables(layoutTypeName)); }
/// <summary> /// Parse {NLog} xml element. /// </summary> /// <param name="nlogElement"></param> /// <param name="baseDirectory">path to directory of config file.</param> private void ParseNLogElement(NLogXmlElement nlogElement, string baseDirectory) { InternalLogger.Trace("ParseNLogElement"); nlogElement.AssertName("nlog"); if (nlogElement.GetOptionalBooleanAttribute("useInvariantCulture", false)) { this.DefaultCultureInfo = CultureInfo.InvariantCulture; } #pragma warning disable 618 this.ExceptionLoggingOldStyle = nlogElement.GetOptionalBooleanAttribute("exceptionLoggingOldStyle", false); #pragma warning restore 618 this.AutoReload = nlogElement.GetOptionalBooleanAttribute("autoReload", false); LogManager.ThrowExceptions = nlogElement.GetOptionalBooleanAttribute("throwExceptions", LogManager.ThrowExceptions); InternalLogger.LogToConsole = nlogElement.GetOptionalBooleanAttribute("internalLogToConsole", InternalLogger.LogToConsole); InternalLogger.LogToConsoleError = nlogElement.GetOptionalBooleanAttribute("internalLogToConsoleError", InternalLogger.LogToConsoleError); InternalLogger.LogFile = nlogElement.GetOptionalAttribute("internalLogFile", InternalLogger.LogFile); InternalLogger.LogLevel = LogLevel.FromString(nlogElement.GetOptionalAttribute("internalLogLevel", InternalLogger.LogLevel.Name)); LogManager.GlobalThreshold = LogLevel.FromString(nlogElement.GetOptionalAttribute("globalThreshold", LogManager.GlobalThreshold.Name)); var children = nlogElement.Children; //first load the extensions, as the can be used in other elements (targets etc) var extensionsChilds = children.Where(child => child.LocalName.Equals("EXTENSIONS", StringComparison.InvariantCultureIgnoreCase)); foreach (var extensionsChild in extensionsChilds) { this.ParseExtensionsElement(extensionsChild, baseDirectory); } //parse all other direct elements foreach (var child in children) { switch (child.LocalName.ToUpper(CultureInfo.InvariantCulture)) { case "EXTENSIONS": //already parsed break; case "INCLUDE": this.ParseIncludeElement(child, baseDirectory); break; case "APPENDERS": case "TARGETS": this.ParseTargetsElement(child); break; case "VARIABLE": this.ParseVariableElement(child); break; case "RULES": this.ParseRulesElement(child, this.LoggingRules); break; case "TIME": this.ParseTimeElement(child); break; default: InternalLogger.Warn("Skipping unknown node: {0}", child.LocalName); break; } } }