Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Commit

Permalink
implemented <default-wrapper /> that wraps all targets in <targets />…
Browse files Browse the repository at this point in the history
… section with the specified wrapper.
  • Loading branch information
jkowalski committed Jun 25, 2006
1 parent 7e1acf2 commit 2004683
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 9 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
@@ -1,3 +1,7 @@
2006-06-25 Jaroslaw Kowalski <jaak@jkowalski.net>
* implemented <targets><default-wrapper /> </targets> that wraps all
targets in <targets> section with the specified wrapper.

2006-06-23 Jaroslaw Kowalski <jaak@jkowalski.net>
* fixed some race conditions in the Network target
* added NetworkTarget.Close()
Expand Down
40 changes: 37 additions & 3 deletions src/NLog/Config/XmlLoggingConfiguration.cs
Expand Up @@ -44,6 +44,7 @@
using NLog.Filters;
using NLog.LayoutRenderers;
using NLog.Internal;
using NLog.Targets.Wrappers;

namespace NLog.Config
{
Expand Down Expand Up @@ -534,17 +535,27 @@ private void ConfigureTargetsFromElement(XmlElement element)
return ;

bool asyncWrap = 0 == String.Compare(GetCaseInsensitiveAttribute(element, "async"), "true", true);
XmlElement defaultWrapperElement = null;

foreach (XmlNode n in element.ChildNodes)
{
XmlElement targetElement = n as XmlElement;

if (targetElement == null)
continue;

if (0 == String.Compare(targetElement.LocalName, "default-wrapper"))
{
defaultWrapperElement = targetElement;
continue;
}

if (0 == String.Compare(targetElement.LocalName, "target", true) ||
0 == String.Compare(targetElement.LocalName, "appender", true) ||
0 == String.Compare(targetElement.LocalName, "wrapper", true))
0 == String.Compare(targetElement.LocalName, "wrapper", true) ||
0 == String.Compare(targetElement.LocalName, "wrapper-target", true) ||
0 == String.Compare(targetElement.LocalName, "compound-target", true)
)
{
string type = GetCaseInsensitiveAttribute(targetElement, "type");
Target newTarget = TargetFactory.CreateTarget(type);
Expand All @@ -563,6 +574,29 @@ private void ConfigureTargetsFromElement(XmlElement element)
newTarget = atw;
}
#endif
if (defaultWrapperElement != null)
{
string wrapperType = GetCaseInsensitiveAttribute(defaultWrapperElement, "type");
Target wrapperTargetInstance = TargetFactory.CreateTarget(wrapperType);
WrapperTargetBase wtb = wrapperTargetInstance as WrapperTargetBase;
if (wtb == null)
throw new Exception("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 Exception("Child target type specified on <default-wrapper /> is not a wrapper.");
}
wtb.WrappedTarget = newTarget;
wrapperTargetInstance.Name = newTarget.Name;
newTarget.Name = newTarget.Name + "_wrapped";

InternalLogger.Debug("Wrapping target '{0}' with '{1}' and renaming to '{2}", wrapperTargetInstance.Name, wrapperTargetInstance.GetType().Name, newTarget.Name);
newTarget = wrapperTargetInstance;
}

InternalLogger.Info("Adding target {0}", newTarget);
AddTarget(newTarget.Name, newTarget);
}
}
Expand Down Expand Up @@ -619,7 +653,7 @@ private void ConfigureTargetFromXmlElement(Target target, XmlElement element)
XmlElement el = (XmlElement)node;
string name = el.LocalName;

if ((name == "target" || name == "wrapper") && compound != null)
if ((name == "target" || name == "wrapper" || name == "wrapper-target" || name == "compound-target") && compound != null)
{
string type = GetCaseInsensitiveAttribute(el, "type");
Target newTarget = TargetFactory.CreateTarget(type);
Expand All @@ -636,7 +670,7 @@ private void ConfigureTargetFromXmlElement(Target target, XmlElement element)
continue;
}

if ((name == "target" || name == "wrapper") && wrapper != null)
if ((name == "target" || name == "wrapper" || name == "wrapper-target" || name == "compound-target") && wrapper != null)
{
string type = GetCaseInsensitiveAttribute(el, "type");
Target newTarget = TargetFactory.CreateTarget(type);
Expand Down
5 changes: 1 addition & 4 deletions src/NLog/Target.cs
Expand Up @@ -196,10 +196,7 @@ protected internal virtual int NeedsStackTrace()
/// <returns>A string that describes the target.</returns>
public override string ToString()
{
if (Name != null)
return String.Format("{0}: {1}", Name, this.GetType().FullName);
else
return this.GetType().FullName;
return ((this.Name != null) ? this.Name : "unnamed") + ":" + this.GetType().Name;
}

/// <summary>
Expand Down
8 changes: 8 additions & 0 deletions src/NLog/Targets/Wrappers/WrapperTargetBase.cs
Expand Up @@ -97,5 +97,13 @@ public override void Initialize()
WrappedTarget.Initialize();
}

/// <summary>
/// Returns the text representation of the object. Used for diagnostics.
/// </summary>
/// <returns>A string that describes the target.</returns>
public override string ToString()
{
return ((this.Name != null) ? this.Name : "unnamed") + ":" + this.GetType().Name + "(" + ((WrappedTarget != null) ? WrappedTarget.ToString() : "null") + ")";
}
}
}
8 changes: 6 additions & 2 deletions tests/NLog.Test/App.config
Expand Up @@ -13,8 +13,12 @@
</extensions>

<targets>
<target name="gg" xsi:type="GaduGadu" uin="9957385" password="123456" recipientUin="4008306" />
<target name="msn" xsi:type="MSNMessenger"/>
<default-wrapper xsi:type="BufferingWrapper">
<wrapper-target xsi:type="RetryingWrapper">
<wrapper-target xsi:type="RepeatingWrapper" repeatCount="3" />
</wrapper-target>
</default-wrapper>

<target name="n" xsi:type="Network"
address="tcp://localhost:4001"
layout="${log4jxmlevent}"/>
Expand Down
28 changes: 28 additions & 0 deletions tools/MakeNLogXSD/Program.cs
Expand Up @@ -183,6 +183,20 @@ static void DumpType(XmlTextWriter xtw, Type t)
xtw.WriteAttributeString("minOccurs", "1");
xtw.WriteAttributeString("maxOccurs", "1");
xtw.WriteEndElement();

xtw.WriteStartElement("xs:element");
xtw.WriteAttributeString("name", "wrapper-target");
xtw.WriteAttributeString("type", "WrapperTargetBase");
xtw.WriteAttributeString("minOccurs", "1");
xtw.WriteAttributeString("maxOccurs", "1");
xtw.WriteEndElement();

xtw.WriteStartElement("xs:element");
xtw.WriteAttributeString("name", "compound-target");
xtw.WriteAttributeString("type", "CompoundTargetBase");
xtw.WriteAttributeString("minOccurs", "1");
xtw.WriteAttributeString("maxOccurs", "1");
xtw.WriteEndElement();
}

if (t == typeof(CompoundTargetBase))
Expand All @@ -193,6 +207,20 @@ static void DumpType(XmlTextWriter xtw, Type t)
xtw.WriteAttributeString("minOccurs", "1");
xtw.WriteAttributeString("maxOccurs", "unbounded");
xtw.WriteEndElement();

xtw.WriteStartElement("xs:element");
xtw.WriteAttributeString("name", "wrapper-target");
xtw.WriteAttributeString("type", "WrapperTargetBase");
xtw.WriteAttributeString("minOccurs", "1");
xtw.WriteAttributeString("maxOccurs", "1");
xtw.WriteEndElement();

xtw.WriteStartElement("xs:element");
xtw.WriteAttributeString("name", "compound-target");
xtw.WriteAttributeString("type", "CompoundTargetBase");
xtw.WriteAttributeString("minOccurs", "1");
xtw.WriteAttributeString("maxOccurs", "1");
xtw.WriteEndElement();
}

xtw.WriteEndElement();
Expand Down
3 changes: 3 additions & 0 deletions tools/MakeNLogXSD/TemplateNLog.xsd
Expand Up @@ -56,7 +56,10 @@

<xs:complexType name="NLogTargets">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="default-wrapper" type="WrapperTargetBase" />
<xs:element name="target" type="Target" />
<xs:element name="wrapper-target" type="WrapperTargetBase" />
<xs:element name="compound-target" type="CompoundTargetBase" />
</xs:choice>
<xs:attribute name="async" type="xs:boolean">
<xs:annotation>
Expand Down

0 comments on commit 2004683

Please sign in to comment.