using log4net; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Web; using System.Xml; public class DebugInfo { private static string basePath; private static bool isInit; private static object obj = new object(); public static string DebugString { get; private set; } public static string BranchName { get; private set; } public const string TemplateName = "change.log"; public const string DataName = "changelog.xml"; public const string ResultName = "changelog.txt"; public static void Init() { if (isInit) return; lock (obj) { if (isInit) return; try { basePath = HttpContext.Current.Server.MapPath("~/"); DebugString = GetStaticDebugString(); isInit = true; } catch (Exception) { } } } public static string GetStaticDebugString() { var ChangeLogFilePath = Path.Combine(basePath, DataName); var ResultNameFilePath = Path.Combine(basePath, ResultName); if (File.Exists(ResultNameFilePath)) { if (File.ReadAllText(ResultNameFilePath, Encoding.Default) != "") { return File.ReadAllText(ResultNameFilePath); } } if (!File.Exists(ChangeLogFilePath)) { return "changelog.xml doesn't exist"; } var ChangeLogPatternFilePath = Path.Combine(basePath, TemplateName); var xmlLog = new XmlDocument(); xmlLog.Load(ChangeLogFilePath); var logs = xmlLog.SelectNodes("//lastBuiltRevision"); if (logs == null) return ""; var nodes = logs.Cast().ToList(); try { var fileContent = File.ReadAllText(ChangeLogPatternFilePath, Encoding.Default); var lastCommitIDNode = nodes.LastOrDefault(); var branchNameNode = lastCommitIDNode; if (lastCommitIDNode != null) { var lastCommitID = lastCommitIDNode.SelectSingleNode("SHA1"); if (lastCommitID != null) { fileContent = fileContent.Replace("{RevisionLast}", lastCommitID.InnerText); } } else { fileContent = fileContent.Replace("{RevisionLast}", ""); } if (branchNameNode != null) { var branchName = branchNameNode.SelectSingleNode(".//name"); if (branchName != null) { fileContent = fileContent.Replace("{BranchName}", branchName.InnerText); } } else { fileContent = fileContent.Replace("{BranchName}", ""); } File.WriteAllText(ResultNameFilePath, fileContent); return fileContent; } catch (Exception e) { LogManager.GetLogger("ASC.DebugInfo").Error("DebugInfo", e); return "e.Message: " + e.Message; } return ""; } private static bool IsServiceLogItem(string log) { return Regex.IsMatch(log, "(^Merged)|(^Sql)", RegexOptions.IgnoreCase); } }