/// <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) { var config = new LoggingConfiguration(); var rule = new LoggingRule("*", minLevel, target); config.LoggingRules.Add(rule); LogManager.Configuration = config; }
/// <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) { Filters = new List<Filter>(); ChildRules = new List<LoggingRule>(); Targets = new List<Target>(); LoggerNamePattern = loggerNamePattern; Targets.Add(target); }
/// <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) { Filters = new List<Filter>(); ChildRules = new List<LoggingRule>(); Targets = new List<Target>(); LoggerNamePattern = loggerNamePattern; Targets.Add(target); for (var i = minLevel.Ordinal; i <= LogLevel.MaxLevel.Ordinal; ++i) { EnableLoggingForLevel(LogLevel.FromOrdinal(i)); } }
private Target WrapWithDefaultWrapper(Target t, NLogXmlElement defaultParameters) { var wrapperType = StripOptionalNamespacePrefix(defaultParameters.GetRequiredAttribute("type")); var wrapperTargetInstance = configurationItemFactory.Targets.CreateInstance(wrapperType); var wtb = wrapperTargetInstance as WrapperTargetBase; if (wtb == null) { throw new NLogConfigurationException("Target type specified on <default-wrapper /> is not a wrapper."); } 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; }
private void ParseTargetElement(Target target, NLogXmlElement targetElement) { var compound = target as CompoundTargetBase; var wrapper = target as WrapperTargetBase; ConfigureObjectFromAttributes(target, targetElement, true); foreach (var childElement in targetElement.Children) { var name = childElement.LocalName; if (compound != null) { if (IsTargetRefElement(name)) { var targetName = childElement.GetRequiredAttribute("name"); var newTarget = FindTargetByName(targetName); if (newTarget == null) { throw new NLogConfigurationException("Referenced target '" + targetName + "' not found."); } compound.Targets.Add(newTarget); continue; } if (IsTargetElement(name)) { var type = StripOptionalNamespacePrefix(childElement.GetRequiredAttribute("type")); var newTarget = configurationItemFactory.Targets.CreateInstance(type); if (newTarget != null) { 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)) { var targetName = childElement.GetRequiredAttribute("name"); var newTarget = FindTargetByName(targetName); if (newTarget == null) { throw new NLogConfigurationException("Referenced target '" + targetName + "' not found."); } wrapper.WrappedTarget = newTarget; continue; } if (IsTargetElement(name)) { var type = StripOptionalNamespacePrefix(childElement.GetRequiredAttribute("type")); var newTarget = configurationItemFactory.Targets.CreateInstance(type); if (newTarget != null) { 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; } } SetPropertyFromElement(target, childElement); } }
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> /// 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); targets[name] = target; }
/// <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) { Target = target; FilterChain = filterChain; stackTraceUsage = StackTraceUsage.None; }
/// <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); }