public Expression <TraceLogFilterPredicate> CreatePredicateExpr(TraceLogFilter filter) { var enabled = filter.Conditions.Where(x => x.IsEnabled).ToList(); var includedConds = enabled.Where(x => x.Action == FilterConditionAction.Include); var excludedConds = enabled.Where(x => x.Action == FilterConditionAction.Exclude); var includeExpr = ExpressionEx.OrElse(includedConds.Select(CreateComparisonExpr), true); var excludeExpr = ExpressionEx.OrElse(excludedConds.Select(CreateComparisonExpr), false); var filterExpr = Expression.AndAlso(includeExpr, Expression.Not(excludeExpr)); return(CreateLambda(filterExpr)); }
public TraceLogFilterPredicate CreatePredicate(TraceLogFilter filter) { // Lambdas compiled with LambdaExpression.Compile perform costly // security checks (SecurityTransparent, APTCA, class access checks // like SecurityCritical or SecuritySafeCritical, and LinkDemands), // see clr!JIT_MethodAccessCheck in vm/jithelpers.cpp. // The only way to avoid these seems to be to use a proper (dynamic) // assembly. We use a GC-collectable transient assembly to host the // delegate. var predicate = CreatePredicateExpr(filter).CompileToTransientAssembly(); // Force JIT-compiling the delegate, executing the pre-stub. // Otherwise calling the delegate involves calls to: // clr!PrecodeFixupThunk // clr!ThePreStub // clr!PreStubWorker // ... // This must be done twice, no idea exactly why. Warmup(predicate); Warmup(predicate); return(predicate); }