Exemple #1
0
        // ******************************************************************
        // *																*
        // *			               Public Methods		                *
        // *																*
        // ******************************************************************

        /// <summary>
        /// Formats the specified instance with the formatter that matches the specified name
        /// </summary>
        /// <param name="level">
        /// A LogLevel enum value
        /// </param>
        /// <param name="obj">
        /// A reference to the instance that must be formatted
        /// </param>
        /// <param name="formatterName">
        /// A string that specifies which formatter to use. If this argument equals <i>null</i>
        /// the default formatter will be used
        /// </param>
        /// <returns>
        /// A LogFormatterResult that holds the formatted result
        /// </returns>
        public virtual LogFormatterResult Format(
            LogLevel level,
            T obj,
            string formatterName = null)
        {
            lock (m_syncRoot)
            {
                try
                {
                    // Determine if we need to use default formatter or not
                    if (formatterName == null)
                    {
                        // Default formatting
                        return(DefaultFormatter.GetFormatter().Format(level, obj));
                    }
                    else
                    {
                        // Specific formatter
                        LogConfigFormatterType <T> formatterType;
                        if (TryGetFormatterType(formatterName, out formatterType))
                        {
                            return(formatterType.GetFormatter().Format(level, obj));
                        }
                    }

                    // If we reach this line, we could not resolve a formatter
                    return(new LogFormatterResult(new InvalidOperationException(string.Format(
                                                                                    "Log formatting failed; unable to format instance '{0}' because formatter '{1}' could not be found",
                                                                                    obj,
                                                                                    formatterName))));
                }
                catch (Exception ex)
                {
                    // Store exception into result
                    return(new LogFormatterResult(new InvalidOperationException(
                                                      "Log formatting failed; an exception occured during formatting", ex)));
                }
            }
        }