internal static void Write(Type loggerType, TargetWithFilterChain targets, LogEventInfo logEvent, LogFactory factory) { if (targets == null) { return; } #if !NET_CF var stu = targets.GetStackTraceUsage(); if (stu != StackTraceUsage.None && !logEvent.HasStackTrace) { StackTrace stackTrace; #if !SILVERLIGHT stackTrace = new StackTrace(StackTraceSkipMethods, stu == StackTraceUsage.WithSource); #else stackTrace = new StackTrace(); #endif var firstUserFrame = FindCallingMethodOnStackTrace(stackTrace, loggerType); logEvent.SetStackTrace(stackTrace, firstUserFrame); } #endif var originalThreadId = Thread.CurrentThread.ManagedThreadId; AsyncContinuation exceptionHandler = ex => { if (ex != null) { if (factory.ThrowExceptions && Thread.CurrentThread.ManagedThreadId == originalThreadId) { throw new NLogRuntimeException("Exception occurred in NLog", ex); } } }; for (var t = targets; t != null; t = t.NextInChain) { if (!WriteToTargetWithFilterChain(t, logEvent, exceptionHandler)) { break; } } }
internal void GetTargetsByLevelForLogger(string name, IList<LoggingRule> rules, TargetWithFilterChain[] targetsByLevel, TargetWithFilterChain[] lastTargetsByLevel) { foreach (var rule in rules) { if (!rule.NameMatches(name)) { continue; } for (var i = 0; i <= LogLevel.MaxLevel.Ordinal; ++i) { if (i < GlobalThreshold.Ordinal || !rule.IsLoggingEnabledForLevel(LogLevel.FromOrdinal(i))) { continue; } foreach (var target in rule.Targets) { var awf = new TargetWithFilterChain(target, rule.Filters); if (lastTargetsByLevel[i] != null) { lastTargetsByLevel[i].NextInChain = awf; } else { targetsByLevel[i] = awf; } lastTargetsByLevel[i] = awf; } } GetTargetsByLevelForLogger(name, rule.ChildRules, targetsByLevel, lastTargetsByLevel); if (rule.Final) { break; } } for (var i = 0; i <= LogLevel.MaxLevel.Ordinal; ++i) { var tfc = targetsByLevel[i]; if (tfc != null) { tfc.PrecalculateStackTraceUsage(); } } }
internal LoggerConfiguration GetConfigurationForLogger(string name, LoggingConfiguration configuration) { var targetsByLevel = new TargetWithFilterChain[LogLevel.MaxLevel.Ordinal + 1]; var lastTargetsByLevel = new TargetWithFilterChain[LogLevel.MaxLevel.Ordinal + 1]; if (configuration != null && IsLoggingEnabled()) { GetTargetsByLevelForLogger(name, configuration.LoggingRules, targetsByLevel, lastTargetsByLevel); } InternalLogger.Debug("Targets for {0} by level:", name); for (var i = 0; i <= LogLevel.MaxLevel.Ordinal; ++i) { var sb = new StringBuilder(); sb.AppendFormat(CultureInfo.InvariantCulture, "{0} =>", LogLevel.FromOrdinal(i)); for (var afc = targetsByLevel[i]; afc != null; afc = afc.NextInChain) { sb.AppendFormat(CultureInfo.InvariantCulture, " {0}", afc.Target.Name); if (afc.FilterChain.Count > 0) { sb.AppendFormat(CultureInfo.InvariantCulture, " ({0} filters)", afc.FilterChain.Count); } } InternalLogger.Debug(sb.ToString()); } return new LoggerConfiguration(targetsByLevel); }
/// <summary> /// Creates a logger that discards all log messages. /// </summary> /// <returns>Null logger instance.</returns> public Logger CreateNullLogger() { var targetsByLevel = new TargetWithFilterChain[LogLevel.MaxLevel.Ordinal + 1]; var newLogger = new Logger(); newLogger.Initialize(string.Empty, new LoggerConfiguration(targetsByLevel), this); return newLogger; }
private static bool WriteToTargetWithFilterChain(TargetWithFilterChain targetListHead, LogEventInfo logEvent, AsyncContinuation onException) { var target = targetListHead.Target; var result = GetFilterResult(targetListHead.FilterChain, logEvent); 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; }