コード例 #1
0
        public IActionResult OnGet()
        {
            ViewData["scheme"] = HttpContext.Request.Scheme;
            ViewData["host"]   = HttpContext.Request.Host;
            ViewData["path"]   = HttpContext.Request.Path;
            bool login = AuthHelper.Check(HttpContext);

            if (!login)
            {
                return(RedirectToPage("Login"));
            }

            string logsPath = Path.Combine(AppContext.BaseDirectory, "logs");

            string[] files = Directory.GetFiles(logsPath);
            files = files.Select(s => Path.GetFileNameWithoutExtension(s)).ToArray();
            int startDate = StartDate.Year * 10000 + StartDate.Month * 100 + StartDate.Day;
            int endDate   = EndDate.Year * 10000 + EndDate.Month * 100 + EndDate.Day;

            LogItems = files
                       .Where(w => new Regex("^[0-9]{8}$").IsMatch(w))
                       .Select(s => Convert.ToInt32(s))
                       .Where(w => w >= startDate && w <= endDate)
                       .OrderByDescending(o => o)
                       .ToArray();

            return(Page());
        }
コード例 #2
0
        public IActionResult OnGet()
        {
            bool login = AuthHelper.Check(HttpContext);

            if (!login)
            {
                return(RedirectToPage("Login"));
            }

            string filePath = Path.Combine(AppContext.BaseDirectory, "logs", $"{Name}.txt");
            string text;

            using (var logFile = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
            {
                var    bytes  = new List <byte>();
                byte[] buffer = new byte[1024 * 1024 * 3];
                while (true)
                {
                    int length = logFile.Read(buffer, 0, buffer.Length);
                    if (length == 0)
                    {
                        break;
                    }
                    bytes.AddRange(buffer.Take(length));
                }
                text = System.Text.Encoding.UTF8.GetString(bytes.ToArray());
            }

            //string text = System.IO.File.ReadAllText(filePath);
            string[] list = text.Trim().Split(new[] { "-----End-----" }, StringSplitOptions.RemoveEmptyEntries);
            var      logs = new ConcurrentBag <LogModel>();

            Parallel.ForEach(list, item =>
            {
                var log        = new LogModel();
                string[] lines = item.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                if (lines.Length > 0)
                {
                    log.Time = Convert.ToDateTime(lines[0].Replace("-", ""));
                }
                if (lines.Length > 1)
                {
                    string[] categoryAndLogLevel = lines[1].Split(':');
                    if (categoryAndLogLevel.Length > 0)
                    {
                        log.Loglevel = categoryAndLogLevel[0].Trim();
                    }
                    if (categoryAndLogLevel.Length > 1)
                    {
                        log.Category = string.Join(string.Empty, categoryAndLogLevel.Skip(1));
                    }
                }
                if (lines.Length > 2)
                {
                    log.Content = string.Join(Environment.NewLine, lines.Skip(2));
                }
                logs.Add(log);
            });
            Logs = logs.OrderByDescending(o => o.Time).ToList();
            return(Page());
        }