private Target WrapWithDefaultWrapper(Target t, XmlElement defaultWrapperElement) { string wrapperType = GetCaseInsensitiveAttribute(defaultWrapperElement, "type"); Target wrapperTargetInstance = TargetFactory.CreateTarget(wrapperType); WrapperTargetBase wtb = wrapperTargetInstance as WrapperTargetBase; if (wtb == null) { throw new NLogConfigurationException("Target type specified on <default-wrapper /> is not a wrapper."); } ConfigureTargetFromXmlElement(wrapperTargetInstance, defaultWrapperElement); while (wtb.WrappedTarget != null) { wtb = wtb.WrappedTarget as WrapperTargetBase; if (wtb == null) { throw new NLogConfigurationException("Child target type specified on <default-wrapper /> is not a wrapper."); } } wtb.WrappedTarget = t; wrapperTargetInstance.Name = t.Name; t.Name = t.Name + "_wrapped"; InternalLogger.Debug("Wrapping target '{0}' with '{1}' and renaming to '{2}", wrapperTargetInstance.Name, wrapperTargetInstance.GetType().Name, t.Name); return(wrapperTargetInstance); }
private void ConfigureTargetFromXmlElement(Target target, XmlElement element) { NLog.Targets.Compound.CompoundTargetBase compound = target as NLog.Targets.Compound.CompoundTargetBase; NLog.Targets.Wrappers.WrapperTargetBase wrapper = target as NLog.Targets.Wrappers.WrapperTargetBase; PropertyHelper.ConfigureObjectFromAttributes(target, element.Attributes, _variables, true); foreach (XmlElement el in PropertyHelper.GetChildElements(element)) { string name = el.LocalName; if (compound != null) { if ((name == "target" || name == "wrapper" || name == "wrapper-target" || name == "compound-target")) { string type = GetCaseInsensitiveAttribute(el, "type"); Target newTarget = TargetFactory.CreateTarget(type); if (newTarget != null) { ConfigureTargetFromXmlElement(newTarget, el); if (newTarget.Name != null) { // if the new target has name, register it AddTarget(newTarget.Name, newTarget); } compound.Targets.Add(newTarget); } continue; } } if (wrapper != null) { if ((name == "target" || name == "wrapper" || name == "wrapper-target" || name == "compound-target")) { string type = GetCaseInsensitiveAttribute(el, "type"); Target newTarget = TargetFactory.CreateTarget(type); if (newTarget != null) { ConfigureTargetFromXmlElement(newTarget, el); if (newTarget.Name != null) { // if the new target has name, register it AddTarget(newTarget.Name, newTarget); } if (wrapper.WrappedTarget != null) { throw new NLogConfigurationException("Wrapped target already defined."); } wrapper.WrappedTarget = newTarget; } continue; } } PropertyHelper.SetPropertyFromElement(target, el, _variables); } }
private void ConfigureTargetsFromElement(XmlElement element) { if (element == null) { return; } bool asyncWrap = EqualsCI(GetCaseInsensitiveAttribute(element, "async"), "true"); XmlElement defaultWrapperElement = null; Hashtable typeNameToDefaultTargetParametersElement = new Hashtable(); foreach (XmlElement targetElement in PropertyHelper.GetChildElements(element)) { string name = targetElement.LocalName.ToLower(); string type = GetCaseInsensitiveAttribute(targetElement, "type"); switch (name) { case "default-wrapper": defaultWrapperElement = targetElement; break; case "default-target-parameters": typeNameToDefaultTargetParametersElement[type] = targetElement; break; case "target": case "appender": case "wrapper": case "wrapper-target": case "compound-target": Target newTarget = TargetFactory.CreateTarget(type); XmlElement defaultParametersElement = typeNameToDefaultTargetParametersElement[type] as XmlElement; if (defaultParametersElement != null) { ConfigureTargetFromXmlElement(newTarget, defaultParametersElement); } ConfigureTargetFromXmlElement(newTarget, targetElement); if (asyncWrap) { newTarget = WrapWithAsyncTarget(newTarget); } if (defaultWrapperElement != null) { newTarget = WrapWithDefaultWrapper(newTarget, defaultWrapperElement); } InternalLogger.Info("Adding target {0}", newTarget); AddTarget(newTarget.Name, newTarget); break; } } }