public TypeReferenceProvider(TraceLoggingConfiguration configuration, ILoggerAdapterMetadataScopeProvider loggerAdapterMetadataScopeProvider, ModuleDefinition moduleDefinition)
 {
     _configuration = configuration;
     _moduleDefinition = moduleDefinition;
     _loggerAdapterMetadataScopeProvider = loggerAdapterMetadataScopeProvider;
     _stringArray = new Lazy<TypeReference>(() => moduleDefinition.Import((typeof(string[]))));
     _objectArray = new Lazy<TypeReference>(() => moduleDefinition.Import(typeof(object[])));
     _type = new Lazy<TypeReference>(() => moduleDefinition.Import(typeof(Type)));
     _stopwatch = new Lazy<TypeReference>(() => moduleDefinition.Import(typeof(Stopwatch)));
     _exception = new Lazy<TypeReference>(() => moduleDefinition.Import(typeof(Exception)));
 }
 /// <summary>
 /// Weaves the logging and tracing into the given module. Please note that the module itself is modified.
 /// Configuration is used to specify certain weaving behaviors and provide necessary input for the weaver
 /// </summary>
 /// <param name="configuration">Configuration information</param>
 /// <param name="moduleDefinition">Target module</param>
 public static void Execute(TraceLoggingConfiguration configuration, ModuleDefinition moduleDefinition)
 {
     try
     {
         WeavingLog.LogInfo("Tracer: Starts weaving.");
         var timer = Stopwatch.StartNew();
         var weaver = new ModuleLevelWeaver(configuration, moduleDefinition);
         weaver.InternalExecute();
         timer.Stop();
         WeavingLog.LogInfo(String.Format("Tracer: Weaving done in {0} ms.", timer.ElapsedMilliseconds));
     }
     catch (Exception ex)
     {
         WeavingLog.LogError(String.Format("Tracer: Weaving failed with {0}", ex));
         throw;
     }
 }
        /// <summary>
        /// Weaves the tracer to a single assembly. It adds a trace enter and trace leave call to all methods defined by the filter.
        /// It also replaces static Log calls to logger instance calls and extends the call parameters with method name information.
        /// Use the configuration to specify exact weaver behavior, for example to define the actual logging library/methods.  
        /// </summary>
        /// <param name="assemblyPath">Path to the assembly to be weaved</param>
        /// <param name="configuration">An instance of <see cref="TraceLoggingConfiguration"/>.</param>
        /// <example>
        /// Given a class with a method where Log is a static class
        /// public MyClass
        /// {
        ///     public int MyMethod(string inputString, int inputInteger)
        ///     {
        ///         ...
        ///         Log.Warning("Something suspicious");
        ///         ...
        ///         return result;
        ///     }
        /// } 
        /// 
        /// The rewriter will create the following (assuming that the trace with method call time is turned on)
        /// public MyClass
        /// {
        ///     private static LogAdapter _log = LogManager.GetLogger(typeof(MyClass));
        /// 
        ///     public int MyMethod(string inputString, int inputInteger)
        ///     {
        ///         _log.TraceEnter("int MyMethod(string, int)", new[] {"inputString", "inputInteger"}, new[] { inputString, inputInteger });
        ///         var startTick = Stopwatch.GetTimestamp();
        ///          ...
        ///         _log.LogWarning("int MyMethod(string, int)","Something suspicious");
        ///         ...
        /// 
        ///         _log.TraceLeave("int MyMethod(string, int)", Stopwatch.GetTimestamp() - startTick, result);
        ///         return result;
        ///     }
        /// } 
        /// </example>
        public static void Execute(string assemblyPath, TraceLoggingConfiguration configuration)
        {
            //get module definition
            ModuleDefinition moduleDef = null;

            var pdbFile = Path.ChangeExtension(assemblyPath, "pdb");
            var hasPdb = File.Exists(pdbFile);

            if (hasPdb)
            {
                using (var symbolStream = File.OpenRead(pdbFile))
                {
                    moduleDef = ModuleDefinition.ReadModule(assemblyPath, new ReaderParameters
                        {
                            AssemblyResolver = new DefaultAssemblyResolver(),
                            ReadSymbols = true,
                            SymbolReaderProvider = new PdbReaderProvider(),
                            SymbolStream = symbolStream
                        });
                }
            }
            else
            {
                moduleDef = ModuleDefinition.ReadModule(assemblyPath);
            }

            //execute weaving
            ModuleLevelWeaver.Execute(configuration, moduleDef);

            //write back the results
            moduleDef.Write(assemblyPath, new WriterParameters
                    {
                        WriteSymbols = hasPdb,
                        SymbolWriterProvider = new PdbWriterProvider()
                    });
        }
 private ModuleLevelWeaver(TraceLoggingConfiguration configuration, ModuleDefinition moduleDefinition)
 {
     _configuration = configuration;
     _moduleDefinition = moduleDefinition;
 }