Upstream NLog/NLog state of the touched files before 390d384461

This commit is contained in:
upstream committed 2026-10-02 16:30:08 +02:00
1 parent 05f5fc4dc9
commit 9fc32d87a6
3 files changed
+590

No files matched your search

+278
View File
@@ -0,0 +1,278 @@
//
// Copyright (c) 2004-2024 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
namespace NLog.Config
{
/// <summary>
/// Provides logging interface and utility functions.
/// </summary>
internal static class AssemblyExtensionTypes
{
#pragma warning disable CS0618 // Type or member is obsolete
public static void RegisterTargetTypes(ConfigurationItemFactory factory, bool checkTypeExists)
{
var targetFactory = factory.GetTargetFactory();
factory.RegisterTypeProperties<NLog.Targets.TargetWithContext.TargetWithContextLayout>(() => null);
#if NETFRAMEWORK
targetFactory.RegisterType<NLog.Targets.EventLogTarget>("EventLog", checkTypeExists);
#endif
targetFactory.RegisterType<NLog.Targets.ColoredConsoleTarget>("ColoredConsole", checkTypeExists);
factory.RegisterType<NLog.Targets.ConsoleRowHighlightingRule>();
targetFactory.RegisterType<NLog.Targets.ConsoleTarget>("Console", checkTypeExists);
factory.RegisterType<NLog.Targets.ConsoleWordHighlightingRule>();
targetFactory.RegisterType<NLog.Targets.DebuggerTarget>("Debugger", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.DebugSystemTarget>("DebugSystem", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.DebugTarget>("Debug", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.FileTarget>("File", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.MemoryTarget>("Memory", checkTypeExists);
factory.RegisterType<NLog.Targets.MethodCallParameter>();
targetFactory.RegisterType<NLog.Targets.MethodCallTarget>("MethodCall", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.NullTarget>("Null", checkTypeExists);
factory.RegisterType<NLog.Targets.TargetPropertyWithContext>();
targetFactory.RegisterType<NLog.Targets.Wrappers.AsyncTargetWrapper>("AsyncWrapper", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.Wrappers.AutoFlushTargetWrapper>("AutoFlushWrapper", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.Wrappers.BufferingTargetWrapper>("BufferingWrapper", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.Wrappers.FallbackGroupTarget>("FallbackGroup", checkTypeExists);
factory.RegisterType<NLog.Targets.Wrappers.FilteringRule>();
targetFactory.RegisterType<NLog.Targets.Wrappers.FilteringTargetWrapper>("FilteringWrapper", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.Wrappers.GroupByTargetWrapper>("GroupByWrapper", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.Wrappers.LimitingTargetWrapper>("LimitingWrapper", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.Wrappers.PostFilteringTargetWrapper>("PostFilteringWrapper", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.Wrappers.RandomizeGroupTarget>("RandomizeGroup", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.Wrappers.RepeatingTargetWrapper>("RepeatingWrapper", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.Wrappers.RetryingTargetWrapper>("RetryingWrapper", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.Wrappers.RoundRobinGroupTarget>("RoundRobinGroup", checkTypeExists);
targetFactory.RegisterType<NLog.Targets.Wrappers.SplitGroupTarget>("SplitGroup", checkTypeExists);
}
public static void RegisterLayoutTypes(ConfigurationItemFactory factory, bool checkTypeExists)
{
var layoutFactory = factory.GetLayoutFactory();
factory.RegisterTypeProperties<NLog.Layouts.CsvLayout.CsvHeaderLayout>(() => null);
layoutFactory.RegisterType<NLog.Layouts.CompoundLayout>("CompoundLayout", checkTypeExists);
factory.RegisterType<NLog.Layouts.CsvColumn>();
layoutFactory.RegisterType<NLog.Layouts.CsvLayout>("CsvLayout", checkTypeExists);
layoutFactory.RegisterType<NLog.Layouts.JsonArrayLayout>("JsonArrayLayout", checkTypeExists);
factory.RegisterType<NLog.Layouts.JsonAttribute>();
layoutFactory.RegisterType<NLog.Layouts.JsonLayout>("JsonLayout", checkTypeExists);
layoutFactory.RegisterType<NLog.Layouts.LayoutWithHeaderAndFooter>("LayoutWithHeaderAndFooter", checkTypeExists);
layoutFactory.RegisterType<NLog.Layouts.SimpleLayout>("SimpleLayout", checkTypeExists);
factory.RegisterType<NLog.Layouts.ValueTypeLayoutInfo>();
factory.RegisterType<NLog.Layouts.XmlAttribute>();
layoutFactory.RegisterType<NLog.Layouts.XmlLayout>("XmlLayout", checkTypeExists);
}
public static void RegisterLayoutRendererTypes(ConfigurationItemFactory factory, bool checkTypeExists)
{
var layoutRendererFactory = factory.GetLayoutRendererFactory();
var ambientPropertyFactory = factory.GetAmbientPropertyFactory();
#if NETFRAMEWORK
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.AppSettingLayoutRenderer>("appsetting", checkTypeExists);
#endif
factory.RegisterTypeProperties<NLog.LayoutRenderers.LiteralWithRawValueLayoutRenderer>(() => null);
factory.RegisterTypeProperties<NLog.LayoutRenderers.FuncLayoutRenderer>(() => null);
factory.RegisterTypeProperties<NLog.LayoutRenderers.FuncThreadAgnosticLayoutRenderer>(() => null);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.AllEventPropertiesLayoutRenderer>("alleventproperties", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.AppDomainLayoutRenderer>("appdomain", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.AssemblyVersionLayoutRenderer>("assemblyversion", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.BaseDirLayoutRenderer>("basedir", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.CallSiteFileNameLayoutRenderer>("callsitefilename", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.CallSiteLayoutRenderer>("callsite", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.CallSiteLineNumberLayoutRenderer>("callsitelinenumber", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.CounterLayoutRenderer>("counter", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.CurrentDirLayoutRenderer>("currentdir", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.DateLayoutRenderer>("date", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.DbNullLayoutRenderer>("dbnull", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.DirectorySeparatorLayoutRenderer>("dirseparator", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.EnvironmentLayoutRenderer>("environment", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.EnvironmentUserLayoutRenderer>("environmentuser", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.EventPropertiesLayoutRenderer>("eventproperties", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.EventPropertiesLayoutRenderer>("eventproperty", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.EventPropertiesLayoutRenderer>("eventcontext", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ExceptionDataLayoutRenderer>("exceptiondata", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ExceptionLayoutRenderer>("exception", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.GarbageCollectorInfoLayoutRenderer>("gc", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.GdcLayoutRenderer>("gdc", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.GuidLayoutRenderer>("guid", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.HostNameLayoutRenderer>("hostname", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.IdentityLayoutRenderer>("identity", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.InstallContextLayoutRenderer>("installcontext", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.LevelLayoutRenderer>("level", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.LevelLayoutRenderer>("loglevel", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.LiteralLayoutRenderer>("literal", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.LoggerNameLayoutRenderer>("loggername", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.LoggerNameLayoutRenderer>("logger", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.LongDateLayoutRenderer>("longdate", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.MachineNameLayoutRenderer>("machinename", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.MessageLayoutRenderer>("message", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.NewLineLayoutRenderer>("newline", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.NLogDirLayoutRenderer>("nlogdir", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ProcessDirLayoutRenderer>("processdir", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ProcessIdLayoutRenderer>("processid", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ProcessInfoLayoutRenderer>("processinfo", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ProcessNameLayoutRenderer>("processname", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ProcessStartLayoutRenderer>("processstart", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ProcessTimeLayoutRenderer>("processtime", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ScopeContextIndentLayoutRenderer>("scopeindent", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ScopeContextNestedStatesLayoutRenderer>("scopenested", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ScopeContextNestedStatesLayoutRenderer>("ndc", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ScopeContextNestedStatesLayoutRenderer>("ndlc", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ScopeContextPropertyLayoutRenderer>("scopeproperty", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ScopeContextPropertyLayoutRenderer>("mdc", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ScopeContextPropertyLayoutRenderer>("mdlc", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ScopeContextTimingLayoutRenderer>("scopetiming", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ScopeContextTimingLayoutRenderer>("ndlctiming", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.SequenceIdLayoutRenderer>("sequenceid", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ShortDateLayoutRenderer>("shortdate", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.SpecialFolderApplicationDataLayoutRenderer>("userApplicationDataDir", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.SpecialFolderCommonApplicationDataLayoutRenderer>("commonApplicationDataDir", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.SpecialFolderLayoutRenderer>("specialfolder", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.SpecialFolderLocalApplicationDataLayoutRenderer>("userLocalApplicationDataDir", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.StackTraceLayoutRenderer>("stacktrace", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.TempDirLayoutRenderer>("tempdir", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ThreadIdLayoutRenderer>("threadid", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.ThreadNameLayoutRenderer>("threadname", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.TicksLayoutRenderer>("ticks", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.TimeLayoutRenderer>("time", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.VariableLayoutRenderer>("var", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.CachedLayoutRendererWrapper>("cached", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.CachedLayoutRendererWrapper>("Cached", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.CachedLayoutRendererWrapper>("ClearCache", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.CachedLayoutRendererWrapper>("CachedSeconds", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.FileSystemNormalizeLayoutRendererWrapper>("filesystemnormalize", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.FileSystemNormalizeLayoutRendererWrapper>("FSNormalize", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.JsonEncodeLayoutRendererWrapper>("jsonencode", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.JsonEncodeLayoutRendererWrapper>("JsonEncode", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.LeftLayoutRendererWrapper>("left", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.LeftLayoutRendererWrapper>("Truncate", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.LowercaseLayoutRendererWrapper>("lowercase", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.LowercaseLayoutRendererWrapper>("Lowercase", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.LowercaseLayoutRendererWrapper>("ToLower", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.NoRawValueLayoutRendererWrapper>("norawvalue", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.NoRawValueLayoutRendererWrapper>("NoRawValue", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.ObjectPathRendererWrapper>("ObjectPath", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.ObjectPathRendererWrapper>("ObjectPath", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.OnExceptionLayoutRendererWrapper>("onexception", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.OnHasPropertiesLayoutRendererWrapper>("onhasproperties", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.PaddingLayoutRendererWrapper>("pad", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.PaddingLayoutRendererWrapper>("Padding", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.PaddingLayoutRendererWrapper>("PadCharacter", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.PaddingLayoutRendererWrapper>("FixedLength", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.PaddingLayoutRendererWrapper>("AlignmentOnTruncation", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.ReplaceLayoutRendererWrapper>("replace", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.ReplaceNewLinesLayoutRendererWrapper>("replacenewlines", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.ReplaceNewLinesLayoutRendererWrapper>("ReplaceNewLines", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.RightLayoutRendererWrapper>("right", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.Rot13LayoutRendererWrapper>("rot13", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.SubstringLayoutRendererWrapper>("substring", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.TrimWhiteSpaceLayoutRendererWrapper>("trimwhitespace", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.TrimWhiteSpaceLayoutRendererWrapper>("TrimWhiteSpace", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.UppercaseLayoutRendererWrapper>("uppercase", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.UppercaseLayoutRendererWrapper>("Uppercase", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.UppercaseLayoutRendererWrapper>("ToUpper", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.UrlEncodeLayoutRendererWrapper>("urlencode", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.WhenEmptyLayoutRendererWrapper>("whenEmpty", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.WhenEmptyLayoutRendererWrapper>("WhenEmpty", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.WhenLayoutRendererWrapper>("when", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.WhenLayoutRendererWrapper>("When", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.WrapLineLayoutRendererWrapper>("wrapline", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.WrapLineLayoutRendererWrapper>("WrapLine", checkTypeExists);
layoutRendererFactory.RegisterType<NLog.LayoutRenderers.Wrappers.XmlEncodeLayoutRendererWrapper>("xmlencode", checkTypeExists);
ambientPropertyFactory.RegisterType<NLog.LayoutRenderers.Wrappers.XmlEncodeLayoutRendererWrapper>("XmlEncode", checkTypeExists);
}
public static void RegisterFilterTypes(ConfigurationItemFactory factory, bool checkTypeExists)
{
var filterFactory = factory.GetFilterFactory();
filterFactory.RegisterType<NLog.Filters.ConditionBasedFilter>("when", checkTypeExists);
filterFactory.RegisterType<NLog.Filters.WhenContainsFilter>("whenContains", checkTypeExists);
filterFactory.RegisterType<NLog.Filters.WhenEqualFilter>("whenEqual", checkTypeExists);
filterFactory.RegisterType<NLog.Filters.WhenNotContainsFilter>("whenNotContains", checkTypeExists);
filterFactory.RegisterType<NLog.Filters.WhenNotEqualFilter>("whenNotEqual", checkTypeExists);
filterFactory.RegisterType<NLog.Filters.WhenRepeatedFilter>("whenRepeated", checkTypeExists);
}
public static void RegisterTimeSourceTypes(ConfigurationItemFactory factory, bool checkTypeExists)
{
var timeSourceFactory = factory.GetTimeSourceFactory();
timeSourceFactory.RegisterType<NLog.Time.AccurateLocalTimeSource>("AccurateLocal", checkTypeExists);
timeSourceFactory.RegisterType<NLog.Time.AccurateUtcTimeSource>("AccurateUTC", checkTypeExists);
timeSourceFactory.RegisterType<NLog.Time.FastLocalTimeSource>("FastLocal", checkTypeExists);
timeSourceFactory.RegisterType<NLog.Time.FastUtcTimeSource>("FastUTC", checkTypeExists);
}
public static void RegisterConditionTypes(ConfigurationItemFactory factory, bool checkTypeExists)
{
var conditionMethodFactory = factory.GetConditionMethodFactory();
factory.RegisterTypeProperties<NLog.Conditions.ConditionAndExpression>(() => null);
factory.RegisterTypeProperties<NLog.Conditions.ConditionExceptionExpression>(() => null);
factory.RegisterTypeProperties<NLog.Conditions.ConditionLayoutExpression>(() => null);
factory.RegisterTypeProperties<NLog.Conditions.ConditionLevelExpression>(() => null);
factory.RegisterTypeProperties<NLog.Conditions.ConditionLiteralExpression>(() => null);
factory.RegisterTypeProperties<NLog.Conditions.ConditionLoggerNameExpression>(() => null);
factory.RegisterTypeProperties<NLog.Conditions.ConditionMessageExpression>(() => null);
factory.RegisterTypeProperties<NLog.Conditions.ConditionMethodExpression>(() => null);
factory.RegisterTypeProperties<NLog.Conditions.ConditionNotExpression>(() => null);
factory.RegisterTypeProperties<NLog.Conditions.ConditionOrExpression>(() => null);
factory.RegisterTypeProperties<NLog.Conditions.ConditionRelationalExpression>(() => null);
if (!checkTypeExists || !conditionMethodFactory.CheckTypeAliasExists("length"))
conditionMethodFactory.RegisterOneParameter("length", (logEvent, arg1) => NLog.Conditions.ConditionMethods.Length(arg1?.ToString()));
if (!checkTypeExists || !conditionMethodFactory.CheckTypeAliasExists("equals"))
conditionMethodFactory.RegisterTwoParameters("equals", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.Equals2(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
if (!checkTypeExists || !conditionMethodFactory.CheckTypeAliasExists("strequals"))
{
conditionMethodFactory.RegisterTwoParameters("strequals", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.Equals2(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
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);
}
if (!checkTypeExists || !conditionMethodFactory.CheckTypeAliasExists("contains"))
{
conditionMethodFactory.RegisterTwoParameters("contains", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.Contains(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
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);
}
if (!checkTypeExists || !conditionMethodFactory.CheckTypeAliasExists("starts-with"))
{
conditionMethodFactory.RegisterTwoParameters("starts-with", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.StartsWith(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
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);
}
if (!checkTypeExists || !conditionMethodFactory.CheckTypeAliasExists("ends-with"))
{
conditionMethodFactory.RegisterTwoParameters("ends-with", (logEvent, arg1, arg2) => NLog.Conditions.ConditionMethods.EndsWith(arg1?.ToString(), arg2?.ToString()) ? Conditions.ConditionExpression.BoxedTrue : Conditions.ConditionExpression.BoxedFalse);
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);
}
}
#pragma warning restore CS0618 // Type or member is obsolete
}
}
@@ -0,0 +1,181 @@
//
// Copyright (c) 2004-2024 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
namespace NLog.LayoutRenderers.Wrappers
{
using System;
using NLog.Config;
using NLog.Internal;
using NLog.Layouts;
/// <summary>
/// Applies caching to another layout output.
/// </summary>
/// <remarks>
/// The value of the inner layout will be rendered only once and reused subsequently.
///
/// <a href="https://github.com/NLog/NLog/wiki/Cached-Layout-Renderer">See NLog Wiki</a>
/// </remarks>
/// <seealso href="https://github.com/NLog/NLog/wiki/Cached-Layout-Renderer">Documentation on NLog Wiki</seealso>
[LayoutRenderer("cached")]
[AmbientProperty(nameof(Cached))]
[AmbientProperty(nameof(ClearCache))]
[AmbientProperty(nameof(CachedSeconds))]
[AppDomainFixedOutput]
[ThreadAgnostic]
public sealed class CachedLayoutRendererWrapper : WrapperLayoutRendererBase, IStringValueRenderer
{
/// <summary>
/// A value indicating when the cache is cleared.
/// </summary>
[Flags]
public enum ClearCacheOption
{
/// <summary>Never clear the cache.</summary>
None = 0,
/// <summary>Clear the cache whenever the <see cref="CachedLayoutRendererWrapper"/> is initialized.</summary>
OnInit = 1,
/// <summary>Clear the cache whenever the <see cref="CachedLayoutRendererWrapper"/> is closed.</summary>
OnClose = 2
}
private readonly object _lockObject = new object();
private string? _cachedValue;
private string? _renderedCacheKey;
private DateTime _cachedValueExpires;
private TimeSpan? _cachedValueTimeout;
/// <summary>
/// Gets or sets a value indicating whether this <see cref="CachedLayoutRendererWrapper"/> is enabled.
/// </summary>
/// <remarks>Default: <see langword="true"/></remarks>
/// <docgen category='Layout Options' order='10' />
public bool Cached { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating when the cache is cleared.
/// </summary>
/// <remarks>Default: <see cref="ClearCacheOption.OnInit"/> | <see cref="ClearCacheOption.OnClose"/></remarks>
/// <docgen category='Layout Options' order='10' />
public ClearCacheOption ClearCache { get; set; } = ClearCacheOption.OnInit | ClearCacheOption.OnClose;
/// <summary>
/// Gets or sets whether to reset cached value when CacheKey output changes. Example CacheKey could render current day, so the cached-value is reset on day roll.
/// </summary>
/// <remarks>Default: <see langword="null"/></remarks>
/// <docgen category='Layout Options' order='10' />
public Layout? CacheKey { get; set; }
/// <summary>
/// Gets or sets a value indicating how many seconds the value should stay cached until it expires
/// </summary>
/// <remarks>Default: <see langword="0"/></remarks>
/// <docgen category='Layout Options' order='10' />
public int CachedSeconds
{
get => (int)(_cachedValueTimeout?.TotalSeconds ?? 0.0);
set
{
_cachedValueTimeout = TimeSpan.FromSeconds(value);
if (_cachedValueTimeout > TimeSpan.Zero)
Cached = true;
}
}
/// <inheritdoc/>
protected override void InitializeLayoutRenderer()
{
base.InitializeLayoutRenderer();
if ((ClearCache & ClearCacheOption.OnInit) == ClearCacheOption.OnInit)
_cachedValue = null;
}
/// <inheritdoc/>
protected override void CloseLayoutRenderer()
{
base.CloseLayoutRenderer();
if ((ClearCache & ClearCacheOption.OnClose) == ClearCacheOption.OnClose)
_cachedValue = null;
}
/// <inheritdoc/>
protected override string Transform(string text)
{
return text;
}
/// <inheritdoc/>
protected override string RenderInner(LogEventInfo logEvent)
{
if (Cached)
{
var newCacheKey = CacheKey?.Render(logEvent) ?? string.Empty;
var cachedValue = LookupValidCachedValue(logEvent, newCacheKey);
if (cachedValue is null)
{
lock (_lockObject)
{
cachedValue = LookupValidCachedValue(logEvent, newCacheKey);
if (cachedValue is null)
{
_cachedValue = cachedValue = base.RenderInner(logEvent);
_renderedCacheKey = newCacheKey;
if (_cachedValueTimeout.HasValue)
_cachedValueExpires = logEvent.TimeStamp + _cachedValueTimeout.Value;
}
}
}
return cachedValue;
}
else
{
return base.RenderInner(logEvent);
}
}
string? LookupValidCachedValue(LogEventInfo logEvent, string newCacheKey)
{
if (_renderedCacheKey != newCacheKey)
return null;
if (_cachedValueTimeout.HasValue && logEvent.TimeStamp > _cachedValueExpires)
return null;
return _cachedValue;
}
string? IStringValueRenderer.GetFormattedString(LogEventInfo logEvent) => Cached ? RenderInner(logEvent) : null;
}
}
@@ -0,0 +1,131 @@
//
// Copyright (c) 2004-2024 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
using NLog.Config;
namespace NLog.UnitTests.LayoutRenderers.Wrappers
{
using System.Linq;
using NLog;
using NLog.Layouts;
using Xunit;
public class CachedTests : NLogTestBase
{
[Fact]
public void CachedLayoutRendererWrapper()
{
SimpleLayout l = "${guid}";
string s1 = l.Render(LogEventInfo.CreateNullEvent());
string s2 = l.Render(LogEventInfo.CreateNullEvent());
string s3;
// normally GUIDs are never the same
Assert.NotEqual(s1, s2);
// but when you apply ${cached}, the guid will only be generated once
l = "${cached:${guid}:cached=true}";
s1 = l.Render(LogEventInfo.CreateNullEvent());
s2 = l.Render(LogEventInfo.CreateNullEvent());
Assert.Equal(s1, s2);
// calling Close() on Layout Renderer will reset the cached value
l.LayoutRenderers.First().Close();
s3 = l.Render(LogEventInfo.CreateNullEvent());
Assert.NotEqual(s2, s3);
// unless we use clearcache=none
l = "${cached:${guid}:cached=true:clearcache=none}";
s1 = l.Render(LogEventInfo.CreateNullEvent());
l.LayoutRenderers.First().Close();
s2 = l.Render(LogEventInfo.CreateNullEvent());
Assert.Equal(s1, s2);
// another way to achieve the same thing is using cached=true
l = "${guid:cached=true}";
s1 = l.Render(LogEventInfo.CreateNullEvent());
s2 = l.Render(LogEventInfo.CreateNullEvent());
Assert.Equal(s1, s2);
// another way to achieve the same thing is using cached=true
l = "${guid:cached=false}";
s1 = l.Render(LogEventInfo.CreateNullEvent());
s2 = l.Render(LogEventInfo.CreateNullEvent());
Assert.NotEqual(s1, s2);
}
/// <summary>
/// test the cachekey
/// </summary>
[Fact]
public void CacheKeyTest()
{
LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"
<nlog>
<targets><target name='debug' type='debug' layout='${cached:${guid}:cached=true:cachekey=${var:var1}}' /></targets>
<rules>
<logger name='*' minlevel='debug' appendto='debug'>
</logger>
</rules>
</nlog>");
LogManager.Configuration.Variables["var1"] = "a";
var logger = LogManager.GetLogger("A");
logger.Debug("msg");
var s1 = GetDebugLastMessage("debug");
logger.Debug("msg");
var s2 = GetDebugLastMessage("debug");
Assert.NotEmpty(s1);
Assert.Equal(s1, s2);
//change var will reset cache
LogManager.Configuration.Variables["var1"] = "b";
logger.Debug("msg");
var s3 = GetDebugLastMessage("debug");
Assert.NotEmpty(s3);
Assert.NotEqual(s1, s3);
}
[Fact]
public void CachedSecondsTimeoutTest()
{
SimpleLayout l = "${guid:cachedSeconds=60}";
var s1 = l.Render(LogEventInfo.CreateNullEvent());
var s2 = l.Render(new LogEventInfo());
Assert.Equal(s1, s2);
var s3 = l.Render(new LogEventInfo() { TimeStamp = NLog.Time.TimeSource.Current.Time.AddMinutes(2) });
Assert.NotEqual(s2, s3);
}
}
}