Skip to content

Commit e3efd12

Browse files
committed
AssemblyExtensionTypes - Reduce code complexity
1 parent d88f1d3 commit e3efd12

File tree

7 files changed

+352
-459
lines changed

7 files changed

+352
-459
lines changed

src/NLog/Config/AssemblyExtensionTypes.cs

Lines changed: 179 additions & 318 deletions
Large diffs are not rendered by default.

src/NLog/Config/AssemblyExtensionTypes.tt

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ namespace NLog.Config
5656
};
5757
Type[] AllTypes = typeof(NLog.LogFactory).Assembly.GetTypes().Where(t => !t.IsAbstract && !t.IsPrimitive && t.IsClass && !t.IsNested && !NetFrameworkOnly.Contains(t.ToString())).OrderBy(t => t.ToString()).ToArray();
5858
#>
59-
public static void RegisterTargetTypes(ConfigurationItemFactory factory, bool skipCheckExists)
59+
public static void RegisterTargetTypes(ConfigurationItemFactory factory, bool checkTypeExists)
6060
{
61+
var targetFactory = factory.GetTargetFactory();
62+
6163
factory.RegisterTypeProperties<NLog.Targets.TargetWithContext.TargetWithContextLayout>(() => null);
6264
#if NETFRAMEWORK
63-
if (skipCheckExists || !factory.GetTargetFactory().CheckTypeAliasExists("EventLog"))
64-
factory.GetTargetFactory().RegisterType<NLog.Targets.EventLogTarget>("EventLog");
65+
targetFactory.RegisterType<NLog.Targets.EventLogTarget>("EventLog", checkTypeExists);
6566
#endif
6667
<#
6768
foreach(var type in AllTypes)
@@ -71,10 +72,9 @@ namespace NLog.Config
7172
var targetAttributes = type.GetCustomAttributes<NLog.Targets.TargetAttribute>(false);
7273
foreach (var targetAlias in targetAttributes)
7374
{
74-
var targetAliasName = targetAlias.Name;
75+
var targetAliasName = targetAlias.Name.Replace("-", "");
7576
#>
76-
if (skipCheckExists || !factory.GetTargetFactory().CheckTypeAliasExists("<#= targetAliasName #>"))
77-
factory.GetTargetFactory().RegisterType<<#= type #>>("<#= targetAliasName #>");
77+
targetFactory.RegisterType<<#= type #>>("<#= targetAliasName #>", checkTypeExists);
7878
<#
7979
}
8080
}
@@ -92,8 +92,10 @@ namespace NLog.Config
9292
#>
9393
}
9494

95-
public static void RegisterLayoutTypes(ConfigurationItemFactory factory, bool skipCheckExists)
95+
public static void RegisterLayoutTypes(ConfigurationItemFactory factory, bool checkTypeExists)
9696
{
97+
var layoutFactory = factory.GetLayoutFactory();
98+
9799
factory.RegisterTypeProperties<NLog.Layouts.CsvLayout.CsvHeaderLayout>(() => null);
98100
<#
99101
foreach(var type in AllTypes)
@@ -103,10 +105,9 @@ namespace NLog.Config
103105
var layoutAttributes = type.GetCustomAttributes<NLog.Layouts.LayoutAttribute>(false);
104106
foreach (var layoutAlias in layoutAttributes)
105107
{
106-
var layoutAliasName = layoutAlias.Name;
108+
var layoutAliasName = layoutAlias.Name.Replace("-", "");
107109
#>
108-
if (skipCheckExists || !factory.GetLayoutFactory().CheckTypeAliasExists("<#= layoutAliasName #>"))
109-
factory.GetLayoutFactory().RegisterType<<#= type #>>("<#= layoutAliasName #>");
110+
layoutFactory.RegisterType<<#= type #>>("<#= layoutAliasName #>", checkTypeExists);
110111
<#
111112
}
112113
}
@@ -124,10 +125,13 @@ namespace NLog.Config
124125
#>
125126
}
126127

127-
public static void RegisterLayoutRendererTypes(ConfigurationItemFactory factory, bool skipCheckExists)
128+
public static void RegisterLayoutRendererTypes(ConfigurationItemFactory factory, bool checkTypeExists)
128129
{
130+
var layoutRendererFactory = factory.GetLayoutRendererFactory();
131+
var ambientPropertyFactory = factory.GetAmbientPropertyFactory();
132+
129133
#if NETFRAMEWORK
130-
factory.GetLayoutRendererFactory().RegisterType<NLog.LayoutRenderers.AppSettingLayoutRenderer>("appsetting");
134+
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.AppSettingLayoutRenderer>("appsetting", checkTypeExists);
131135
#endif
132136
factory.RegisterTypeProperties<NLog.LayoutRenderers.LiteralWithRawValueLayoutRenderer>(() => null);
133137
factory.RegisterTypeProperties<NLog.LayoutRenderers.FuncLayoutRenderer>(() => null);
@@ -140,20 +144,18 @@ namespace NLog.Config
140144
var layoutAttributes = type.GetCustomAttributes<NLog.LayoutRenderers.LayoutRendererAttribute>(false);
141145
foreach (var layoutAlias in layoutAttributes)
142146
{
143-
var layoutAliasName = layoutAlias.Name;
147+
var layoutAliasName = layoutAlias.Name.Replace("-", "");
144148
#>
145-
if (skipCheckExists || !factory.GetLayoutRendererFactory().CheckTypeAliasExists("<#= layoutAliasName #>"))
146-
factory.GetLayoutRendererFactory().RegisterType<<#= type #>>("<#= layoutAliasName #>");
149+
layoutRendererFactory.RegisterType<<#= type #>>("<#= layoutAliasName #>", checkTypeExists);
147150
<#
148151
}
149152

150153
var ambientAttributes = type.GetCustomAttributes<NLog.LayoutRenderers.AmbientPropertyAttribute>(false);
151154
foreach (var layoutAlias in ambientAttributes)
152155
{
153-
var layoutAliasName = layoutAlias.Name;
156+
var ambientPropertyName = layoutAlias.Name.Replace("-", "");
154157
#>
155-
if (skipCheckExists || !factory.GetAmbientPropertyFactory().CheckTypeAliasExists("<#= layoutAliasName #>"))
156-
factory.GetAmbientPropertyFactory().RegisterType<<#= type #>>("<#= layoutAliasName #>");
158+
ambientPropertyFactory.RegisterType<<#= type #>>("<#= ambientPropertyName #>", checkTypeExists);
157159
<#
158160
}
159161
}
@@ -171,8 +173,9 @@ namespace NLog.Config
171173
#>
172174
}
173175

174-
public static void RegisterFilterTypes(ConfigurationItemFactory factory, bool skipCheckExists)
176+
public static void RegisterFilterTypes(ConfigurationItemFactory factory, bool checkTypeExists)
175177
{
178+
var filterFactory = factory.GetFilterFactory();
176179
<#
177180
foreach(var type in AllTypes)
178181
{
@@ -181,10 +184,9 @@ namespace NLog.Config
181184
var filterAttributes = type.GetCustomAttributes<NLog.Filters.FilterAttribute>(false);
182185
foreach (var filterAlias in filterAttributes)
183186
{
184-
var filterAliasName = filterAlias.Name;
187+
var filterAliasName = filterAlias.Name.Replace("-", "");
185188
#>
186-
if (skipCheckExists || !factory.GetFilterFactory().CheckTypeAliasExists("<#= filterAliasName #>"))
187-
factory.GetFilterFactory().RegisterType<<#= type #>>("<#= filterAliasName #>");
189+
filterFactory.RegisterType<<#= type #>>("<#= filterAliasName #>", checkTypeExists);
188190
<#
189191
}
190192
}
@@ -202,8 +204,9 @@ namespace NLog.Config
202204
#>
203205
}
204206

205-
public static void RegisterTimeSourceTypes(ConfigurationItemFactory factory, bool skipCheckExists)
207+
public static void RegisterTimeSourceTypes(ConfigurationItemFactory factory, bool checkTypeExists)
206208
{
209+
var timeSourceFactory = factory.GetTimeSourceFactory();
207210
<#
208211
foreach(var type in AllTypes)
209212
{
@@ -212,10 +215,9 @@ namespace NLog.Config
212215
var timeSourceAttribute = type.GetCustomAttributes<NLog.Time.TimeSourceAttribute>(false);
213216
foreach (var timeAlias in timeSourceAttribute)
214217
{
215-
var timeAliasName = timeAlias.Name;
218+
var timeAliasName = timeAlias.Name.Replace("-", "");
216219
#>
217-
if (skipCheckExists || !factory.GetTimeSourceFactory().CheckTypeAliasExists("<#= timeAliasName #>"))
218-
factory.GetTimeSourceFactory().RegisterType<<#= type #>>("<#= timeAliasName #>");
220+
timeSourceFactory.RegisterType<<#= type #>>("<#= timeAliasName #>", checkTypeExists);
219221
<#
220222
}
221223
}
@@ -233,8 +235,9 @@ namespace NLog.Config
233235
#>
234236
}
235237

236-
public static void RegisterConditionTypes(ConfigurationItemFactory factory, bool skipCheckExists)
238+
public static void RegisterConditionTypes(ConfigurationItemFactory factory, bool checkTypeExists)
237239
{
240+
var conditionMethodFactory = factory.GetConditionMethodFactory();
238241
<#
239242
foreach(var type in AllTypes)
240243
{
@@ -246,30 +249,29 @@ namespace NLog.Config
246249
}
247250
}
248251
#>
249-
250-
if (skipCheckExists || !factory.GetConditionMethodFactory().CheckTypeAliasExists("length"))
251-
factory.GetConditionMethodFactory().RegisterOneParameter("length", (logEvent, arg1) => NLog.Conditions.ConditionMethods.Length(arg1?.ToString()));
252-
if (skipCheckExists || !factory.GetConditionMethodFactory().CheckTypeAliasExists("equals"))
253-
factory.GetConditionMethodFactory().RegisterTwoParameters("equals", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.Equals2(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
254-
if (skipCheckExists || !factory.GetConditionMethodFactory().CheckTypeAliasExists("strequals"))
252+
if (!checkTypeExists || !conditionMethodFactory.CheckTypeAliasExists("length"))
253+
conditionMethodFactory.RegisterOneParameter("length", (logEvent, arg1) => NLog.Conditions.ConditionMethods.Length(arg1?.ToString()));
254+
if (!checkTypeExists || !conditionMethodFactory.CheckTypeAliasExists("equals"))
255+
conditionMethodFactory.RegisterTwoParameters("equals", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.Equals2(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
256+
if (!checkTypeExists || !conditionMethodFactory.CheckTypeAliasExists("strequals"))
255257
{
256-
factory.GetConditionMethodFactory().RegisterTwoParameters("strequals", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.Equals2(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
257-
factory.GetConditionMethodFactory().RegisterThreeParameters("strequals", (logEvent, arg1, arg2, arg3) => NLog.Conditions.ConditionMethods.Equals2(arg1?.ToString(), arg2?.ToString(), arg3 is bool ignoreCase ? ignoreCase : true) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
258+
conditionMethodFactory.RegisterTwoParameters("strequals", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.Equals2(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
259+
conditionMethodFactory.RegisterThreeParameters("strequals", (logEvent, arg1, arg2, arg3) => NLog.Conditions.ConditionMethods.Equals2(arg1?.ToString(), arg2?.ToString(), arg3 is bool ignoreCase ? ignoreCase : true) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
258260
}
259-
if (skipCheckExists || !factory.GetConditionMethodFactory().CheckTypeAliasExists("contains"))
261+
if (!checkTypeExists || !conditionMethodFactory.CheckTypeAliasExists("contains"))
260262
{
261-
factory.GetConditionMethodFactory().RegisterTwoParameters("contains", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.Contains(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
262-
factory.GetConditionMethodFactory().RegisterThreeParameters("contains", (logEvent, arg1, arg2, arg3) => NLog.Conditions.ConditionMethods.Contains(arg1?.ToString(), arg2?.ToString(), arg3 is bool ignoreCase ? ignoreCase : true) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
263+
conditionMethodFactory.RegisterTwoParameters("contains", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.Contains(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
264+
conditionMethodFactory.RegisterThreeParameters("contains", (logEvent, arg1, arg2, arg3) => NLog.Conditions.ConditionMethods.Contains(arg1?.ToString(), arg2?.ToString(), arg3 is bool ignoreCase ? ignoreCase : true) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
263265
}
264-
if (skipCheckExists || !factory.GetConditionMethodFactory().CheckTypeAliasExists("starts-with"))
266+
if (!checkTypeExists || !conditionMethodFactory.CheckTypeAliasExists("starts-with"))
265267
{
266-
factory.GetConditionMethodFactory().RegisterTwoParameters("starts-with", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.StartsWith(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
267-
factory.GetConditionMethodFactory().RegisterThreeParameters("starts-with", (logEvent, arg1, arg2, arg3) => NLog.Conditions.ConditionMethods.StartsWith(arg1?.ToString(), arg2?.ToString(), arg3 is bool ignoreCase ? ignoreCase : true) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
268+
conditionMethodFactory.RegisterTwoParameters("starts-with", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.StartsWith(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
269+
conditionMethodFactory.RegisterThreeParameters("starts-with", (logEvent, arg1, arg2, arg3) => NLog.Conditions.ConditionMethods.StartsWith(arg1?.ToString(), arg2?.ToString(), arg3 is bool ignoreCase ? ignoreCase : true) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
268270
}
269-
if (skipCheckExists || !factory.GetConditionMethodFactory().CheckTypeAliasExists("ends-with"))
271+
if (!checkTypeExists || !conditionMethodFactory.CheckTypeAliasExists("ends-with"))
270272
{
271-
factory.GetConditionMethodFactory().RegisterTwoParameters("ends-with", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.EndsWith(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
272-
factory.GetConditionMethodFactory().RegisterThreeParameters("ends-with", (logEvent, arg1, arg2, arg3) => NLog.Conditions.ConditionMethods.EndsWith(arg1?.ToString(), arg2?.ToString(), arg3 is bool ignoreCase ? ignoreCase : true) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
273+
conditionMethodFactory.RegisterTwoParameters("ends-with", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.EndsWith(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
274+
conditionMethodFactory.RegisterThreeParameters("ends-with", (logEvent, arg1, arg2, arg3) => NLog.Conditions.ConditionMethods.EndsWith(arg1?.ToString(), arg2?.ToString(), arg3 is bool ignoreCase ? ignoreCase : true) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
273275
}
274276
}
275277
#pragma warning restore CS0618 // Type or member is obsolete

0 commit comments

Comments
 (0)