Exemplo n.º 1
0
 public MethodWeaverFactory(TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider,
                            MethodWeaverBase.ILoggerProvider loggerProvider)
 {
     _typeReferenceProvider   = typeReferenceProvider;
     _methodReferenceProvider = methodReferenceProvider;
     _loggerProvider          = loggerProvider;
 }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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}");
        }
Exemplo n.º 4
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;
 }
Exemplo n.º 5
0
 public TypeWeaverFactory(ITraceLoggingFilter filter,
                          TypeReferenceProvider typeReferenceProvider,
                          MethodReferenceProvider methodReferenceProvider,
                          bool shouldTraceConstructors,
                          bool shouldTraceProperties)
 {
     _filter = filter;
     _typeReferenceProvider   = typeReferenceProvider;
     _methodReferenceProvider = methodReferenceProvider;
     _shouldTraceConstructors = shouldTraceConstructors;
     _shouldTraceProperties   = shouldTraceProperties;
 }
Exemplo n.º 6
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, _configuration.ShouldTraceConstructors,
                                                _configuration.ShouldTraceProperties);

            foreach (var type in _moduleDefinition.GetAllTypes())
            {
                var weaver = factory.Create(type);
                weaver.Execute();
            }
        }
Exemplo n.º 7
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>(() =>
                                                      _typeDefinition.HasCustomAttributes && _typeDefinition.CustomAttributes
                                                      .Any(attr => attr.AttributeType.FullName.Equals(typeof(CompilerGeneratedAttribute).FullName, StringComparison.Ordinal)));
     _shouldTraceConstructors = shouldTraceConstructors;
     _shouldTraceProperties   = shouldTraceProperties;
 }
Exemplo n.º 8
0
 internal MethodWeaver(TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider,
                       ILoggerProvider loggerProvider, MethodDefinition methodDefinition) : base(typeReferenceProvider, methodReferenceProvider,
                                                                                                 loggerProvider, methodDefinition)
 {
 }