private static bool WriteToTargetWithFilterChain(Targets.Target target, FilterResult result, LogEventInfo logEvent, AsyncContinuation onException) { if ((result == FilterResult.Ignore) || (result == FilterResult.IgnoreFinal)) { if (InternalLogger.IsDebugEnabled) { InternalLogger.Debug("{0}.{1} Rejecting message because of a filter.", logEvent.LoggerName, logEvent.Level); } if (result == FilterResult.IgnoreFinal) { return(false); } return(true); } target.WriteAsyncLogEvent(logEvent.WithContinuation(onException)); if (result == FilterResult.LogFinal) { return(false); } return(true); }
/// <summary> /// Configures NLog for to log to the specified target so that all messages /// above and including the specified level are output. /// </summary> /// <param name="target">The target to log all messages to.</param> /// <param name="minLevel">The minimal logging level.</param> public static void ConfigureForTargetLogging(Target target, LogLevel minLevel) { LoggingConfiguration config = new LoggingConfiguration(); LoggingRule rule = new LoggingRule("*", minLevel, target); config.LoggingRules.Add(rule); LogManager.Configuration = config; }
/// <summary> /// Initialize logger /// </summary> private void initLoggerCfg() { LoggingConfiguration config = LogManager.Configuration; if (config == null) { config = new LoggingConfiguration(); } NLog.Targets.Target t = config.FindTargetByName(GuidList.PACKAGE_LOGGER); if (t != null) { return; // the config is already contains our logger } // configure entry point config.AddTarget(GuidList.PACKAGE_LOGGER, target); config.LoggingRules.Add(new LoggingRule(GuidList.PACKAGE_LOGGER, LogLevel.Trace, target)); // update configuration LogManager.Configuration = config; LogManager.Configuration.Reload(); }
/// <summary> /// Create a new <see cref="LoggingRule" /> with a <paramref name="minLevel"/> and <paramref name="maxLevel"/> which writes to <paramref name="target"/>. /// </summary> /// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param> /// <param name="minLevel">Minimum log level needed to trigger this rule.</param> /// <param name="maxLevel">Maximum log level needed to trigger this rule.</param> /// <param name="target">Target to be written to when the rule matches.</param> public LoggingRule(string loggerNamePattern, LogLevel minLevel, LogLevel maxLevel, Target target) : this() { this.LoggerNamePattern = loggerNamePattern; this.Targets.Add(target); EnableLoggingForLevels(minLevel, maxLevel); }
#pragma warning restore IDE0060 // Remove unused parameter #region Target Methods /// <summary>Adds the supplied target to the current NLog configuration.</summary> /// <param name="target">The target.</param> /// <param name="minLevel"></param> // ReSharper disable once RedundantNameQualifier // ReSharper disable once UnusedMember.Global public static void AddTarget(NLog.Targets.Target target, LogLevel minLevel) { if (minLevel == null) { minLevel = LogLevel.Trace; } LogManager.Configuration.AddTarget(target); LogManager.Configuration.AddRule(minLevel, LogLevel.Fatal, target); LogManager.ReconfigExistingLoggers(); }
/// <summary> /// Initializes a new instance of the <see cref="LoggingRule" /> class. /// </summary> /// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param> /// <param name="minLevel">Minimum log level needed to trigger this rule.</param> /// <param name="target">Target to be written to when the rule matches.</param> public LoggingRule(string loggerNamePattern, LogLevel minLevel, Target target) { this.Filters = new List<Filter>(); this.ChildRules = new List<LoggingRule>(); this.Targets = new List<Target>(); this.LoggerNamePattern = loggerNamePattern; this.Targets.Add(target); for (int i = minLevel.Ordinal; i <= LogLevel.MaxLevel.Ordinal; ++i) { this.EnableLoggingForLevel(LogLevel.FromOrdinal(i)); } }
/// <summary> /// Initializes a new instance of the <see cref="AspNetBufferingTargetWrapper" /> class. /// </summary> /// <param name="wrappedTarget">The wrapped target.</param> public AspNetBufferingTargetWrapper(Target wrappedTarget) : this(wrappedTarget, 100) { }
/// <summary> /// Adds an element with the specified key and value to this TargetDictionary. /// </summary> /// <param name="key"> /// The string key of the element to add. /// </param> /// <param name="value"> /// The Target value of the element to add. /// </param> public virtual void Add(string key, Target value) { this.Dictionary.Add(key, value); }
/// <summary> /// Determines whether this TargetDictionary contains a specific value. /// </summary> /// <param name="value"> /// The Target value to locate in this TargetDictionary. /// </param> /// <returns> /// true if this TargetDictionary contains an element with the specified value; /// otherwise, false. /// </returns> public virtual bool ContainsValue(Target value) { foreach (Target item in this.Dictionary.Values) { if (item == value) return true; } return false; }
private static void AssertOutput(Target target, string message, string[] expectedParts) { var consoleOutWriter = new PartsWriter(); TextWriter oldConsoleOutWriter = Console.Out; Console.SetOut(consoleOutWriter); try { var exceptions = new List<Exception>(); target.Initialize(null); target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "Logger", message).WithContinuation(exceptions.Add)); target.Close(); Assert.Equal(1, exceptions.Count); Assert.True(exceptions.TrueForAll(e => e == null)); } finally { Console.SetOut(oldConsoleOutWriter); } var expected = Enumerable.Repeat("Logger " + expectedParts[0], 1).Concat(expectedParts.Skip(1)); Assert.Equal(expected, consoleOutWriter.Values); }
public TargetWithFilterChain(Target a, FilterCollection filterChain) { _target = a; _filterChain = filterChain; _needsStackTrace = 0; }
/// <summary> /// Configures NLog for to log to the specified target so that all messages /// above and including the <see cref="LogLevel.Info"/> level are output. /// </summary> /// <param name="target">The target to log all messages to.</param> public static void ConfigureForTargetLogging(Target target) { ConfigureForTargetLogging(target, LogLevel.Info); }
/// <summary> /// Initializes a new instance of the <see cref="LoggingRule" /> class. /// </summary> /// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param> /// <param name="target">Target to be written to when the rule matches.</param> /// <remarks>By default no logging levels are defined. You should call <see cref="EnableLoggingForLevel"/> and <see cref="DisableLoggingForLevel"/> to set them.</remarks> public LoggingRule(string loggerNamePattern, Target target) { this.Filters = new List<Filter>(); this.ChildRules = new List<LoggingRule>(); this.Targets = new List<Target>(); this.LoggerNamePattern = loggerNamePattern; this.Targets.Add(target); }
private Target WrapWithDefaultWrapper(Target t, XmlReader reader) { string wrapperType; if (!this.TryGetCaseInsensitiveAttribute(reader, "type", out wrapperType)) { // TODO - add error handling } Target wrapperTargetInstance = this.nlogFactories.TargetFactory.CreateInstance(wrapperType); WrapperTargetBase wtb = wrapperTargetInstance as WrapperTargetBase; if (wtb == null) { throw new NLogConfigurationException("Target type specified on <default-wrapper /> is not a wrapper."); } this.ParseTargetElement(wrapperTargetInstance, reader); 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 Target WrapWithAsyncTarget(Target t) { AsyncTargetWrapper atw = new AsyncTargetWrapper(); atw.WrappedTarget = t; atw.Name = t.Name; t.Name = t.Name + "_wrapped"; InternalLogger.Debug("Wrapping target '{0}' with AsyncTargetWrapper and renaming to '{1}", atw.Name, t.Name); return atw; }
private void ParseTargetElement(Target target, XmlReader reader) { InternalLogger.Trace("ParseTargetElement name={0} type={1}", reader.GetAttribute("name"), reader.GetAttribute("type")); var compound = target as CompoundTargetBase; var wrapper = target as WrapperTargetBase; this.ConfigureObjectFromAttributes(target, reader, this.variables, true); if (!reader.IsEmptyElement) { while (this.MoveToNextElement(reader)) { string name = reader.LocalName.ToLower(CultureInfo.InvariantCulture); if (compound != null) { if (name == "target" || name == "wrapper" || name == "wrapper-target" || name == "compound-target") { string type; if (!this.TryGetCaseInsensitiveAttribute(reader, "type", out type)) { throw new NLogConfigurationException("Missing 'type' attribute on <" + name + " />"); } Target newTarget = this.nlogFactories.TargetFactory.CreateInstance(type); if (newTarget != null) { this.ParseTargetElement(newTarget, reader); 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; if (!this.TryGetCaseInsensitiveAttribute(reader, "type", out type)) { throw new NLogConfigurationException("Missing 'type' attribute on <" + name + " />"); } Target newTarget = this.nlogFactories.TargetFactory.CreateInstance(type); if (newTarget != null) { this.ParseTargetElement(newTarget, reader); 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; } } this.SetPropertyFromElement(target, reader); } } }
/// <summary> /// Initializes a new instance of the <see cref="AspNetBufferingTargetWrapper" /> class. /// </summary> /// <param name="wrappedTarget">The wrapped target.</param> /// <param name="bufferSize">Size of the buffer.</param> public AspNetBufferingTargetWrapper(Target wrappedTarget, int bufferSize) { WrappedTarget = wrappedTarget; BufferSize = bufferSize; GrowBufferAsNeeded = true; }
private static Target WrapWithAsyncTargetWrapper(Target target) { var asyncTargetWrapper = new AsyncTargetWrapper(); asyncTargetWrapper.WrappedTarget = target; asyncTargetWrapper.Name = target.Name; target.Name = target.Name + "_wrapped"; InternalLogger.Debug("Wrapping target '{0}' with AsyncTargetWrapper and renaming to '{1}", asyncTargetWrapper.Name, target.Name); target = asyncTargetWrapper; return target; }
/// <summary> /// Create a (disabled) <see cref="LoggingRule" />. You should call <see cref="EnableLoggingForLevel"/> or see cref="EnableLoggingForLevels"/> to enable logging. /// </summary> /// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param> /// <param name="target">Target to be written to when the rule matches.</param> public LoggingRule(string loggerNamePattern, Target target) : this() { this.LoggerNamePattern = loggerNamePattern; this.Targets.Add(target); }
private Target WrapWithDefaultWrapper(Target t, NLogXmlElement defaultParameters) { string wrapperType = StripOptionalNamespacePrefix(defaultParameters.GetRequiredAttribute("type")); Target wrapperTargetInstance = this.ConfigurationItemFactory.Targets.CreateInstance(wrapperType); WrapperTargetBase wtb = wrapperTargetInstance as WrapperTargetBase; if (wtb == null) { throw new NLogConfigurationException("Target type specified on <default-wrapper /> is not a wrapper."); } this.ParseTargetElement(wrapperTargetInstance, defaultParameters); 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; }
/// <summary> /// Initializes a new instance of the <see cref="TargetWithFilterChain" /> class. /// </summary> /// <param name="target">The target.</param> /// <param name="filterChain">The filter chain.</param> public TargetWithFilterChain(Target target, IList<Filter> filterChain) { this.Target = target; this.FilterChain = filterChain; this.stackTraceUsage = StackTraceUsage.None; }
private void ParseTargetElement(Target target, NLogXmlElement targetElement) { var compound = target as CompoundTargetBase; var wrapper = target as WrapperTargetBase; this.ConfigureObjectFromAttributes(target, targetElement, true); foreach (var childElement in targetElement.Children) { string name = childElement.LocalName; if (compound != null) { if (IsTargetRefElement(name)) { string targetName = childElement.GetRequiredAttribute("name"); Target newTarget = this.FindTargetByName(targetName); if (newTarget == null) { throw new NLogConfigurationException("Referenced target '" + targetName + "' not found."); } compound.Targets.Add(newTarget); continue; } if (IsTargetElement(name)) { string type = StripOptionalNamespacePrefix(childElement.GetRequiredAttribute("type")); Target newTarget = this.ConfigurationItemFactory.Targets.CreateInstance(type); if (newTarget != null) { this.ParseTargetElement(newTarget, childElement); 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 (IsTargetRefElement(name)) { string targetName = childElement.GetRequiredAttribute("name"); Target newTarget = this.FindTargetByName(targetName); if (newTarget == null) { throw new NLogConfigurationException("Referenced target '" + targetName + "' not found."); } wrapper.WrappedTarget = newTarget; continue; } if (IsTargetElement(name)) { string type = StripOptionalNamespacePrefix(childElement.GetRequiredAttribute("type")); Target newTarget = this.ConfigurationItemFactory.Targets.CreateInstance(type); if (newTarget != null) { this.ParseTargetElement(newTarget, childElement); 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; } } this.SetPropertyFromElement(target, childElement); } }
/// <summary> /// Initializes a new instance of the <see cref="TargetWithFilterChain" /> class. /// </summary> /// <param name="target">The target.</param> /// <param name="filterChain">The filter chain.</param> public TargetWithFilterChain(Target target, IList<Filter> filterChain) { this.Target = target; this.FilterChain = filterChain; }
/// <summary> /// Registers the specified target object under a given name. /// </summary> /// <param name="name"> /// Name of the target. /// </param> /// <param name="target"> /// The target object. /// </param> public void AddTarget(string name, Target target) { if (name == null) { throw new ArgumentException("Target name cannot be null", "name"); } InternalLogger.Debug("Registering target {0}: {1}", name, target.GetType().FullName); this.targets[name] = target; }
private int _state; // 0 = idle, 1 = timer active, 2 = timer active + possibly more work #endregion Fields #region Constructors public SlowRunningAsyncTargetWrapper(Target wrappedTarget) : base(wrappedTarget) { }