Esempio n. 1
0
File: Target.cs Progetto: zmidl/NLog
        /// <summary>
        /// Calls the <see cref="Layout.Precalculate"/> on each volatile layout
        /// used by this target.
        /// This method won't prerender if all layouts in this target are thread-agnostic.
        /// </summary>
        /// <param name="logEvent">
        /// The log event.
        /// </param>
        public void PrecalculateVolatileLayouts(LogEventInfo logEvent)
        {
            if (_allLayoutsAreThreadAgnostic && (!_oneLayoutIsMutableUnsafe || logEvent.IsLogEventMutableSafe()))
            {
                return;
            }

            // Not all Layouts support concurrent threads, so we have to protect them
            if (OptimizeBufferReuse && _allLayoutsAreThreadSafe)
            {
                PrecalculateVolatileLayoutsConcurrent(logEvent);
            }
            else
            {
                PrecalculateVolatileLayoutsWithLock(logEvent);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Calls the <see cref="Layout.Precalculate"/> on each volatile layout
        /// used by this target.
        /// This method won't prerender if all layouts in this target are thread-agnostic.
        /// </summary>
        /// <param name="logEvent">
        /// The log event.
        /// </param>
        public void PrecalculateVolatileLayouts(LogEventInfo logEvent)
        {
            if (_allLayoutsAreThreadAgnostic)
            {
                if (!_oneLayoutIsMutableUnsafe || logEvent.IsLogEventMutableSafe())
                {
                    return;
                }
            }

            // Not all Layouts support concurrent threads, so we have to protect them
            if (OptimizeBufferReuse)
            {
                if (_allLayoutsAreThreadSafe)
                {
                    if (!IsInitialized)
                    {
                        return;
                    }

                    if (_allLayouts == null)
                    {
                        return;
                    }

                    if (_precalculateStringBuilderPool == null)
                    {
                        System.Threading.Interlocked.CompareExchange(ref _precalculateStringBuilderPool, new StringBuilderPool(System.Environment.ProcessorCount * 4, 1024), null);
                    }

                    using (var targetBuilder = _precalculateStringBuilderPool.Acquire())
                    {
                        foreach (Layout layout in _allLayouts)
                        {
                            targetBuilder.Item.ClearBuilder();
                            layout.PrecalculateBuilder(logEvent, targetBuilder.Item);
                        }
                    }
                }
                else
                {
                    lock (SyncRoot)
                    {
                        if (!_isInitialized)
                        {
                            return;
                        }

                        if (_allLayouts == null)
                        {
                            return;
                        }

                        using (var targetBuilder = ReusableLayoutBuilder.Allocate())
                        {
                            foreach (Layout layout in _allLayouts)
                            {
                                targetBuilder.Result.ClearBuilder();
                                layout.PrecalculateBuilder(logEvent, targetBuilder.Result);
                            }
                        }
                    }
                }
            }
            else
            {
                lock (SyncRoot)
                {
                    if (!_isInitialized)
                    {
                        return;
                    }

                    if (_allLayouts == null)
                    {
                        return;
                    }

                    foreach (Layout layout in _allLayouts)
                    {
                        layout.Precalculate(logEvent);
                    }
                }
            }
        }