Exemplo n.º 1
0
        private static void Initialize()
        {
            try
            {
                Assembly currentAssembly = typeof(Log).Assembly;

                List<Assembly> asmList = new StackTrace().GetFrames()
                    .Where(frame => 
                        {
                            MethodBase method = frame.GetMethod();
                            return method != null && method.DeclaringType != null && method.DeclaringType.Assembly != null;
                        })
                    .Select(frame => frame.GetMethod().DeclaringType.Assembly)
                    .ToList();
                int currentAssemblyIndex = asmList.FindLastIndex(asm => 
                    asm.FullName.Equals(currentAssembly.FullName, StringComparison.OrdinalIgnoreCase)) + 1;

                Assembly entryAssembly = null;
                if (currentAssemblyIndex < asmList.Count)
                {
                    entryAssembly = asmList
                        .Skip(currentAssemblyIndex)
                        .LastOrDefault(asm => asm.FullName.IndexOf(MasterNamespace, StringComparison.OrdinalIgnoreCase) != -1);
                }
                else
                {
                    entryAssembly = asmList.LastOrDefault(asm => asm.FullName.IndexOf(MasterNamespace, StringComparison.OrdinalIgnoreCase) != -1);
                }

                if (entryAssembly == null)
                {
                    if (currentAssemblyIndex < asmList.Count)
                    {
                        entryAssembly = asmList
                            .Skip(currentAssemblyIndex)
                            .LastOrDefault(asm => !asm.GlobalAssemblyCache);
                    }
                    else
                    {
                        entryAssembly = asmList.LastOrDefault(asm => !asm.GlobalAssemblyCache);
                    }
                }

                if (entryAssembly == null)
                {
                    MethodBase method = new StackTrace().GetFrames().Last().GetMethod();
                    if (method != null && method.DeclaringType != null && method.DeclaringType.Assembly != null)
                        entryAssembly = method.DeclaringType.Assembly;
                }

                Version asmVer = entryAssembly.GetName().Version;

                log4net.GlobalContext.Properties["Assembly.Version"] = asmVer.ToString();
                log4net.GlobalContext.Properties["Assembly.Version.Short"] = string.Format("{0}_{1}", asmVer.Major, asmVer.Minor);
                log4net.GlobalContext.Properties["Assembly.FullName"] = entryAssembly.FullName;
                log4net.GlobalContext.Properties["Runtime.Version"] = entryAssembly.ImageRuntimeVersion;
            }
            catch
            {
                // unable to set up log4net properties
            }

            // Configure log4net within application configuration file
            // For web apps, this will work if the config info is in web.config:
            Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        }