예제 #1
0
        /// <summary>
        /// Parse supplied template string and creates the chain of converters using specified 'factory'
        /// </summary>
        /// <param name="template">Template string</param>
        /// <param name="factory">Factory to create converters</param>
        /// <returns>Final combined converter</returns>
        /// <exception cref="LoggerMessageTemplateParsingException">Exception for any parsing error</exception>
        public static LoggingEventConverterBase Parse(string template, ConverterFactory factory)
        {
            Contract.Requires(template != null);
            Contract.Requires(factory != null);
            Contract.Ensures(Contract.Result <LoggingEventConverterBase>() != null);

            if (template == "")
            {
                return(factory.CreateConstConverter(""));
            }


            char[] stopSymbols = { '{' };
            var    converters  = new List <LoggingEventConverterBase>();

            int curPos = 0;

            while (curPos < template.Length)
            {
                if (template[curPos] == '{')
                {
                    curPos++;
                    var curConv = ParseConverter(template, ref curPos, factory);
                    converters.Add(curConv);
                }
                else
                {
                    string str = ReadString(template, ref curPos, stopSymbols);
                    converters.Add(factory.CreateConstConverter(str));
                }
            }


            if (converters.Count == 1)
            {
                return(converters[0]);
            }

            return(new LoggingEventConverter(converters));
        }
예제 #2
0
        private static LoggingEventConverterBase CreateConverter(string key, ParsedConverterParams parsedParams, ConverterFactory factory)
        {
            Contract.Requires(key != null);
            Contract.Requires(parsedParams != null);
            Contract.Requires(factory != null);

            ConverterTypes type;

            if (!Substitutions.TryGetValue(key.ToLowerInvariant(), out type))
            {
                throw new LoggerMessageTemplateParsingException("Incorrect or unsupported token name: " + key);
            }

            switch (type)
            {
            case ConverterTypes.LevelConverter:
                return(factory.CreateLevelConverter());

            case ConverterTypes.MachineNameConverter:
                return(WrapByParsedParams(factory.CreateMachineNameConverter(), parsedParams, "??"));

            case ConverterTypes.MachineIpAddressConverter:
                return(WrapByParsedParams(factory.CreateMachineIpAddressConverter(), parsedParams, "??"));

            case ConverterTypes.ProcessNameConverter:
                return(WrapByParsedParams(factory.CreateProcessNameConverter(), parsedParams, "??"));

            case ConverterTypes.ProcessIdConverter:
                return(WrapByParsedParams(factory.CreateProcessIdConverter(), parsedParams, "??"));

            case ConverterTypes.AssemblyConverter:
                return(WrapByParsedParams(factory.CreateAssemblyConverter(), parsedParams, "<no asm>"));

            case ConverterTypes.NamespaceConverter:
                return(WrapByParsedParams(factory.CreateNamespaceConverter(), parsedParams, "<no ns>"));

            case ConverterTypes.ClassConverter:
                return(WrapByParsedParams(factory.CreateClassConverter(), parsedParams, "<no class>"));

            case ConverterTypes.MethodConverter:
                return(WrapByParsedParams(factory.CreateMethodConverter(), parsedParams, "<no method>"));

            case ConverterTypes.MessageConverter:
                return(WrapByParsedParams(factory.CreateMessageConverter(), parsedParams, "<no msg>"));

            case ConverterTypes.ContextConverter:
                return(WrapByParsedParams(factory.CreateContextConverter(), parsedParams, null));

            case ConverterTypes.ExceptionConverter:
                return(WrapByParsedParams(factory.CreateExceptionConverter(), parsedParams, "<no exception>"));

            case ConverterTypes.StackSourceConverter:
                return(WrapByParsedParams(factory.CreateStackSourceConverter(), parsedParams, "??"));

            case ConverterTypes.StackSourceHeadConverter:
                return(WrapByParsedParams(factory.CreateStackSourceHeadConverter(), parsedParams, null));

            case ConverterTypes.StackSourceTailConverter:
                return(WrapByParsedParams(factory.CreateStackSourceTailConverter(), parsedParams, null));

            case ConverterTypes.DateConverter:
                return(factory.CreateDateConverter(parsedParams.Format));

            default:
                throw new ArgumentException(string.Format("Unknown ConverterTypes value: \"{0}\"", type.ToString()));
            }
        }
예제 #3
0
        private static LoggingEventConverterBase ParseConverter(string data, ref int curPos, ConverterFactory factory)
        {
            int startPos = curPos;

            if (startPos > data.Length)
            {
                startPos = data.Length;
            }

            SkipSpaces(data, ref curPos);
            if (curPos >= data.Length)
            {
                throw new LoggerMessageTemplateParsingException("Unexpected end of string. Substitution token end was not found. String: '" + data + "', Position: " + curPos.ToString());
            }


            string key = ReadTerm(data, ref curPos);

            SkipSpaces(data, ref curPos);

            if (curPos >= data.Length)
            {
                throw new LoggerMessageTemplateParsingException("Unexpected end of string. Closing brace for substitution token not found ('}'). Token: '" + data.Substring(startPos) + "', String: '" + data + "', Postion: " + curPos.ToString());
            }

            ParsedConverterParams parsedParams = new ParsedConverterParams();

            while (curPos < data.Length)
            {
                switch (data[curPos])
                {
                case '}':
                    curPos++;
                    return(CreateConverter(key, parsedParams, factory));

                case ',':
                    curPos++;
                    var keyValue = ParseKeyValue(data, ref curPos);
                    FillParsedConverterParams(parsedParams, keyValue, data.Substring(startPos));
                    break;

                default:
                    throw new LoggerMessageTemplateParsingException("Incorrect symbol during parsing token parameters ('" + data[curPos].ToString() + "'). '}' or ',' expected. Token: '" + data.Substring(startPos) + "', String: '" + data + "', Position: " + curPos.ToString());
                }
                SkipSpaces(data, ref curPos);
            }

            throw new LoggerMessageTemplateParsingException("Unexpected end of string. Closing brace for substitution token not found ('}'). Token: '" + data.Substring(startPos) + "', String: '" + data + "', Postion: " + curPos.ToString());
        }