NLog WriteToTargets method

 

 public void Debug(Exception exception, [Localizable(false)] string message)
        {
            if (IsDebugEnabled)
            {
                WriteToTargets(LogLevel.Debug, exception, message, null);
            }
        }

 

 

https://github.com/NLog/NLog/blob/dev/src/NLog/Logger.cs#L599

private void WriteToTargets(LogLevel level, Exception ex, [Localizable(false)] string message, object[] args)

复制代码
 private void WriteToTargets(LogLevel level, Exception ex, [Localizable(false)] string message, object[] args)
        {
            var targetsForLevel = GetTargetsForLevel(level);
            if (targetsForLevel != null)
            {
                var logEvent = LogEventInfo.Create(level, Name, ex, Factory.DefaultCultureInfo, message, args);
                WriteToTargets(logEvent, targetsForLevel);
            }
        }
复制代码

 

 

  private void WriteToTargets([NotNull] LogEventInfo logEvent, [NotNull] TargetWithFilterChain targetsForLevel)
        {
            LoggerImpl.Write(DefaultLoggerType, targetsForLevel, PrepareLogEventInfo(logEvent), Factory);
        }

 

复制代码
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", Justification = "Using 'NLog' in message.")]
        internal static void Write([NotNull] Type loggerType, [NotNull] TargetWithFilterChain targetsForLevel, LogEventInfo logEvent, LogFactory logFactory)
        {
            logEvent.SetMessageFormatter(logFactory.ActiveMessageFormatter, targetsForLevel.NextInChain == null ? logFactory.SingleTargetMessageFormatter : null);

#if CaptureCallSiteInfo
            StackTraceUsage stu = targetsForLevel.GetStackTraceUsage();
            if (stu != StackTraceUsage.None)
            {
                bool attemptCallSiteOptimization = targetsForLevel.TryCallSiteClassNameOptimization(stu, logEvent);
                if (attemptCallSiteOptimization && targetsForLevel.TryLookupCallSiteClassName(logEvent, out string callSiteClassName))
                {
                    logEvent.CallSiteInformation.CallerClassName = callSiteClassName;
                }
                else if (attemptCallSiteOptimization || targetsForLevel.MustCaptureStackTrace(stu, logEvent))
                {
                    CaptureCallSiteInfo(logFactory, loggerType, logEvent, stu);

                    if (attemptCallSiteOptimization)
                    {
                        targetsForLevel.TryRememberCallSiteClassName(logEvent);
                    }
                }
            }
#endif

            AsyncContinuation exceptionHandler = (ex) => { };
            if (logFactory.ThrowExceptions)
            {
                int originalThreadId = AsyncHelpers.GetManagedThreadId();
                exceptionHandler = ex =>
                {
                    if (ex != null && AsyncHelpers.GetManagedThreadId() == originalThreadId)
                    {
                        throw new NLogRuntimeException("Exception occurred in NLog", ex);
                    }
                };
            }

            IList<Filter> prevFilterChain = null;
            FilterResult prevFilterResult = FilterResult.Neutral;
            for (var t = targetsForLevel; t != null; t = t.NextInChain)
            {
                FilterResult result = ReferenceEquals(prevFilterChain, t.FilterChain) ?
                    prevFilterResult : GetFilterResult(t.FilterChain, logEvent, t.DefaultResult);
                if (!WriteToTargetWithFilterChain(t.Target, result, logEvent, exceptionHandler))
                {
                    break;
                }

                prevFilterResult = result;  // Cache the result, and reuse it for the next target, if it comes from the same logging-rule
                prevFilterChain = t.FilterChain;
            }
        }
复制代码

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(369)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2018-07-06 What does jQuery.fn mean?
2018-07-06 Bootstrap4 网格系统
2017-07-06 out
2017-07-06 TortoiseSvn安装的时候,将svn的命令行工具单独隔离出来
2015-07-06 Binary to Text (ASCII) Conversion
2015-07-06 Pizza pieces
点击右上角即可分享
微信分享提示