Beispiel #1
0
        public override Func <LogRecord, string> BuildLambda()
        {
            OutputTemplate = OutputTemplate ?? DefaultOutputTemplate;
            Formats        = Formats ?? new RecordFormatStore();
            Colors         = Colors ?? new RecordConsoleColorStore();
            PropertyFilter = PropertyFilter ?? (p => true);

            var consoleType  = typeof(Console);
            var instanceExpr = Expression.Parameter(typeof(LogRecord), "instance");
            var blocks       = new List <Expression>();
            var strVars      = new List <ParameterExpression>();

            if (OutputTemplate.IsEmpty())
            {
                blocks.Add(
                    Expression.Assign(
                        Expression.Property(null, consoleType, nameof(Console.ForegroundColor)),
                        Expression.Constant(ConsoleColor.White)
                        )
                    );

                var valExpr = Expression.Call(instanceExpr, nameof(ToString), null);

                blocks.Add(
                    Expression.Call(consoleType.GetMethod(nameof(Console.Write), new[] { typeof(string) }), valExpr)
                    );
            }
            else
            {
                var tokens        = parser.Parse(OutputTemplate);
                var propertyInfo  = default(PropertyInfo);
                var filteredProps = props.Where(PropertyFilter)
                                    .ToArray();

                for (int i = 0; i < tokens.Length; i++)
                {
                    var t         = tokens[i];
                    var valExpr   = default(Expression);
                    var colorExpr = default(Expression);

                    if (t.IsLiteral)
                    {
                        valExpr = Expression.Constant(t.Value);
                    }
                    else
                    {
                        if (Formats.ContainsKey(t.Value))
                        {
                            var callFormatExpr = Expression.Constant(Formats[t.Value]);

                            valExpr = Expression.Invoke(callFormatExpr, instanceExpr);
                        }
                        else if ((propertyInfo = filteredProps.FirstOrDefault(x => x.Name.Equals(t.Value, StringComparison.OrdinalIgnoreCase))) != null)
                        {
                            var msgPropExpr = Expression.Property(instanceExpr, propertyInfo);

                            valExpr = propertyInfo.PropertyType == typeof(string)
                                       ? (Expression)msgPropExpr
                                       : Expression.Call(msgPropExpr, nameof(object.ToString), null);
                        }
                        else
                        {
                            valExpr = Expression.Constant(t.Key);
                        }

                        if (Colors.ContainsKey(t.Value))
                        {
                            var callTokenColor = Expression.Constant(Colors[t.Value]);

                            colorExpr = Expression.Invoke(callTokenColor, instanceExpr);
                        }
                    }

                    colorExpr = colorExpr ?? Expression.Constant(ConsoleColor.White);

                    blocks.Add(
                        Expression.Assign(
                            Expression.Property(null, consoleType, nameof(Console.ForegroundColor)),
                            colorExpr
                            )
                        );

                    var valVar = Expression.Parameter(typeof(string), $"tokenVar{i}");

                    strVars.Add(valVar);

                    blocks.Add(
                        Expression.Assign(valVar, valExpr)
                        );

                    blocks.Add(
                        Expression.Call(consoleType.GetMethod(nameof(Console.Write), new[] { typeof(string) }), valVar)
                        );
                }
            }

            blocks.Add(
                Expression.Call(consoleType, nameof(Console.ResetColor), null)
                );

            blocks.Add(
                Expression.Call(consoleType, nameof(Console.WriteLine), null)
                );

            blocks.Add(
                ExpressionsHelper.BuildStringConcat(strVars.ToArray())
                );

            var body           = Expression.Block(strVars, blocks);
            var lambda         = Expression.Lambda <Func <LogRecord, string> >(body, instanceExpr);
            var serializerFunc = lambda.CompileFast();

            return(serializerFunc);
        }
Beispiel #2
0
        public virtual Func <TObj, string> BuildLambda()
        {
            Formats        = Formats ?? new TFormatStore();
            PropertyFilter = PropertyFilter ?? (p => true);

            var instanceExpr = Expression.Parameter(typeof(TObj), "instance");
            var strExprs     = new List <Expression>();

            if (OutputTemplate.IsEmpty())
            {
                strExprs.Add(
                    Expression.Call(instanceExpr, nameof(ToString), null)
                    );
            }
            else
            {
                var filteredProps = props.Where(PropertyFilter)
                                    .ToArray();

                var tokens = parser.Parse(OutputTemplate);

                foreach (var t in tokens)
                {
                    var valExpr      = default(Expression);
                    var propertyInfo = default(PropertyInfo);

                    if (t.IsLiteral)
                    {
                        valExpr = Expression.Constant(t.Value);
                    }
                    else
                    {
                        if (Formats.ContainsKey(t.Value))
                        {
                            var callFormatExpr = Expression.Constant(Formats[t.Value]);

                            valExpr = Expression.Invoke(callFormatExpr, instanceExpr);
                        }
                        else if ((propertyInfo = filteredProps.FirstOrDefault(x => x.Name.Equals(t.Value, StringComparison.OrdinalIgnoreCase))) != null)
                        {
                            var propertyExpr = Expression.Property(instanceExpr, propertyInfo);

                            valExpr = propertyInfo.PropertyType == typeof(string)
                                ? (Expression)propertyExpr
                                : Expression.Call(propertyExpr, nameof(ToString), null);
                        }
                        else
                        {
                            valExpr = Expression.Constant(t.Key);
                        }
                    }

                    strExprs.Add(valExpr);
                }
            }

            var body           = ExpressionWizard.Spell.StringConcat(strExprs.ToArray());
            var lambda         = Expression.Lambda <Func <TObj, string> >(body, instanceExpr);
            var serializerFunc = lambda.CompileFast();

            return(serializerFunc);
        }