Пример #1
0
        private static bool ReadCustomSection()
        {
            try
            {
                var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var logSection = config.GetSection("Log") as LogSection;
                if (logSection == null) return false;
                for (var i = 0; i < logSection.Types.Count; i++)
                {
                    var type = logSection.Types[i];
                    if (string.IsNullOrWhiteSpace(type.Type)) continue;
                    var typeName = type.Type.Trim();
                    TypeInfo[] types;
                    if (typeName.Equals("*"))
                    {
                        TypeInfo allTypes;
                        if (!TypeInfos.TryGetValue(typeName, out allTypes))
                            TypeInfos[typeName] = allTypes = new TypeInfo();
                        types = new[] {allTypes};
                    }
                    else
                    {
                        var typeNames = typeName.Split(',');
                        types = new TypeInfo[typeNames.Length];
                        for (var i1 = 0; i1 < typeNames.Length; i1++)
                        {
                            typeName = typeNames[i1].Trim();
                            TypeInfo info;
                            if (!TypeInfos.TryGetValue(typeName, out info))
                                TypeInfos[typeName] = info = new TypeInfo();
                            types[i1] = info;
                        }
                    }

                    foreach (var typeInfo in types)
                    {
                        if (type.Method.Equals("*")) typeInfo.AllMethods = true;
                        else if (!string.IsNullOrWhiteSpace(type.Method))
                        {
                            var names = type.Method.Split(',');
                            foreach (var t in names)
                            {
                                var methodName = t.Trim();
                                if (!string.IsNullOrWhiteSpace(methodName))
                                    typeInfo.Methods.Add(methodName);
                            }
                        }
                        if (type.Ignore.Equals("*")) typeInfo.AllIgnores = true;
                        else if (!string.IsNullOrWhiteSpace(type.Ignore))
                        {
                            var names = type.Ignore.Split(',');
                            foreach (var t in names)
                            {
                                var methodName = t.Trim();
                                if (!string.IsNullOrWhiteSpace(methodName))
                                    typeInfo.Ignores.Add(methodName);
                            }
                        }
                    }
                }
            }
            catch (ConfigurationErrorsException err)
            {
                Console.WriteLine("ReadCustomSection(string): {0}", err);
                return false;
            }
            return true;
        }
Пример #2
0
        private static void PatchType(TypeDefinition type, TypeInfo allTypes, MethodDefinition stringConcat, MethodDefinition logMethod)
        {
            var patchedMethods = new List<MethodDefinition>();
            if (allTypes != null)
            {
                foreach (var method in type.Methods)
                {
                    if (patchedMethods.Contains(method) || method.Name.Contains("<")) continue;
                    if (allTypes.AllIgnores || allTypes.Ignores.Any(method.Name.Equals)) continue;
                    if (!allTypes.AllMethods && !allTypes.Methods.Any(method.Name.Equals)) continue;
                    PatchMethod(method, type, stringConcat, logMethod);
                    patchedMethods.Add(method);
                }
            }

            var keys = TypeInfos.Keys.Where(k => type.Name.StartsWith(k, StringComparison.OrdinalIgnoreCase));
            foreach (var key in keys)
            {
                var typeInfo = TypeInfos[key];
                foreach (var method in type.Methods)
                {
                    if (patchedMethods.Contains(method) || method.Name.Contains("<")) continue;
                    if (typeInfo.AllIgnores || typeInfo.Ignores.Any(method.Name.Equals)) continue;
                    if (!typeInfo.AllMethods && !typeInfo.Methods.Any(method.Name.Equals)) continue;
                    PatchMethod(method, type, stringConcat, logMethod);
                    patchedMethods.Add(method);
                }
            }
            foreach (var nestedType in type.NestedTypes)
                PatchType(nestedType, allTypes, stringConcat, logMethod);
        }