Ejemplo n.º 1
0
        public void AssemblyLevelSpecification_InternalClass_AllFilter()
        {
            string code = @"
                using TracerAttributes;

                namespace First
                {
                    internal class MyClass
                    {
                        public void PublicMethod()
                        {}

                        internal void InternalMethod()
                        {}

                        protected void ProtectedMethod()
                        {}

                        private void PrivateMethod()
                        {}
                    }
                }
            ";

            MethodDefinition    publicMethodDef    = GetMethodDefinition(code, "PublicMethod");
            MethodDefinition    internalMethodDef  = GetMethodDefinition(code, "InternalMethod");
            MethodDefinition    protectedMethodDef = GetMethodDefinition(code, "ProtectedMethod");
            MethodDefinition    privateMethodDef   = GetMethodDefinition(code, "PrivateMethod");
            ITraceLoggingFilter filter             = GetDefaultFilter(TraceTargetVisibility.All, TraceTargetVisibility.ProtectedOrMoreVisible);

            filter.ShouldAddTrace(publicMethodDef).Should().BeTrue("public");
            filter.ShouldAddTrace(internalMethodDef).Should().BeTrue("internal");
            filter.ShouldAddTrace(protectedMethodDef).Should().BeTrue("protected");
            filter.ShouldAddTrace(privateMethodDef).Should().BeFalse("private");
        }
Ejemplo n.º 2
0
        public void NestedClassLevelNoTrace_Overrides_AssemblyLevel()
        {
            string code = @"
                using TracerAttributes;

                namespace First
                {
                    [NoTrace]
                    public class MyClass
                    {
                        public class InnerClass
                        {
                            public void PublicMethod()
                            {}

                            public void PublicMethod2()
                            {}
                        }
                    }
                }
            ";

            MethodDefinition    publicMethodDef  = GetMethodDefinition(code, "PublicMethod");
            MethodDefinition    publicMethodDef2 = GetMethodDefinition(code, "PublicMethod");
            ITraceLoggingFilter filter           = GetDefaultFilter(TraceTargetVisibility.Public, TraceTargetVisibility.All);

            filter.ShouldAddTrace(publicMethodDef).Should().BeFalse("public");
            filter.ShouldAddTrace(publicMethodDef2).Should().BeFalse("public");
        }
 private TraceLoggingConfiguration(ITraceLoggingFilter filter, string adapterAssemblyDisplayName, 
     string loggerAdapterTypeName, string logManagerAdapterTypeName, string staticLoggerTypeFullName)
 {
     _filter = filter ?? NullFilter.Instance;
     _adapterAssemblyName = new AssemblyName(adapterAssemblyDisplayName ?? "Tracer.LogAdapter, Version=1.0.0.0");
     //set version to avoid cecil nullRef exception issue
     if (_adapterAssemblyName.Version == null) _adapterAssemblyName.Version = new Version(0,0,0,0);
     _loggerAdapterTypeFullName = loggerAdapterTypeName ?? "Tracer.LogAdapter.ILog";
     _logManagerAdapterTypeFullName = logManagerAdapterTypeName ?? "Tracer.LogAdapter.LogManager";
     _staticLoggerTypeFullName = staticLoggerTypeFullName ?? "Tracer.LogAdapter.Log";
 }
Ejemplo n.º 4
0
 public TypeWeaverFactory(ITraceLoggingFilter filter,
                          TypeReferenceProvider typeReferenceProvider,
                          MethodReferenceProvider methodReferenceProvider,
                          bool shouldTraceConstructors,
                          bool shouldTraceProperties)
 {
     _filter = filter;
     _typeReferenceProvider   = typeReferenceProvider;
     _methodReferenceProvider = methodReferenceProvider;
     _shouldTraceConstructors = shouldTraceConstructors;
     _shouldTraceProperties   = shouldTraceProperties;
 }
Ejemplo n.º 5
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)));
 }
Ejemplo n.º 6
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)));
 }
Ejemplo n.º 7
0
        protected void Rewrite(string assemblyPath, ITraceLoggingFilter filter)
        {
            //Set-up log adapter to our mock
            var assembly = Assembly.GetExecutingAssembly();

            var config = TraceLoggingConfiguration.New
                         .WithFilter(filter)
                         .WithAdapterAssembly(assembly.GetName().FullName)
                         .WithLogManager(typeof(MockLogManagerAdapter).FullName)
                         .WithLogger(typeof(IMockLogAdapter).FullName)
                         .WithStaticLogger(typeof(MockLog).FullName);

            AssemblyWeaver.Execute(assemblyPath, config);
        }
Ejemplo n.º 8
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;
 }
 private TraceLoggingConfiguration(ITraceLoggingFilter filter, string adapterAssemblyDisplayName,
                                   string loggerAdapterTypeName, string logManagerAdapterTypeName, string staticLoggerTypeFullName)
 {
     _filter = filter ?? NullFilter.Instance;
     _adapterAssemblyName = new AssemblyName(adapterAssemblyDisplayName ?? "Tracer.LogAdapter, Version=1.0.0.0");
     //set version to avoid cecil nullRef exception issue
     if (_adapterAssemblyName.Version == null)
     {
         _adapterAssemblyName.Version = new Version(0, 0, 0, 0);
     }
     _loggerAdapterTypeFullName     = loggerAdapterTypeName ?? "Tracer.LogAdapter.ILog";
     _logManagerAdapterTypeFullName = logManagerAdapterTypeName ?? "Tracer.LogAdapter.LogManager";
     _staticLoggerTypeFullName      = staticLoggerTypeFullName ?? "Tracer.LogAdapter.Log";
 }
Ejemplo n.º 10
0
 private TraceLoggingConfiguration(ITraceLoggingFilter filter, string adapterAssemblyDisplayName,
                                   string loggerAdapterTypeName, string logManagerAdapterTypeName, string staticLoggerTypeFullName, bool traceConstructors
                                   , bool traceProperties, bool isDisabled = false)
 {
     _isDisabled          = isDisabled;
     _filter              = filter ?? NullFilter.Instance;
     _adapterAssemblyName = new AssemblyName(adapterAssemblyDisplayName ?? "Tracer.LogAdapter, Version=1.0.0.0");
     //set version to avoid cecil nullRef exception issue
     if (_adapterAssemblyName.Version == null)
     {
         _adapterAssemblyName.Version = new Version(0, 0, 0, 0);
     }
     _loggerAdapterTypeFullName     = loggerAdapterTypeName ?? "Tracer.LogAdapter.ILog";
     _logManagerAdapterTypeFullName = logManagerAdapterTypeName ?? "Tracer.LogAdapter.LogManager";
     _staticLoggerTypeFullName      = staticLoggerTypeFullName ?? "Tracer.LogAdapter.Log";
     _traceConstructors             = traceConstructors;
     _traceProperties = traceProperties;
 }
Ejemplo n.º 11
0
        protected MockLogResult RunTest(string source, ITraceLoggingFilter filter, string staticEntryPoint, bool shouldTraceConstructors = false)
        {
            string[] splitEntry = staticEntryPoint.Split(new[] { "::" }, StringSplitOptions.RemoveEmptyEntries);
            if (splitEntry.Length != 2)
            {
                throw new ApplicationException("Static entry point must be in a form Namesp.Namesp2.Class::Method");
            }
            string entryClass  = splitEntry[0];
            string entryMethod = splitEntry[1];

            Uri testDllLocation = new Uri(Assembly.GetExecutingAssembly().CodeBase);

            string assemblyPath = Compile(source, "testasm", new[] { testDllLocation.AbsolutePath });

            Rewrite(assemblyPath, filter, shouldTraceConstructors);

            //----
            return(RunCode(assemblyPath, entryClass, entryMethod));
        }
Ejemplo n.º 12
0
        protected void Rewrite(string assemblyPath, ITraceLoggingFilter filter, bool traceConstructors = false)
        {
            //Set-up log adapter to our mock
            Assembly assembly = Assembly.GetExecutingAssembly();

            TraceLoggingConfiguration.TraceLoggingConfigurationBuilder config = TraceLoggingConfiguration.New
                                                                                .WithFilter(filter)
                                                                                .WithAdapterAssembly(assembly.GetName().FullName)
                                                                                .WithLogManager(typeof(MockLogManagerAdapter).FullName)
                                                                                .WithLogger(typeof(MockLogAdapter).FullName)
                                                                                .WithStaticLogger(typeof(MockLog).FullName);

            if (traceConstructors)
            {
                config.WithConstructorTraceOn();
            }

            AssemblyWeaver.Execute(assemblyPath, config);
        }
Ejemplo n.º 13
0
        public void MethodLevelNoTrace_Overrides_AssemblyLevel()
        {
            string code = @"
                using TracerAttributes;

                namespace First
                {
                    public class MyClass
                    {
                        [NoTrace]
                        public void MyMethod()
                        {}
                    }
                }
            ";

            MethodDefinition    methodDef = GetMethodDefinition(code, "MyMethod");
            ITraceLoggingFilter filter    = GetDefaultFilter(TraceTargetVisibility.Public, TraceTargetVisibility.Public);

            filter.ShouldAddTrace(methodDef).Should().BeFalse();
        }
Ejemplo n.º 14
0
        public void NestedClassLevelTraceOn_Overrides_AssemblyLevel()
        {
            string code = @"
                using TracerAttributes;

                namespace First
                {
                    [TraceOn(TraceTarget.Protected)]
                    public class MyClass
                    {
                        public class InnerClass
                        {
                            public void PublicMethod()
                            {}

                            internal void InternalMethod()
                            {}

                            protected void ProtectedMethod()
                            {}

                            private void PrivateMethod()
                            {}
                        }
                    }
                }
            ";

            MethodDefinition    publicMethodDef    = GetMethodDefinition(code, "PublicMethod");
            MethodDefinition    internalMethodDef  = GetMethodDefinition(code, "InternalMethod");
            MethodDefinition    protectedMethodDef = GetMethodDefinition(code, "ProtectedMethod");
            MethodDefinition    privateMethodDef   = GetMethodDefinition(code, "PrivateMethod");
            ITraceLoggingFilter filter             = GetDefaultFilter(TraceTargetVisibility.Public, TraceTargetVisibility.All);

            filter.ShouldAddTrace(publicMethodDef).Should().BeTrue("public");
            filter.ShouldAddTrace(internalMethodDef).Should().BeTrue("internal");
            filter.ShouldAddTrace(protectedMethodDef).Should().BeTrue("protected");
            filter.ShouldAddTrace(privateMethodDef).Should().BeFalse("private");
        }
 public TraceLoggingConfigurationBuilder WithFilter(ITraceLoggingFilter filter)
 {
     _filter = filter;
     return this;
 }
Ejemplo n.º 16
0
 public TypeWeaverFactory(ITraceLoggingFilter filter, TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider)
 {
     _filter = filter;
     _typeReferenceProvider   = typeReferenceProvider;
     _methodReferenceProvider = methodReferenceProvider;
 }
Ejemplo n.º 17
0
        protected void Rewrite(string assemblyPath, ITraceLoggingFilter filter)
        {
            //Set-up log adapter to our mock 
            var assembly = Assembly.GetExecutingAssembly();

            var config = TraceLoggingConfiguration.New
                .WithFilter(filter)
                .WithAdapterAssembly(assembly.GetName().FullName)
                .WithLogManager(typeof(MockLogManagerAdapter).FullName)
                .WithLogger(typeof(IMockLogAdapter).FullName)
                .WithStaticLogger(typeof(MockLog).FullName);
                
            AssemblyWeaver.Execute(assemblyPath, config);
        }
Ejemplo n.º 18
0
 public TypeWeaverFactory(ITraceLoggingFilter filter, TypeReferenceProvider typeReferenceProvider, MethodReferenceProvider methodReferenceProvider)
 {
     _filter = filter;
     _typeReferenceProvider = typeReferenceProvider;
     _methodReferenceProvider = methodReferenceProvider;
 }
Ejemplo n.º 19
0
 public TraceLoggingConfigurationBuilder WithFilter(ITraceLoggingFilter filter)
 {
     _filter = filter;
     return(this);
 }
Ejemplo n.º 20
0
        protected MockLogResult RunTest(string source, ITraceLoggingFilter filter, string staticEntryPoint)
        {
            var splitEntry = staticEntryPoint.Split(new [] { "::" }, StringSplitOptions.RemoveEmptyEntries);
            if (splitEntry.Length != 2) throw new ApplicationException("Static entry point must be in a form Namesp.Namesp2.Class::Method");
            var entryClass = splitEntry[0];
            var entryMethod = splitEntry[1];

            var testDllLocation = new Uri(Assembly.GetExecutingAssembly().CodeBase);

            var assemblyPath = Compile(source, "testasm", new []{ testDllLocation.AbsolutePath });
            Rewrite(assemblyPath, filter);

            //----
            return RunCode(assemblyPath, entryClass, entryMethod);
        }