/// <summary>
 /// Resolves the specified tag.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="chunk">The chunk.</param>
 /// <returns>A <see cref="Resolution" />.</returns>
 public override object Resolve(FormatWriteContext context, FormatChunk chunk)
 {
     // ReSharper disable once PossibleNullReferenceException
     switch (chunk.Tag.ToLowerInvariant())
     {
         case "verbose":
             return VerboseFormat;
         case "reversed":
             return ReversedFormat;
         case "key":
             return Key;
         case "value":
             return Value;
         case "position":
             return new Resolution(context.Position, true);
         case "!fill":
             int length = Math.Min(
                 120,
                 context.Layout.Width.Value - context.Layout.FirstLineIndentSize.Value);
             char c = !string.IsNullOrEmpty(chunk.Format) ? chunk.Format[0] : '-';
             StringBuilder fill = new StringBuilder(length + context.Position < 1 ? 2 : 4);
             if (context.Position > 0) fill.AppendLine();
             fill.AppendLine(new string(c, length));
             // Note we have to replace control chunk with value chunk to write it out.
             return new Resolution(new FormatChunk(fill.ToString()), true);
         case "nest":
             string k;
             string v;
             if (string.IsNullOrWhiteSpace(chunk.Format))
             {
                 k = "UnspecKey";
                 v = "UnspecValue";
             }
             else
             {
                 string[] split = chunk.Format.Split(new[] { ',' }, 2, StringSplitOptions.RemoveEmptyEntries);
                 if (split.Length < 1)
                 {
                     k = "UnspecKey";
                     v = "UnspecValue";
                 }
                 else if (split.Length < 2)
                 {
                     k = split[0];
                     v = "UnspecValue";
                 }
                 else
                 {
                     k = split[0];
                     v = split[1];
                 }
             }
             return new RWTest(k, v);
         default:
             return Resolution.Unknown;
     }
 }
Example #2
0
        /// <summary>
        /// Resolves the specified tag.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="chunk">The chunk.</param>
        /// <returns>A <see cref="Resolution" />.</returns>
        // ReSharper disable once CodeAnnotationAnalyzer
        public override object Resolve(FormatWriteContext context, FormatChunk chunk)
        {
            TValue value;

            // ReSharper disable once AssignNullToNotNullAttribute
            return(_values.TryGetValue(chunk.Tag, out value)
                ? value
                : Resolution.Unknown);
        }
Example #3
0
        /// <summary>
        /// Resolves the specified tag.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="chunk">The chunk.</param>
        /// <returns>A <see cref="Resolution" />.</returns>
        // ReSharper disable once CodeAnnotationAnalyzer
        public override object Resolve(FormatWriteContext context, FormatChunk chunk)
        {
            int index;

            return(int.TryParse(chunk.Tag, out index) &&
                   index >= 0 &&
                   index < _values.Count
                ? _values[index]
                : Resolution.Unknown);
        }
            /// <summary>
            /// Resolves the specified tag.
            /// </summary>
            /// <param name="context">The context.</param>
            /// <param name="chunk">The chunk.</param>
            /// <returns>A <see cref="Resolution" />.</returns>
            // ReSharper disable once CodeAnnotationAnalyzer
            public override object Resolve(FormatWriteContext context, FormatChunk chunk)
            {
                Resolution resolution;
                // ReSharper disable once PossibleNullReferenceException
                string tag = chunk.Tag;

                Debug.Assert(tag != null);

                if (ResolveControls || !chunk.IsControl)
                {
                    if ((_values == null || !_values.TryGetValue(tag, out resolution)))
                    {
                        // Get the resolution using the resolver.
                        object result = _resolver(context, chunk);
                        resolution = result as Resolution? ?? new Resolution(result);

                        if (!resolution.NoCache)
                        {
                            // Cache the resolution.
                            if (_values == null)
                            {
                                _values =
                                    new Dictionary <string, Resolution>(
                                        IsCaseSensitive
                                            ? StringComparer.CurrentCulture
                                            : StringComparer.CurrentCultureIgnoreCase);
                            }

                            // ReSharper disable once AssignNullToNotNullAttribute
                            _values[tag] = resolution;
                        }
                    }
                }
                else
                {
                    resolution = (Resolution)Resolution.Unknown;
                }

                // If we don't have a resolution, ask the parent.
                if (!resolution.IsResolved &&
                    ResolveOuterTags &&
                    Parent != null)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    resolution = (Resolution)Parent.Resolve(context, chunk);
                }
                return(resolution);
            }
 /// <summary>
 /// Resolves the specified tag.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="chunk">The chunk.</param>
 /// <returns>An object that will be cached unless it is a <see cref="T:WebApplications.Utilities.Formatting.Resolution" />.</returns>
 // ReSharper disable once CodeAnnotationAnalyzer
 public override object Resolve(FormatWriteContext context, FormatChunk chunk)
 {
     // ReSharper disable once PossibleNullReferenceException
     switch (chunk.Tag.ToLowerInvariant())
     {
         case "default":
         case "verbose":
             return ElementVerboseFormat;
         case "xml":
             return ElementXMLFormat;
         case "json":
             return ElementJSONFormat;
         case "noline":
             return ElementNoLineFormat;
         case "key":
             return Key;
         case "keyxml":
             return Key.XmlEscape();
         case "keyxmltag":
             return Key.Replace(' ', '_');
         case "value":
             return Value;
         case "valuexml":
             return Value.XmlEscape();
         case "valuejson":
             return Value == null ? "null" : Value.ToJSON();
         default:
             return Resolution.Unknown;
     }
 }
 /// <summary>
 /// Resolves the specified tag.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="chunk">The chunk.</param>
 /// <returns>A <see cref="Resolution" />.</returns>
 // ReSharper disable once CodeAnnotationAnalyzer
 public override object Resolve(FormatWriteContext context, FormatChunk chunk) => _resolver(context, chunk);
 /// <summary>
 /// Resolves the specified tag.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="chunk">The chunk.</param>
 /// <returns>An object that will be cached unless it is a <see cref="T:WebApplications.Utilities.Formatting.Resolution" />.</returns>
 // ReSharper disable once CodeAnnotationAnalyzer
 public override object Resolve(FormatWriteContext context, FormatChunk chunk)
 {
     // ReSharper disable once PossibleNullReferenceException
     switch (chunk.Tag.ToLowerInvariant())
     {
         case "name":
             return Name;
         case "value":
             return Value;
         default:
             return Resolution.Unknown;
     }
 }
 /// <summary>
 /// Resolves the specified tag.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="chunk">The chunk.</param>
 /// <returns>
 /// An object that will be cached unless it is a <see cref="T:WebApplications.Utilities.Formatting.Resolution" />.
 /// </returns>
 public override object Resolve(FormatWriteContext context, FormatChunk chunk)
 {
     Debug.Assert(chunk.Tag != null);
     switch (chunk.Tag.ToLowerInvariant())
     {
         case "name":
             return Name;
         case "altnames":
             return _names.Length > 1
                 ? _names.Skip(1)
                 : Resolution.Null;
         case "description":
             return Description;
         case "parameters":
             object[] parameters = ArgumentParameters.Select(ResolveParameter).ToArray();
             return parameters.Length > 0 ? parameters : Resolution.Null;
         default:
             return Resolution.Unknown;
     }
 }
        /// <summary>
        /// Resolves the specified tag.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="chunk">The chunk.</param>
        /// <returns>An object that will be cached unless it is a <see cref="T:WebApplications.Utilities.Formatting.Resolution" />.</returns>
        // ReSharper disable once CodeAnnotationAnalyzer
        public override object Resolve(FormatWriteContext context, FormatChunk chunk)
        {
            if (chunk.Tag == null) throw new ArgumentException();

            switch (chunk.Tag.ToLowerInvariant())
            {
                case "isvalid":
                    return IsValid ? Resolution.Empty : Resolution.Null;
                case "guid":
                    return Guid;
                case "host":
                    return Host;
                case "name":
                    return Name;
                case "fullname":
                    return FullName;
                case "pipe":
                    return Pipe;
                default:
                    return Resolution.Unknown;
            }
        }
Example #10
0
 /// <summary>
 /// Resolves the specified tag.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="chunk">The chunk.</param>
 /// <returns>An object that will be cached unless it is a <see cref="Resolution" />.</returns>
 public abstract object Resolve(FormatWriteContext context, FormatChunk chunk);
Example #11
0
 /// <summary>
 /// Resolves the specified tag.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="chunk">The chunk.</param>
 /// <returns>An object that will be cached unless it is a <see cref="Resolution" />.</returns>
 public abstract object Resolve(FormatWriteContext context, FormatChunk chunk);
            /// <summary>
            /// Resolves the specified tag.
            /// </summary>
            /// <param name="context">The context.</param>
            /// <param name="chunk">The chunk.</param>
            /// <returns>A <see cref="Resolution" />.</returns>
            // ReSharper disable once CodeAnnotationAnalyzer
            public override object Resolve(FormatWriteContext context, FormatChunk chunk)
            {
                Resolution resolution;
                // ReSharper disable once PossibleNullReferenceException
                string tag = chunk.Tag;
                Debug.Assert(tag != null);

                if (ResolveControls || !chunk.IsControl)
                {
                    if ((_values == null || !_values.TryGetValue(tag, out resolution)))
                    {
                        // Get the resolution using the resolver.
                        object result = _resolver(context, chunk);
                        resolution = result as Resolution? ?? new Resolution(result);

                        if (!resolution.NoCache)
                        {
                            // Cache the resolution.
                            if (_values == null)
                                _values =
                                    new Dictionary<string, Resolution>(
                                        IsCaseSensitive
                                            ? StringComparer.CurrentCulture
                                            : StringComparer.CurrentCultureIgnoreCase);

                            // ReSharper disable once AssignNullToNotNullAttribute
                            _values[tag] = resolution;
                        }
                    }
                }
                else
                    resolution = (Resolution)Resolution.Unknown;

                // If we don't have a resolution, ask the parent.
                if (!resolution.IsResolved &&
                    ResolveOuterTags &&
                    Parent != null)
                    // ReSharper disable once PossibleNullReferenceException
                    resolution = (Resolution)Parent.Resolve(context, chunk);
                return resolution;
            }
 /// <summary>
 /// Resolves the specified tag.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="chunk">The chunk.</param>
 /// <returns>An object that will be cached unless it is a <see cref="T:WebApplications.Utilities.Formatting.Resolution" />.</returns>
 public override object Resolve(FormatWriteContext context, FormatChunk chunk)
 {
     string key;
     // ReSharper disable once PossibleNullReferenceException
     switch (chunk.Tag.ToLowerInvariant())
     {
         case "default":
         case "verbose":
             return EnumerableElementVerboseFormat;
         case "xml":
             return EnumerableElementXMLFormat;
         case "json":
             return EnumerableElementJSONFormat;
         case "noline":
             return EnumerableElementNoLineFormat;
         case "key":
             key = Translation.GetResource(
                 Resource,
                 context.FormatProvider as CultureInfo ?? Translation.DefaultCulture);
             return string.IsNullOrEmpty(key)
                 ? Resolution.Null
                 : key;
         case "keyxml":
             key = Translation.GetResource(
                 Resource,
                 context.FormatProvider as CultureInfo ?? Translation.DefaultCulture);
             return string.IsNullOrEmpty(key)
                 ? Resolution.Null
                 : key.XmlEscape();
         case "keyxmltag":
             key = Translation.GetResource(
                 Resource,
                 context.FormatProvider as CultureInfo ?? Translation.DefaultCulture);
             return string.IsNullOrEmpty(key)
                 ? Resolution.Null
                 : key.Replace(' ', '_');
         case "value":
             return Values;
         case "valuexml":
             return Values.Select(v => v != null ? v.XmlEscape() : null);
         default:
             return Resolution.Unknown;
     }
 }
Example #14
0
        /// <summary>
        /// Resolves the specified tag.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="chunk">The chunk.</param>
        /// <returns>An object that will be cached unless it is a <see cref="T:WebApplications.Utilities.Formatting.Resolution" />.</returns>
        // ReSharper disable once CodeAnnotationAnalyzer
        public override object Resolve(FormatWriteContext context, FormatChunk chunk)
        {
            CultureInfo culture = context.FormatProvider as CultureInfo ?? Translation.DefaultCulture;
            // ReSharper disable once PossibleNullReferenceException
            switch (chunk.Tag.ToLowerInvariant())
            {
                /*
                 * Standard named formats.
                 */
                case "default":
                case "verbose":
                    return VerboseFormat;
                case "short":
                    return ShortFormat;
                case "all":
                    return AllFormat;
                case "json":
                    return JSONFormat;
                case "xml":
                    return XMLFormat;

                /* 
                 * Control Tags.
                 */
                case FormatBuilder.ForegroundColorTag:
                case FormatBuilder.BackgroundColorTag:
                    // Replace the LogLevel colour with this colour.
                    return string.Equals(
                        chunk.Format,
                        LogLevelColorName,
                        StringComparison.InvariantCultureIgnoreCase)
                        // Don't cache the response as it is format dependant.
                        ? new Resolution(_level.ToColor(), true)
                        : Resolution.UnknownYet;

                /*
                 * Log element completion.
                 */
                case FormatTagHeader:
                    // Create a header based on the format pattern.
                    // Get the width of the writer, for creating headers.
                    int width = Math.Min(256, context.Layout.Width.Value - context.Layout.FirstLineIndentSize.Value);

                    string pattern = chunk.Format;
                    if (string.IsNullOrEmpty(pattern))
                        pattern = "=";

                    bool startNewLine = context.Position > 0;
                    char[] header = new char[width + (startNewLine ? 2 : 1)];
                    int c = 0;
                    if (startNewLine)
                        header[c++] = '\r'; // Formatter normalizes '\r' to newline automatically.
                    for (int p = 0; p < width; p++)
                        header[c++] = pattern[p % pattern.Length];
                    header[c] = '\r';

                    // We have to replace the original chunk with a non-control chunk for it to to be output.
                    // Set NoCache to true to support format changes.
                    return new Resolution(new FormatChunk(new string(header)), true);

                case FormatTagMessage:
                    return new LogElement(
                        () => Resources.LogKeys_Message,
                        GetMessage(culture));

                case FormatTagResource:
                    return string.IsNullOrWhiteSpace(_resourceProperty)
                        ? Resolution.Null
                        : new LogElement(
                            () => Resources.LogKeys_Resource,
                            _resourceProperty);

                case FormatTagCulture:
                    return new LogElement(
                        () => Resources.LogKeys_Culture,
                        culture);

                case FormatTagTimeStamp:
                    return new LogElement(
                        () => Resources.LogKeys_TimeStamp,
                        ((CombGuid)_guid).Created);

                case FormatTagLevel:
                    return new LogElement(
                        () => Resources.LogKeys_Level,
                        _level);

                case FormatTagGuid:
                    return new LogElement(
                        () => Resources.LogKeys_Guid,
                        _guid);

                case FormatTagException:
                    return string.IsNullOrWhiteSpace(_exceptionType)
                        ? Resolution.Null
                        : new LogElement(
                            () => Resources.LogKeys_Exception,
                            _exceptionType);

                case FormatTagThreadID:
                    return _threadID < 0
                        ? Resolution.Null
                        : new LogElement(
                            () => Resources.LogKeys_ThreadID,
                            _threadID);

                case FormatTagThreadName:
                    return string.IsNullOrWhiteSpace(_threadName)
                        ? Resolution.Null
                        : new LogElement(
                            () => Resources.LogKeys_ThreadName,
                            _threadName);

                case FormatTagApplicationName:
                    return string.IsNullOrWhiteSpace(ApplicationName)
                        ? Resolution.Null
                        : new LogElement(
                            () => Resources.LogKeys_ApplicationName,
                            ApplicationName);

                case FormatTagApplicationGuid:
                    return new LogElement(
                        () => Resources.LogKeys_ApplicationGuid,
                        ApplicationGuid);

                case FormatTagInnerException:
                    if ((_innerExceptionGuids == null) ||
                        (_innerExceptionGuids.Length < 1)) return Resolution.Null;
                    return new LogEnumerableElement(
                        () => Resources.LogKeys_InnerException,
                        _innerExceptionGuids.Cast<object>());

                case FormatTagStoredProcedure:
                    // TODO This can be a specialized LogElement!
                    return string.IsNullOrWhiteSpace(_storedProcedure)
                        ? Resolution.Null
                        : new LogElement(
                            () => Resources.LogKeys_StoredProcedure,
                            _storedProcedure + " at line " + _storedProcedureLine.ToString("D"));

                case FormatTagContext:
                    return (_context == null) ||
                           (_context.Count < 1)
                        ? Resolution.Null
                        : _context;

                case FormatTagStackTrace:
                    return string.IsNullOrWhiteSpace(_stackTrace)
                        ? Resolution.Null
                        : new LogElement(
                            () => Resources.LogKeys_StackTrace,
                            _stackTrace);

                default:
                    return Resolution.Unknown;
            }
        }
Example #15
0
 /// <summary>
 /// Resolves the specified tag.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="chunk">The chunk.</param>
 /// <returns>A <see cref="Resolution" />.</returns>
 // ReSharper disable once CodeAnnotationAnalyzer
 public override object Resolve(FormatWriteContext context, FormatChunk chunk) => _resolver(context, chunk);
Example #16
0
 /// <summary>
 /// Resolves the specified tag.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="chunk">The chunk.</param>
 /// <returns>An object that will be cached unless it is a <see cref="T:WebApplications.Utilities.Formatting.Resolution" />.</returns>
 // ReSharper disable once CodeAnnotationAnalyzer
 public override object Resolve(FormatWriteContext context, FormatChunk chunk)
 {
     // ReSharper disable once PossibleNullReferenceException
     switch (chunk.Tag.ToLowerInvariant())
     {
         case "default":
         case "verbose":
             return ElementVerboseFormat;
         case "xml":
             return ElementXMLFormat;
         case "json":
             return ElementJSONFormat;
         case "jsonfirst":
             return ElementJSONFirstFormat;
         case "noline":
             return ElementNoLineFormat;
         case "key":
             string key = Translation.GetResource(
                 Resource,
                 context.FormatProvider as CultureInfo ?? Translation.DefaultCulture);
             return string.IsNullOrEmpty(key)
                 ? Resolution.Null
                 : key;
         case "keyxml":
             string keyXml = Translation.GetResource(
                 Resource,
                 context.FormatProvider as CultureInfo ?? Translation.DefaultCulture);
             return string.IsNullOrEmpty(keyXml)
                 ? Resolution.Null
                 : keyXml.XmlEscape();
         case "keyxmltag":
             string keyXmlTag = Translation.GetResource(
                 Resource,
                 context.FormatProvider as CultureInfo ?? Translation.DefaultCulture);
             return string.IsNullOrEmpty(keyXmlTag)
                 ? Resolution.Null
                 : keyXmlTag.Replace(' ', '_');
         case "value":
             return Value;
         case "valuexml":
             return Value == null ? Resolution.Null : Value.XmlEscape();
         case "valuejson":
             return Value == null ? "null" : Value.ToString().ToJSON();
         default:
             return Resolution.Unknown;
     }
 }