Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace WebApi.Pages.Services
- {
- using Domain;
- using System;
- using System.IO;
- using System.Linq;
- using WebApi.Pages.Models;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- using Microsoft.Extensions.FileProviders;
- public interface IRecordService
- {
- IEnumerable<string> GetLogFiles(string serviceDir);
- bool DeleteLog(string serviceDir, string fileName);
- IEnumerable<Service> GetServices();
- ICollection<Log> GetLogs(string dir, string fileName);
- }
- public class RecordsService:IRecordService
- {
- private string DirectoryRoute => @"c:\logs\MicroServices\";
- private IFileProvider provider => new PhysicalFileProvider(DirectoryRoute);
- private string apiName => Constants.ApiName;
- public IEnumerable<string> GetLogFiles(string serviceDir)
- {
- string path = $"{apiName}\\{serviceDir}";
- var files = this.provider.GetDirectoryContents(path);
- var currentLogs = files.Select(x => x.Name).OrderByDescending(x => x);
- return currentLogs;
- }
- public IEnumerable<Service> GetServices()
- {
- var directories = this.provider.GetDirectoryContents(apiName);
- var services = directories.Select(x => new Service
- {
- ServiceName = x.Name
- });
- return services;
- }
- public bool DeleteLog(string dir, string fileName)
- {
- string path = $"{this.DirectoryRoute}{apiName}\\{dir}\\{fileName}";
- var fileExists = File.Exists(path);
- if (fileExists)
- {
- File.Delete(path);
- return true;
- }
- return false;
- }
- public ICollection<Log> GetLogs(string dir, string fileName)
- {
- List<Log> currentLogs = new List<Log>();
- string path = $"{apiName}\\{dir}";
- var files = this.provider.GetDirectoryContents(path);
- var f = files.FirstOrDefault(x => x.Name == fileName);
- string content;
- using (FileStream fileStream = new FileStream(f.PhysicalPath,
- FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
- {
- using (StreamReader streamReader = new StreamReader(fileStream))
- {
- content = streamReader.ReadToEnd();
- }
- }
- var logs = GetLogsPerFile(content);
- currentLogs.AddRange(logs);
- return currentLogs;
- }
- private ICollection<Log> GetLogsPerFile(string content)
- {
- ICollection<Log> fileLogs = new List<Log>();
- using (StringReader reader = new StringReader(content))
- {
- var readenContent = reader.ReadToEnd();
- string[] LogArray = readenContent.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
- Array.Reverse(LogArray);
- foreach (var item in LogArray)
- {
- if (string.IsNullOrEmpty(item))
- {
- continue;
- }
- Regex regex = new Regex(@"^(\d){4}.*");
- Match match = regex.Match(item);
- if (match.Success)
- {
- var currentLogPieces = item.Split(" ", StringSplitOptions.RemoveEmptyEntries);
- var log = new Log();
- var date = DateTime.Parse(currentLogPieces[0]);
- var timeSpan = TimeSpan.Parse(currentLogPieces[1]);
- date = date.Add(timeSpan);
- log.CreatedOn = date;
- log.LogType = currentLogPieces[3];
- string message = string.Join(" ", currentLogPieces.Skip(4));
- log.Message = message;
- fileLogs.Add(log);
- continue;
- }
- var logToUpdate = fileLogs.Last();
- logToUpdate.Message = logToUpdate.Message.Insert(0, item);
- }
- }
- return fileLogs;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment