Esempio n. 1
0
        /// <summary>
        /// Gets a <see cref="IPropertyRenderer"/> for the specified type.
        /// </summary>
        /// <param name="type">The type that can be rendered by this renderer.</param>
        /// <returns>A <see cref="IPropertyRenderer"/> reference, or null if no renderer could be found.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="type"/> is null.</exception>
        public IPropertyRenderer GetRenderer(Type type)
        {
#if CONTRACTS_ONLY
            BaitExceptionHelper.Throw();
            return(null);
#else
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (_KnownRenderers.Count == 0)
            {
                return(null);
            }

            IPropertyRenderer retVal = null;

            retVal = GetFromCache(type);
            if (retVal == null)
            {
                retVal = LookupByType(type);
                _CachedRenderers.TryAdd(type, retVal);
            }

            return(retVal);
#endif
        }
Esempio n. 2
0
        private IPropertyRenderer LookupByType(Type type)
        {
#if CONTRACTS_ONLY
            BaitExceptionHelper.Throw();
            return(null);
#else
            var originalType         = type;
            IPropertyRenderer retVal = null;
            while (type != null && !_KnownRenderers.TryGetValue(type, out retVal))
            {
#if !NETFX_CORE
                type = type.BaseType;
#else
                type = type.GetTypeInfo().BaseType;
#endif
            }

            if (retVal == null)
            {
                var interfaces = originalType.GetInterfaces();
                for (int cnt = 0; cnt < interfaces.Length; cnt++)
                {
                    if (_KnownRenderers.TryGetValue(interfaces[cnt], out retVal))
                    {
                        break;
                    }
                }
            }

            return(retVal);
#endif
        }
Esempio n. 3
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        public TypeRendererMap() : this(null)
        {
#if CONTRACTS_ONLY
            BaitExceptionHelper.Throw();
#else
            _KnownRenderers  = new System.Collections.Concurrent.ConcurrentDictionary <Type, IPropertyRenderer>();
            _CachedRenderers = new System.Collections.Concurrent.ConcurrentDictionary <Type, IPropertyRenderer>();
#endif
        }
Esempio n. 4
0
        private IPropertyRenderer GetFromCache(Type type)
        {
#if CONTRACTS_ONLY
            BaitExceptionHelper.Throw();
            return(null);
#else
            IPropertyRenderer retVal = null;
            _CachedRenderers.TryGetValue(type, out retVal);
            return(retVal);
#endif
        }
Esempio n. 5
0
        protected override void AddPropertiesCore(LogEvent logEvent, ITypeRendererMap rendererMap)
        {
#if CONTRACTS_ONLY
            BaitExceptionHelper.Throw();
#else
#if NETFX_CORE
            var stacktrace = new System.Diagnostics.StackTrace(new Exception(), true);
#else
            var stacktrace = new System.Diagnostics.StackTrace(true);
#endif
            AddProperty(logEvent.Properties, "Stacktrace", stacktrace.ToString(), rendererMap);
#endif
        }
Esempio n. 6
0
        /// <summary>
        /// Gets a <see cref="IPropertyRenderer"/> for the provided type.
        /// </summary>
        /// <typeparam name="T">The type of value the renderer operates on.</typeparam>
        /// <returns>A <see cref="IPropertyRenderer"/> reference, or null if no renderer could be found.</returns>
        public IPropertyRenderer GetRenderer <T>()
        {
#if CONTRACTS_ONLY
            BaitExceptionHelper.Throw();
            return(null);
#else
            if (_KnownRenderers.Count == 0)
            {
                return(null);
            }

            return(GetRenderer(typeof(T)));
#endif
        }
Esempio n. 7
0
        /// <summary>
        /// Full constructor.
        /// </summary>
        /// <param name="serializer">A pre-configured <see cref="System.Xml.Serialization.XmlSerializer"/> used to serialised property values.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="serializer"/> is null.</exception>
        public XmlPropertyRenderer(System.Xml.Serialization.XmlSerializer serializer)
        {
#if CONTRACTS_ONLY
            BaitExceptionHelper.Throw();
#else
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            _Serialiser      = serializer;
            _EmptyNamespaces = new System.Xml.Serialization.XmlSerializerNamespaces();
            _EmptyNamespaces.Add(String.Empty, String.Empty);
#endif
        }
Esempio n. 8
0
        /// <summary>
        /// Removes the renderer for the given type from the map.
        /// </summary>
        /// <param name="type">The type the renderer was added for.</param>
        /// <returns>A boolean indicating if the item was removed (true), or false if it was not removed because a renderer was not found for that type.</returns>
        public bool RemoveRenderer(Type type)
        {
#if CONTRACTS_ONLY
            BaitExceptionHelper.Throw();
            return(false);
#else
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            IPropertyRenderer removedValue = null;
            var retVal = _KnownRenderers.TryRemove(type, out removedValue);
            _CachedRenderers.TryRemove(type, out removedValue);
            return(retVal);
#endif
        }
Esempio n. 9
0
        /// <summary>
        /// Full constructor.
        /// </summary>
        public TypeRendererMap(params KeyValuePair <Type, IPropertyRenderer>[] renderers)
        {
#if CONTRACTS_ONLY
            BaitExceptionHelper.Throw();
#else
            _KnownRenderers  = new System.Collections.Concurrent.ConcurrentDictionary <Type, IPropertyRenderer>();
            _CachedRenderers = new System.Collections.Concurrent.ConcurrentDictionary <Type, IPropertyRenderer>();

            if (renderers != null)
            {
                foreach (var kvp in renderers)
                {
                    _KnownRenderers.TryAdd(kvp.Key, kvp.Value);
                    _CachedRenderers.TryAdd(kvp.Key, kvp.Value);
                }
            }
#endif
        }
Esempio n. 10
0
        /// <summary>
        /// Adds a renderer for the given type to the map.
        /// </summary>
        /// <param name="type">The type that can be rendered by this renderer.</param>
        /// <param name="renderer">The renderer to add.</param>
        /// <returns>A boolean indicating if the item was added (true), or false if it was not added because a renderer is already loaded for this type.</returns>
        public bool AddRenderer(Type type, IPropertyRenderer renderer)
        {
#if CONTRACTS_ONLY
            BaitExceptionHelper.Throw();
            return(false);
#else
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (renderer == null)
            {
                throw new ArgumentNullException(nameof(renderer));
            }

            var retVal = _KnownRenderers.TryAdd(type, renderer);
            _CachedRenderers.TryAdd(type, renderer);
            return(retVal);
#endif
        }
Esempio n. 11
0
        /// <summary>
        /// Converts the provided object to an XML string.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <returns>A string containing an XML representation of <paramref name="value"/>.</returns>
        public object RenderValue(object value)
        {
#if CONTRACTS_ONLY
            BaitExceptionHelper.Throw();
            return(null);
#else
            if (value == null)
            {
                return(null);
            }

            using (var pooledWriter = Globals.TextWriterPool.Take())
            {
                _Serialiser.Serialize(pooledWriter.Value, value, _EmptyNamespaces);

                pooledWriter.Value.Flush();
                return(pooledWriter.Value.GetStringBuilder().ToString());
            }
#endif
        }
Esempio n. 12
0
        private static IList <System.Reflection.PropertyInfo> GetTypeProperties(Type type)
        {
#if CONTRACTS_ONLY
            BaitExceptionHelper.Throw();
            return(null);
#else
            IList <System.Reflection.PropertyInfo> retVal = null;

            if (!TypeProperties.TryGetValue(type, out retVal))
            {
                retVal = new List <PropertyInfo>(
                    (from pi
                     in type.GetProperties()
                     where pi.GetIndexParameters().Length <= 0
                     select pi)
                    );
                TypeProperties.TryAdd(type, retVal);
            }

            return(retVal);
#endif
        }