示例#1
0
    public async Task <List <LaobianLog> > GetLogsAsync(LaobianSite site, DateTime date,
                                                        CancellationToken cancellationToken = default)
    {
        var result  = new List <LaobianLog>();
        var logFile =
            (await _logFileRepository.SearchFilesAsync(
                 Path.Combine(site.ToString().ToLowerInvariant(), date.Year.ToString("D4"), $"{date.ToDate()}.log"),
                 cancellationToken: cancellationToken)).FirstOrDefault();

        if (!string.IsNullOrEmpty(logFile))
        {
            var logs = await _logFileRepository.ReadAsync(logFile, cancellationToken);

            if (!string.IsNullOrEmpty(logs))
            {
                using var sr = new StringReader(logs);
                string line;
                while ((line = await sr.ReadLineAsync()) != null)
                {
                    result.Add(JsonUtil.Deserialize <LaobianLog>(line));
                }
            }
        }

        return(result);
    }
    private async Task <List <LaobianLog> > ReadLogsAsync(LaobianSite site, int days, int minLevel)
    {
        var result = new List <LaobianLog>();

        for (var i = 0; i <= days; i++)
        {
            var date = DateTime.Now.AddDays(-i);
            var logs = await _logFileService.GetLogsAsync(site, date);

            result.AddRange(logs.Where(x => (int)x.Level >= minLevel));
        }

        return(result);
    }