using System.Text.RegularExpressions; namespace LTEMvcApp.Models { /// /// 日志工具类 /// public static class LogUtils { /// /// 日志级别常量 /// public static class LogLevels { public const int None = 0; public const int Error = 1; public const int Warn = 2; public const int Info = 3; public const int Debug = 4; /// /// 获取级别名称 /// /// 级别值 /// 级别名称 public static string GetLevelName(int level) { return level switch { None => "none", Error => "error", Warn => "warn", Info => "info", Debug => "debug", _ => "unknown" }; } /// /// 获取级别值 /// /// 级别名称 /// 级别值 public static int GetLevelValue(string levelName) { return levelName?.ToLower() switch { "none" => None, "error" => Error, "warn" => Warn, "info" => Info, "debug" => Debug, _ => None }; } /// /// 获取所有级别选项 /// /// 级别选项列表 public static List GetLevelOptions() { return new List { new() { Value = Error, Text = "Error" }, new() { Value = Warn, Text = "Warning" }, new() { Value = Info, Text = "Info" }, new() { Value = Debug, Text = "Debug" } }; } } /// /// 级别选项 /// public class LevelOption { public int Value { get; set; } public string Text { get; set; } = string.Empty; } /// /// 时间戳转换为时间字符串 /// /// 时间戳(毫秒) /// 是否显示完整日期 /// 时间字符串 public static string TimestampToTime(long timestamp, bool full = false) { // 持续时间 (< 2000-01-01 00:00:00) if (timestamp < 946681200000) { return TimestampToDuration(timestamp); } var dateTime = DateTimeOffset.FromUnixTimeMilliseconds(timestamp).DateTime; if (full) { return dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff"); } return dateTime.ToString("HH:mm:ss.fff"); } /// /// 时间戳转换为持续时间字符串 /// /// 时间戳(毫秒) /// 持续时间字符串 public static string TimestampToDuration(long timestamp) { var prefix = timestamp < 0 ? "-" : ""; var absTimestamp = Math.Abs(timestamp); var timeSpan = TimeSpan.FromMilliseconds(absTimestamp); return $"{prefix}{timeSpan.Hours:D2}:{timeSpan.Minutes:D2}:{timeSpan.Seconds:D2}.{timeSpan.Milliseconds:D3}"; } /// /// 时间字符串转换为时间戳 /// /// 时间字符串 /// 时间戳,转换失败返回null public static long? TimeStringToTimestamp(string timeString) { if (string.IsNullOrEmpty(timeString)) return null; // 匹配格式: HH:mm:ss.fff 或 mm:ss.fff 或 ss.fff var match = Regex.Match(timeString, @"^(((\d+):)?(\d+):)?(\d+)(\.(\d+))?$"); if (!match.Success) return null; var hours = int.Parse(match.Groups[3].Success ? match.Groups[3].Value : "0"); var minutes = int.Parse(match.Groups[4].Success ? match.Groups[4].Value : "0"); var seconds = int.Parse(match.Groups[5].Value); var milliseconds = match.Groups[7].Success ? int.Parse(match.Groups[7].Value.PadRight(3, '0')) : 0; return (hours * 3600000L) + (minutes * 60000L) + (seconds * 1000L) + milliseconds; } /// /// 获取日期偏移量 /// /// 时间戳 /// 日期偏移量 public static long GetDayOffset(long timestamp) { if (timestamp < 946681200000) return 0; var dateTime = DateTimeOffset.FromUnixTimeMilliseconds(timestamp).DateTime; var dayStart = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, DateTimeKind.Utc); return new DateTimeOffset(dayStart).ToUnixTimeMilliseconds(); } /// /// 字符串转ID /// /// 字符串 /// ID值 public static int StringToId(string str) { if (string.IsNullOrEmpty(str)) return 0; var hash = 5381; foreach (var c in str) { hash = ((hash << 5) + hash) ^ c; } return hash; } /// /// 创建正则表达式 /// /// 模式 /// 是否忽略大小写 /// 正则表达式,创建失败返回null public static Regex? CreateRegex(string pattern, bool ignoreCase = true) { if (string.IsNullOrEmpty(pattern)) return null; try { var options = ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None; // 检查是否是正则表达式格式 /pattern/flags var regexMatch = Regex.Match(pattern, @"^/(.+)/([gi]*)$"); if (regexMatch.Success) { var regexPattern = regexMatch.Groups[1].Value; var flags = regexMatch.Groups[2].Value; if (flags.Contains('i')) options |= RegexOptions.IgnoreCase; if (flags.Contains('g')) options |= RegexOptions.Multiline; return new Regex(regexPattern, options); } // 普通文本搜索,添加转义 var escapedPattern = Regex.Escape(pattern); return new Regex(escapedPattern, options); } catch { return null; } } /// /// 高亮搜索文本 /// /// 原文本 /// 搜索模式 /// 高亮后的文本 public static string HighlightSearchText(string text, string searchPattern) { if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(searchPattern)) return text; var regex = CreateRegex(searchPattern); if (regex == null) return text; return regex.Replace(text, "$&"); } /// /// 格式化文件大小 /// /// 字节数 /// 格式化后的字符串 public static string FormatFileSize(long bytes) { string[] sizes = { "B", "KB", "MB", "GB", "TB" }; double len = bytes; int order = 0; while (len >= 1024 && order < sizes.Length - 1) { order++; len = len / 1024; } return $"{len:0.##} {sizes[order]}"; } /// /// 解析大小字符串 /// /// 大小字符串 /// 字节数 public static long ParseSizeString(string sizeString) { if (string.IsNullOrEmpty(sizeString)) return 0; var match = Regex.Match(sizeString, @"^(\d+)([KMG])?$", RegexOptions.IgnoreCase); if (!match.Success) return 0; var size = long.Parse(match.Groups[1].Value); var unit = match.Groups[2].Value.ToUpper(); return unit switch { "K" => size * 1024, "M" => size * 1024 * 1024, "G" => size * 1024 * 1024 * 1024, _ => size }; } /// /// 获取颜色亮度 /// /// 颜色代码 /// 亮度值 (0-1) public static double GetColorBrightness(string color) { var match = Regex.Match(color, @"#([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})"); if (!match.Success) return 1; var r = int.Parse(match.Groups[1].Value, System.Globalization.NumberStyles.HexNumber); var g = int.Parse(match.Groups[2].Value, System.Globalization.NumberStyles.HexNumber); var b = int.Parse(match.Groups[3].Value, System.Globalization.NumberStyles.HexNumber); return (r * r * 0.299 + g * g * 0.587 + b * b * 0.114) / (255.0 * 255.0); } /// /// 获取文本颜色 /// /// 背景颜色 /// 文本颜色 public static string GetTextColor(string backgroundColor) { var brightness = GetColorBrightness(backgroundColor); return brightness >= 0.15 ? "black" : "white"; } /// /// 生成唯一ID /// /// 唯一ID public static string GenerateUniqueId() { return Guid.NewGuid().ToString("N"); } /// /// 安全地执行操作 /// /// 要执行的操作 /// 默认值 /// 操作结果或默认值 public static T SafeExecute(Func action, T defaultValue) { try { return action(); } catch { return defaultValue; } } } }