当前位置: 首页>>代码示例>>C#>>正文


C# StackTrace.LastOrDefault方法代码示例

本文整理汇总了C#中System.Diagnostics.StackTrace.LastOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# StackTrace.LastOrDefault方法的具体用法?C# StackTrace.LastOrDefault怎么用?C# StackTrace.LastOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Diagnostics.StackTrace的用法示例。


在下文中一共展示了StackTrace.LastOrDefault方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Initialize

        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);
        }
开发者ID:rbramwell,项目名称:OrionSDK,代码行数:66,代码来源:Log.cs


注:本文中的System.Diagnostics.StackTrace.LastOrDefault方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。