Example #1
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
        }
Example #2
0
        /// <summary>
        /// Full constructor.
        /// </summary>
        /// <param name="policy">A <see cref="LogPolicy"/> instance used to configure this logger.</param>
        /// <remarks>
        /// <para>The <paramref name="policy"/> should not be changed after being provided to the logger. Values from the policy are cached or copied before use and will not change even if the policy is updated after the logger is constructed.</para>
        /// </remarks>
        public Logger(LogPolicy policy)
        {
            if (policy == null)
            {
                throw new ArgumentNullException(nameof(policy));
            }
            if (policy.LogWriter == null)
            {
                throw new ArgumentException(String.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.PropertyCannotBeNull, "policy.LogWriter"), nameof(policy));
            }

            _ErrorHandler = policy.ErrorHandler ?? SuppressingErrorHandler.DefaultInstance;
            _EntryPool    = new LogEventPool(policy.LogEventPoolCapacity);
            _JobPool      = new LoggedJobPool(policy.JobPoolCapacity);

            _LogWriter                = policy.LogWriter;
            _LogClock                 = policy.Clock ?? new CachingClock(new LocalSystemClock(), TimeSpan.FromTicks(16));
            _Filter                   = policy.Filter;
            _FirstChanceFilter        = policy.FirstChanceFilter;
            _RendererMap              = policy.TypeRendererMap;
            _Source                   = policy.Source;
            _DefaultExceptionRenderer = policy.DefaultExceptionRenderer;

            if (policy.ContextProviders != null)
            {
                _ContextProviders = (policy.ContextProviders as ILogEventContextProvider[]) ?? policy.ContextProviders.ToArray();
            }

            _IsEnabled = true;
        }
Example #3
0
        private void RenderProperties(KeyValuePair <string, object>[] properties, LogEvent logEvent)
        {
            if (Utils.Any <KeyValuePair <string, object> >(properties))
            {
                IPropertyRenderer renderer = null;
                bool hasRenderers          = _RendererMap != null;
                var  eventProperties       = (logEvent.Properties = logEvent.Properties ?? new Dictionary <string, object>((properties?.Length ?? 1) + (_ContextProviders?.Length ?? 0)));
                foreach (var property in properties)
                {
                    renderer = null;
                    if (property.Value != null)
                    {
                        if (hasRenderers)
                        {
                            renderer = _RendererMap.GetRenderer(property.Value.GetType());
                        }
                        else if (_DefaultExceptionRenderer != null && property.Value is Exception)
                        {
                            renderer = _DefaultExceptionRenderer;
                        }
                    }

                    if (renderer != null)
                    {
                        eventProperties[property.Key] = renderer.RenderValue(property.Value);
                    }
                    else
                    {
                        eventProperties[property.Key] = property.Value;
                    }
                }
            }
        }
Example #4
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
        }
Example #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ColumnContext"/> class.
 /// </summary>
 /// <param name="sheetContext">Owner sheet context.</param>
 /// <param name="columnMetadata">Column metadata.</param>
 /// <param name="propertyRenderer"><see cref="IPropertyRenderer"/> to render column cells.</param>
 public ColumnContext(
     SheetContext sheetContext,
     IExcelMetadata columnMetadata,
     IPropertyRenderer propertyRenderer)
 {
     SheetContext     = sheetContext.AssertArgumentNotNull(nameof(sheetContext));
     ColumnMetadata   = columnMetadata.AssertArgumentNotNull(nameof(columnMetadata));
     PropertyRenderer = propertyRenderer.AssertArgumentNotNull(nameof(propertyRenderer));
 }
Example #6
0
        public void RegisterPropertyRenderer(IPropertyRenderer renderer)
        {
            var existing = this.propertyRenderers.FindIndex(x => x.Name == renderer.Name);

            if (existing >= 0)
            {
                propertyRenderers.RemoveAt(existing);
            }
            propertyRenderers.Add(renderer);
        }
Example #7
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
        }
        /// <summary>
        /// Executes <paramref name="configure"/> and returns the renderer of the same input type.
        /// <paramref name="configure"/> should return the same renderer or method will throw exception.
        /// </summary>
        /// <typeparam name="TPropertyRenderer">PropertyRenderer type.</typeparam>
        /// <param name="renderer">Source renderer.</param>
        /// <param name="configure">Configure action.</param>
        /// <returns>The same renderer of the same input type.</returns>
        public static TPropertyRenderer ConfigureTyped <TPropertyRenderer>(
            this TPropertyRenderer renderer,
            Func <TPropertyRenderer, IPropertyRenderer> configure)
            where TPropertyRenderer : IPropertyRenderer
        {
            IPropertyRenderer propertyRenderer = configure(renderer);

            if (!ReferenceEquals(propertyRenderer, renderer))
            {
                throw new InvalidOperationException("Configure action should return the same renderer instance");
            }
            return(renderer);
        }
 /// <summary>
 /// Creates renderers by properties.
 /// </summary>
 /// <param name="propertySet">Properties.</param>
 /// <param name="configureRenderer">Configure created renderer.</param>
 /// <returns>Renderers.</returns>
 public static IEnumerable <IPropertyRenderer> ToRenderers(
     this IEnumerable <IProperty> propertySet,
     Action <PropertyRendererOptions>?configureRenderer = null)
 {
     foreach (IProperty property in propertySet)
     {
         IPropertyRenderer propertyRenderer = property.ToRendererUntyped();
         if (configureRenderer != null)
         {
             propertyRenderer = propertyRenderer.ConfigureRenderer(configureRenderer);
         }
         yield return(propertyRenderer);
     }
 }
Example #10
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
        }
Example #11
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
        }
Example #12
0
        protected static void AddProperty(IDictionary <string, object> properties, string propertyName, object value, ITypeRendererMap rendererMap)
        {
            if (!properties.ContainsKey(propertyName))
            {
                IPropertyRenderer renderer = null;

                if (rendererMap != null && value != null)
                {
                    renderer = rendererMap.GetRenderer(value.GetType());
                }

                if (renderer != null)
                {
                    properties[propertyName] = renderer.RenderValue(value);
                }
                else
                {
                    properties[propertyName] = value;
                }
            }
        }
 /// <summary>
 /// Sets <see cref="IPropertyRenderer.TargetName"/>.
 /// </summary>
 /// <param name="renderer">Source renderer.</param>
 /// <param name="targetName">Target name.</param>
 /// <returns>The same renderer for chaining.</returns>
 public static IPropertyRenderer SetTargetName(this IPropertyRenderer renderer, string targetName)
 {
     return(renderer.ConfigureRenderer(options => options.TargetName = targetName));
 }
 /// <summary>
 /// Casts typed <see cref="IPropertyRenderer{T}"/> to untyped <see cref="IPropertyRenderer"/>.
 /// </summary>
 /// <typeparam name="T">Property type.</typeparam>
 /// <param name="renderer">Source renderer.</param>
 /// <returns>The same renderer casted to untyped form.</returns>
 public static IPropertyRenderer AsUntyped <T>(this IPropertyRenderer <T> renderer) => renderer;
 /// <summary>
 /// Sets render for <see cref="IFormattable"/> objects with text format.
 /// Method creates custom rendering for <see cref="IPropertyRenderer"/>.
 /// </summary>
 /// <param name="renderer">Source renderer.</param>
 /// <param name="format">Text format.</param>
 /// <param name="formatProvider">Optional <see cref="IFormatProvider"/>.</param>
 /// <returns>The same renderer for chaining.</returns>
 public static IPropertyRenderer SetFormat(this IPropertyRenderer renderer, string format, IFormatProvider?formatProvider = null)
 {
     return(renderer.ConfigureRenderer(options =>
     {
         options.CustomRender = (property, container) => RenderAsFormattable(property, container, options, format, formatProvider);
     }));
 /// <summary>
 /// Sets <see cref="SearchOptions"/> for <paramref name="renderer"/>.
 /// </summary>
 /// <param name="renderer">Source renderer.</param>
 /// <param name="searchOptions"><see cref="SearchOptions"/> for property search.</param>
 /// <returns>The same renderer for chaining.</returns>
 public static IPropertyRenderer SetSearchOptions(this IPropertyRenderer renderer, SearchOptions searchOptions)
 {
     return(renderer.ConfigureRenderer(options => options.SearchOptions = searchOptions));
 }
Example #17
0
        private string ConstructCell(IPropertyRenderer propertyRenderer, IPropertyContainer source)
        {
            string textValue = propertyRenderer.Render(source);

            return(textValue);
        }
 /// <summary>
 /// Adds property renderer.
 /// </summary>
 /// <typeparam name="T">Property type.</typeparam>
 /// <param name="renderer">Renderer to add.</param>
 /// <returns>The same provider instance for chaining.</returns>
 public ReportProvider AddRenderer(IPropertyRenderer renderer)
 {
     _renderers.Add(renderer.AssertArgumentNotNull(nameof(renderer)));
     return(this);
 }