예제 #1
0
        internal MethodWeaverBase(TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider,
                                  ILoggerProvider loggerProvider, MethodDefinition methodDefinition)
        {
            _typeReferenceProvider   = typeReferenceProvider;
            _methodReferenceProvider = methodReferenceProvider;
            _methodDefinition        = methodDefinition;
            _body                 = methodDefinition.Body;
            _loggerProvider       = loggerProvider;
            _isEmptyBody          = (_body.Instructions.Count == 0);
            _firstRealInstruction = _body.Instructions.FirstOrDefault();

            if (_body.Method.IsConstructor && !_body.Method.IsStatic)
            {
                var constructorCall = _body.Instructions.FirstOrDefault(IsConstructorCall);
                if (constructorCall != null)
                {
                    var cctrCallIndex = _body.Instructions.IndexOf(constructorCall);
                    if (cctrCallIndex == _body.Instructions.Count - 1)
                    {
                        _isEmptyBody = true;
                    }
                    else
                    {
                        _firstRealInstruction = _body.Instructions[cctrCallIndex + 1];
                    }
                }
            }

            HasNoTraceOnReturnValue = _methodDefinition.CustomAttributes.Any(attr =>
                                                                             attr.AttributeType.FullName.Equals("TracerAttributes.NoReturnTrace", StringComparison.Ordinal));
        }
예제 #2
0
        internal MethodWeaverBase(TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider,
                                  ILoggerProvider loggerProvider, MethodDefinition methodDefinition)
        {
            _typeReferenceProvider   = typeReferenceProvider;
            _methodReferenceProvider = methodReferenceProvider;
            _methodDefinition        = methodDefinition;
            _body                 = methodDefinition.Body;
            _loggerProvider       = loggerProvider;
            _isEmptyBody          = (_body.Instructions.Count == 0);
            _firstRealInstruction = _body.Instructions.FirstOrDefault();

            if (_body.Method.IsConstructor && !_body.Method.IsStatic)
            {
                var constructorCall = _body.Instructions.FirstOrDefault(IsConstructorCall);
                if (constructorCall != null)
                {
                    var cctrCallIndex = _body.Instructions.IndexOf(constructorCall);
                    if (cctrCallIndex == _body.Instructions.Count - 1)
                    {
                        _isEmptyBody = true;
                    }
                    else
                    {
                        _firstRealInstruction = _body.Instructions[cctrCallIndex + 1];
                    }
                }
            }
        }
예제 #3
0
 public MethodWeaverFactory(TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider,
                            MethodWeaverBase.ILoggerProvider loggerProvider)
 {
     _typeReferenceProvider   = typeReferenceProvider;
     _methodReferenceProvider = methodReferenceProvider;
     _loggerProvider          = loggerProvider;
 }
예제 #4
0
 public MethodWeaverFactory(TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider,
     MethodWeaver.ILoggerProvider loggerProvider)
 {
     _typeReferenceProvider = typeReferenceProvider;
     _methodReferenceProvider = methodReferenceProvider;
     _loggerProvider = loggerProvider;
 }
예제 #5
0
        internal static FieldReference CreateLoggerStaticField(TypeReferenceProvider typeReferenceProvider,
                                                               MethodReferenceProvider methodReferenceProvider,
                                                               TypeDefinition typeDefinition)
        {
            var logTypeRef        = typeReferenceProvider.LogAdapterReference;
            var logManagerTypeRef = typeReferenceProvider.LogManagerReference;

            //look for existing one
            var loggerField = typeDefinition.Fields.FirstOrDefault(fld => fld.IsStatic &&
                                                                   fld.FieldType.FullName.Equals(logTypeRef.FullName));

            if (loggerField != null)
            {
                return(loggerField.FixFieldReferenceIfDeclaringTypeIsGeneric());
            }

            //$log should be unique
            loggerField = new FieldDefinition("$log", FieldAttributes.Private | FieldAttributes.Static, logTypeRef);
            typeDefinition.Fields.Add(loggerField);

            //create field init
            var staticConstructor = typeDefinition.GetStaticConstructor();

            if (staticConstructor == null)
            {
                const MethodAttributes methodAttributes = MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
                staticConstructor = new MethodDefinition(".cctor", methodAttributes, typeReferenceProvider.Void);
                typeDefinition.Methods.Add(staticConstructor);
                staticConstructor.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
            }

            var getLoggerMethod = new MethodReference("GetLogger", logTypeRef, logManagerTypeRef);

            getLoggerMethod.Parameters.Add(new ParameterDefinition(typeReferenceProvider.Type));

            //build up typeInfo
            var getTypeFromHandleMethod = methodReferenceProvider.GetGetTypeFromHandleReference();

            //spec treatment for generic types
            var loggerFieldRef = loggerField.FixFieldReferenceIfDeclaringTypeIsGeneric();

            //if generated nested type use the declaring type as logger type as it is more natural from
            //end users perspective
            var hasCompilerGeneratedAttribute = typeDefinition.HasCustomAttributes && typeDefinition.CustomAttributes
                                                .Any(attr => attr.AttributeType.FullName.Equals(typeof(CompilerGeneratedAttribute).FullName, StringComparison.Ordinal));

            var loggerTypeDefinition = hasCompilerGeneratedAttribute && typeDefinition.IsNested
                                                ? typeDefinition.DeclaringType :  typeDefinition;

            staticConstructor.Body.InsertAtTheBeginning(new[]
            {
                Instruction.Create(OpCodes.Ldtoken, loggerTypeDefinition.GetGenericInstantiationIfGeneric()),
                Instruction.Create(OpCodes.Call, getTypeFromHandleMethod),
                Instruction.Create(OpCodes.Call, getLoggerMethod),
                Instruction.Create(OpCodes.Stsfld, loggerFieldRef)
            });

            return(loggerFieldRef);
        }
예제 #6
0
 internal MethodWeaver(TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider,
     ILoggerProvider loggerProvider, MethodDefinition methodDefinition)
 {
     _typeReferenceProvider = typeReferenceProvider;
     _methodReferenceProvider = methodReferenceProvider;
     _methodDefinition = methodDefinition;
     _body = methodDefinition.Body;
     _loggerProvider = loggerProvider;
 }
예제 #7
0
 internal MethodWeaver(TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider,
                       ILoggerProvider loggerProvider, MethodDefinition methodDefinition)
 {
     _typeReferenceProvider   = typeReferenceProvider;
     _methodReferenceProvider = methodReferenceProvider;
     _methodDefinition        = methodDefinition;
     _body           = methodDefinition.Body;
     _loggerProvider = loggerProvider;
 }
예제 #8
0
 internal MethodWeaverBase(TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider,
                           ILoggerProvider loggerProvider, MethodDefinition methodDefinition)
 {
     _typeReferenceProvider   = typeReferenceProvider;
     _methodReferenceProvider = methodReferenceProvider;
     _methodDefinition        = methodDefinition;
     _body           = methodDefinition.Body;
     _isEmptyBody    = (_body.Instructions.Count == 0);
     _loggerProvider = loggerProvider;
 }
예제 #9
0
        internal AsyncMethodWeaver(TypeReferenceProvider typeReferenceProvider,
                                   MethodReferenceProvider methodReferenceProvider, ILoggerProvider loggerProvider,
                                   MethodDefinition methodDefinition)
            : base(typeReferenceProvider, methodReferenceProvider, loggerProvider, methodDefinition)
        {
            var asyncAttribute = methodDefinition.CustomAttributes.Single(it => it.AttributeType.FullName.Equals(_typeReferenceProvider.AsyncStateMachineAttribute.FullName));

            _generatedType = asyncAttribute.ConstructorArguments[0].Value as TypeDefinition;
            WeavingLog.LogDebug($"Weaving {methodDefinition.FullName}");
        }
예제 #10
0
 public MethodReferenceProvider(TypeReferenceProvider typeReferenceProvider, ModuleDefinition moduleDefinition)
 {
     _moduleDefinition = moduleDefinition;
     _typeReferenceProvider = typeReferenceProvider;
     _getTypeFromHandleReference =
         new Lazy<MethodReference>(() => _moduleDefinition.ImportReference(typeof(Type).GetRuntimeMethod("GetTypeFromHandle", new[] { typeof(RuntimeTypeHandle) })));
     _getTimestampReference =
         new Lazy<MethodReference>(() => _moduleDefinition.ImportReference(typeof(Stopwatch).GetRuntimeMethod("GetTimestamp", new Type[0])));
     _getTupleCreateReference =
         new Lazy<MethodReference>(InternalGetTupleCreateReference);
 }
예제 #11
0
        private void InternalExecute()
        {
            var typeReferenceProvider = new TypeReferenceProvider(_configuration, this, _moduleDefinition);
            var methodReferenceProvider = new MethodReferenceProvider(typeReferenceProvider, _moduleDefinition);

            var factory = new TypeWeaverFactory(_configuration.Filter, typeReferenceProvider, methodReferenceProvider);
            foreach (var type in _moduleDefinition.GetAllTypes())
            {
                var weaver = factory.Create(type);
                weaver.Execute();
            }
        }
예제 #12
0
 public TypeWeaverFactory(ITraceLoggingFilter filter,
                          TypeReferenceProvider typeReferenceProvider,
                          MethodReferenceProvider methodReferenceProvider,
                          bool shouldTraceConstructors,
                          bool shouldTraceProperties)
 {
     _filter = filter;
     _typeReferenceProvider   = typeReferenceProvider;
     _methodReferenceProvider = methodReferenceProvider;
     _shouldTraceConstructors = shouldTraceConstructors;
     _shouldTraceProperties   = shouldTraceProperties;
 }
예제 #13
0
 internal TypeWeaver(ITraceLoggingFilter filter, TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider,
     TypeDefinition typeDefinition)
 {
     _filter = filter;
     _typeReferenceProvider = typeReferenceProvider;
     _methodReferenceProvider = methodReferenceProvider;
     _typeDefinition = typeDefinition;
     _staticLoggerField = new Lazy<FieldReference>(CreateLoggerStaticField);
     _methodWeaverFactory = new MethodWeaverFactory(typeReferenceProvider, methodReferenceProvider, this);
     _hasCompilerGeneratedAttribute = new Lazy<bool>(() =>
         _typeDefinition.HasCustomAttributes && _typeDefinition.CustomAttributes
             .Any(attr => attr.AttributeType.FullName.Equals(typeof(CompilerGeneratedAttribute).FullName, StringComparison.Ordinal)));
 }
예제 #14
0
 internal TypeWeaver(ITraceLoggingFilter filter, TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider,
                     TypeDefinition typeDefinition)
 {
     _filter = filter;
     _typeReferenceProvider         = typeReferenceProvider;
     _methodReferenceProvider       = methodReferenceProvider;
     _typeDefinition                = typeDefinition;
     _staticLoggerField             = new Lazy <FieldReference>(CreateLoggerStaticField);
     _methodWeaverFactory           = new MethodWeaverFactory(typeReferenceProvider, methodReferenceProvider, this);
     _hasCompilerGeneratedAttribute = new Lazy <bool>(() =>
                                                      _typeDefinition.HasCustomAttributes && _typeDefinition.CustomAttributes
                                                      .Any(attr => attr.AttributeType.FullName.Equals(typeof(CompilerGeneratedAttribute).FullName, StringComparison.Ordinal)));
 }
예제 #15
0
        private void InternalExecute()
        {
            var typeReferenceProvider   = new TypeReferenceProvider(_configuration, this, _moduleDefinition);
            var methodReferenceProvider = new MethodReferenceProvider(typeReferenceProvider, _moduleDefinition);

            var factory = new TypeWeaverFactory(_configuration.Filter, typeReferenceProvider, methodReferenceProvider);

            foreach (var type in _moduleDefinition.GetAllTypes())
            {
                var weaver = factory.Create(type);
                weaver.Execute();
            }
        }
예제 #16
0
 internal TypeWeaver(ITraceLoggingFilter filter, bool shouldTraceConstructors, bool shouldTraceProperties, TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider,
                     TypeDefinition typeDefinition)
 {
     _filter = filter;
     _typeReferenceProvider         = typeReferenceProvider;
     _methodReferenceProvider       = methodReferenceProvider;
     _typeDefinition                = typeDefinition;
     _staticLoggerField             = new Lazy <FieldReference>(() => CreateLoggerStaticField(_typeReferenceProvider, _methodReferenceProvider, _typeDefinition));
     _methodWeaverFactory           = new MethodWeaverFactory(typeReferenceProvider, methodReferenceProvider, this);
     _hasCompilerGeneratedAttribute =
         new Lazy <bool>(() => CalculateTypeHasCompilerGeneratedAttribute(typeDefinition));
     _shouldTraceConstructors = shouldTraceConstructors;
     _shouldTraceProperties   = shouldTraceProperties;
 }
예제 #17
0
        private void InternalExecute()
        {
            TypeReferenceProvider   typeReferenceProvider   = new TypeReferenceProvider(_configuration, this, _moduleDefinition);
            MethodReferenceProvider methodReferenceProvider = new MethodReferenceProvider(typeReferenceProvider, _moduleDefinition);

            TypeWeaverFactory factory = new TypeWeaverFactory(_configuration.Filter, typeReferenceProvider, methodReferenceProvider, _configuration.ShouldTraceConstructors,
                                                              _configuration.ShouldTraceProperties);

            foreach (TypeDefinition type in _moduleDefinition.GetAllTypes())
            {
                TypeWeaver weaver = factory.Create(type);
                weaver.Execute();
            }
        }
예제 #18
0
 public MethodReferenceProvider(TypeReferenceProvider typeReferenceProvider, ModuleDefinition moduleDefinition)
 {
     _moduleDefinition = moduleDefinition;
     _typeReferenceProvider = typeReferenceProvider;
 }
예제 #19
0
 public MethodReferenceProvider(TypeReferenceProvider typeReferenceProvider, ModuleDefinition moduleDefinition)
 {
     _moduleDefinition      = moduleDefinition;
     _typeReferenceProvider = typeReferenceProvider;
 }
예제 #20
0
 public TypeWeaverFactory(ITraceLoggingFilter filter, TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider)
 {
     _filter = filter;
     _typeReferenceProvider   = typeReferenceProvider;
     _methodReferenceProvider = methodReferenceProvider;
 }
예제 #21
0
 internal MethodWeaver(TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider,
                       ILoggerProvider loggerProvider, MethodDefinition methodDefinition) : base(typeReferenceProvider, methodReferenceProvider,
                                                                                                 loggerProvider, methodDefinition)
 {
 }
예제 #22
0
 public TypeWeaverFactory(ITraceLoggingFilter filter, TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider)
 {
     _filter = filter;
     _typeReferenceProvider = typeReferenceProvider;
     _methodReferenceProvider = methodReferenceProvider;
 }