Aliendreamer

sexy clear code

Mar 14th, 2019
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | None | 0 0
  1. namespace WebApi.Pages.Services
  2. {
  3.     using Domain;
  4.     using System;
  5.     using System.IO;
  6.     using System.Linq;
  7.     using WebApi.Pages.Models;
  8.     using System.Collections.Generic;
  9.     using System.Text.RegularExpressions;
  10.     using Microsoft.Extensions.FileProviders;
  11.  
  12.     public interface IRecordService
  13.     {
  14.         IEnumerable<string> GetLogFiles(string serviceDir);
  15.         bool DeleteLog(string serviceDir, string fileName);
  16.         IEnumerable<Service> GetServices();
  17.         ICollection<Log> GetLogs(string dir, string fileName);
  18.     }
  19.  
  20.     public class RecordsService:IRecordService
  21.     {
  22.      
  23.         private string DirectoryRoute => @"c:\logs\MicroServices\";
  24.         private IFileProvider provider => new PhysicalFileProvider(DirectoryRoute);
  25.        
  26.         private string apiName => Constants.ApiName;
  27.  
  28.         public IEnumerable<string> GetLogFiles(string serviceDir)
  29.         {
  30.             string path = $"{apiName}\\{serviceDir}";
  31.             var files = this.provider.GetDirectoryContents(path);
  32.  
  33.             var currentLogs = files.Select(x => x.Name).OrderByDescending(x => x);
  34.  
  35.             return currentLogs;
  36.         }
  37.      
  38.         public IEnumerable<Service> GetServices()
  39.         {
  40.             var directories = this.provider.GetDirectoryContents(apiName);
  41.             var services = directories.Select(x => new Service
  42.             {
  43.                 ServiceName = x.Name
  44.             });
  45.  
  46.             return services;
  47.         }
  48.  
  49.         public bool DeleteLog(string dir, string fileName)
  50.         {
  51.             string path = $"{this.DirectoryRoute}{apiName}\\{dir}\\{fileName}";
  52.             var fileExists = File.Exists(path);
  53.             if (fileExists)
  54.             {
  55.                 File.Delete(path);
  56.                 return true;
  57.             }
  58.             return false;
  59.         }
  60.  
  61.         public ICollection<Log> GetLogs(string dir, string fileName)
  62.         {
  63.             List<Log> currentLogs = new List<Log>();
  64.             string path = $"{apiName}\\{dir}";
  65.             var files = this.provider.GetDirectoryContents(path);          
  66.             var f = files.FirstOrDefault(x => x.Name == fileName);
  67.             string content;
  68.             using (FileStream fileStream = new FileStream(f.PhysicalPath,
  69.                    FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  70.             {
  71.                 using (StreamReader streamReader = new StreamReader(fileStream))
  72.                 {
  73.                     content = streamReader.ReadToEnd();
  74.                 }
  75.             }
  76.             var logs = GetLogsPerFile(content);
  77.             currentLogs.AddRange(logs);
  78.  
  79.             return currentLogs;
  80.         }
  81.  
  82.         private ICollection<Log> GetLogsPerFile(string content)
  83.         {
  84.             ICollection<Log> fileLogs = new List<Log>();
  85.  
  86.             using (StringReader reader = new StringReader(content))
  87.             {
  88.                 var readenContent = reader.ReadToEnd();
  89.                 string[] LogArray = readenContent.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
  90.                 Array.Reverse(LogArray);
  91.                 foreach (var item in LogArray)
  92.                 {
  93.                     if (string.IsNullOrEmpty(item))
  94.                     {
  95.                         continue;
  96.                     }
  97.  
  98.                     Regex regex = new Regex(@"^(\d){4}.*");
  99.                     Match match = regex.Match(item);
  100.                     if (match.Success)
  101.                     {
  102.                         var currentLogPieces = item.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  103.                         var log = new Log();
  104.                         var date = DateTime.Parse(currentLogPieces[0]);
  105.                         var timeSpan = TimeSpan.Parse(currentLogPieces[1]);
  106.                         date = date.Add(timeSpan);
  107.                         log.CreatedOn = date;
  108.                         log.LogType = currentLogPieces[3];
  109.                         string message = string.Join(" ", currentLogPieces.Skip(4));
  110.                         log.Message = message;
  111.  
  112.                         fileLogs.Add(log);
  113.                         continue;
  114.                     }
  115.                     var logToUpdate = fileLogs.Last();
  116.                     logToUpdate.Message = logToUpdate.Message.Insert(0, item);
  117.                 }
  118.             }
  119.             return fileLogs;
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment