本文整理汇总了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);
}