2021-05-13 20:03:07 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
namespace TINK.Model.Logging
{
public class LoggingDirectoryManager : ILoggingDirectoryManager
{
/// <summary> Name of logging subdirectory.</summary>
private const string LOGDIRECTORYTITLE = "Log" ;
/// <summary> Prevents an invalid instance to be created. </summary>
private LoggingDirectoryManager ( ) { }
public LoggingDirectoryManager (
2022-08-30 15:42:25 +02:00
Func < string , IList < string > > fileListProvider ,
Func < string , bool > directoryExistsChecker ,
Action < string > directoryCreator ,
Action < string > fileEraser ,
string logFilePath ,
char directorySeparatorChar ,
2021-05-13 20:03:07 +02:00
int p_iRetainedFilesCountLimit )
{
2022-08-30 15:42:25 +02:00
m_oFileListProvider = fileListProvider ? ? throw new ArgumentException ( $"Can not instantiate {nameof(LoggingDirectoryManager)}- object. File list provider delegate can not be null." ) ;
2021-05-13 20:03:07 +02:00
2022-08-30 15:42:25 +02:00
if ( directoryExistsChecker = = null )
2021-05-13 20:03:07 +02:00
{
throw new ArgumentException ( $"Can not instantiate {nameof(LoggingDirectoryManager)}- object. Directory existance checker delegate can not be null." ) ;
}
2022-08-30 15:42:25 +02:00
if ( directoryCreator = = null )
2021-05-13 20:03:07 +02:00
{
throw new ArgumentException ( $"Can not instantiate {nameof(LoggingDirectoryManager)}- object. Directory creator delegate can not be null." ) ;
}
2022-08-30 15:42:25 +02:00
m_oFileEraser = fileEraser ? ? throw new ArgumentException ( $"Can not instantiate {nameof(LoggingDirectoryManager)}- object. File eraser delegate can not be null." ) ;
2021-05-13 20:03:07 +02:00
2022-08-30 15:42:25 +02:00
if ( string . IsNullOrEmpty ( logFilePath ) )
2021-05-13 20:03:07 +02:00
{
throw new ArgumentException ( $"Can not instantiate {nameof(LoggingDirectoryManager)}- object. Log file path can not be null or empty." ) ;
}
2022-08-30 15:42:25 +02:00
if ( string . IsNullOrEmpty ( directorySeparatorChar . ToString ( ) ) )
2021-05-13 20:03:07 +02:00
{
throw new ArgumentException ( $"Can not instantiate {nameof(LoggingDirectoryManager)}- object. Directory separtor character can not be null or empty." ) ;
}
if ( p_iRetainedFilesCountLimit < 1 )
{
throw new ArgumentException ( $"Can not instantiate {nameof(LoggingDirectoryManager)}- object. Count of retained log files is {p_iRetainedFilesCountLimit} but must be equal or larger one." ) ;
}
2022-08-30 15:42:25 +02:00
DirectorySeparatorChar = directorySeparatorChar . ToString ( ) ;
2021-05-13 20:03:07 +02:00
2022-08-30 15:42:25 +02:00
LogFilePath = $"{logFilePath}{DirectorySeparatorChar}{LOGDIRECTORYTITLE}" ;
2021-05-13 20:03:07 +02:00
m_iRetainedFilesCountLimit = p_iRetainedFilesCountLimit ;
if ( string . IsNullOrEmpty ( LogFileTitle ) )
{
2021-06-26 20:57:55 +02:00
LogFileTitle = $"{DateTime.Now:yyyy_MM_dd_HH_mm_ss}.jsnl" ;
2021-05-13 20:03:07 +02:00
}
// Create directory if direcotry does not exist.
2022-08-30 15:42:25 +02:00
if ( directoryExistsChecker ( LogFilePath ) = = false )
2021-05-13 20:03:07 +02:00
{
try
{
2022-08-30 15:42:25 +02:00
directoryCreator ( LogFilePath ) ;
2021-05-13 20:03:07 +02:00
}
catch ( Exception l_oException )
{
throw new FileOperationException ( $"Logging directory {LogFilePath} could not be created successfully." , l_oException ) ;
}
}
}
/// <summary> Deletes files which are out of retainment scope. </summary>
public void DeleteObsoleteLogs ( )
{
var l_oExceptions = new List < Exception > ( ) ;
var l_oSortedFileArray = m_oFileListProvider ( LogFilePath ) . OrderByDescending ( x = > x ) . ToArray ( ) ;
2022-08-30 15:42:25 +02:00
for ( int l_iIndex = l_oSortedFileArray . Length - 1 ;
l_iIndex > = m_iRetainedFilesCountLimit - 1 ; /* files remaining count must be m_iRetainedFilesCountLimit - 1 because new log file will be added afterwards */
l_iIndex - - )
2021-05-13 20:03:07 +02:00
{
try
{
m_oFileEraser ( l_oSortedFileArray [ l_iIndex ] ) ;
}
catch ( Exception l_oExpetion )
{
// Getting list of log files found.
l_oExceptions . Add ( l_oExpetion ) ;
}
}
if ( l_oExceptions . Count < = 0 )
{
return ;
}
throw new AggregateException ( "Deleting obsolete log files failed." , l_oExceptions . ToArray ( ) ) ;
}
/// <summary> Gets all log files in logging directory. </summary>
/// <param name="p_oLogger"></param>
/// <returns>List of log files.</returns>
public IList < string > GetLogFiles ( )
{
try
{
return m_oFileListProvider ( LogFilePath ) . OrderBy ( x = > x ) . ToList ( ) ;
}
catch ( Exception l_oExpetion )
{
// Getting list of log files found.
throw new FileOperationException ( "Getting list of log files failed." , l_oExpetion ) ;
}
}
/// <summary>Holds delegate to provide file names.</summary>
private readonly Func < string , IList < string > > m_oFileListProvider ;
/// <summary>Holds delegate to delete files.</summary>
private readonly Action < string > m_oFileEraser ;
/// <summary>Holds delegate to provide file names.</summary>
private int m_iRetainedFilesCountLimit ;
/// <summary> Holds the log file name. </summary>
private string LogFileTitle { get ; }
/// <summary> Holds the log file name. </summary>
public string LogFilePath { get ; }
/// <summary> Holds the directory separator character. </summary>
private string DirectorySeparatorChar { get ; }
/// <summary> Holds the log file name. </summary>
public string LogFileName { get { return $"{LogFilePath}{DirectorySeparatorChar}{LogFileTitle}" ; } }
}
}