using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tango.Logging
{
public class SessionFileLogger : ILogger
{
private bool _inInSession;
private int _writeCount;
private const string FILE_SESSION_EXTENSION = "_session";
private const int FILE_SIZE_CHECK_COUNT = 100;
#region Static Properties
///
/// Gets the default logs folder.
///
public static String DefaultLogsFolder { get; private set; }
#endregion
#region Properties
public bool Enabled { get; set; }
///
/// Folder is used for save file session logs
///
public String Folder { get; private set; }
///
/// Full path of log file
///
public String LogFile { get; private set; }
///
/// Gets the tag name which will be appended to the file.
///
public String Tag { get; private set; }
///
/// Gets or sets the maximum file size limit.
///
public long MaxFileSizeLimit { get; set; }
#endregion
#region Constructors
///
/// Initializes the static members of the class.
///
static SessionFileLogger()
{
DefaultLogsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Logs", Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName), "Session");
}
public SessionFileLogger(String folder, String tag)
{
Folder = folder;
Tag = tag;
Directory.CreateDirectory(Folder);
Enabled = true;
_writeCount = 0;
MaxFileSizeLimit = 1000000 * 10; //10 MB
}
public SessionFileLogger() : this(DefaultLogsFolder, Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName))
{
}
#endregion
#region Public Methods
///
/// Creates the session. Only one file should be for session log, so removed old file and create a new file.
///
public void CreateSession()
{
_writeCount = 0;
RemoveOldLogFile();
LogFile = CreateLogFileName();
_inInSession = true;
}
///
/// Ends the session. Set flag _inInSession to false.
///
public void EndSession()
{
_inInSession = false;
}
///
/// Called when write to session log file.
///
/// The output.
public void OnLog(LogItemBase output)
{
if (_inInSession)
{
try
{
if (!File.Exists(LogFile) || (++_writeCount > FILE_SIZE_CHECK_COUNT && new FileInfo(LogFile).Length > MaxFileSizeLimit))
{
CreateSession();
}
File.AppendAllText(LogFile, output.ToString() + Environment.NewLine);
}
catch (Exception ex)
{
Debug.WriteLine($"Error Writing To Session Log File!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n{ex.ToString()}");
}
}
}
#endregion
#region Private Methods
///
/// Creates the name of the log file.
///
///
private String CreateLogFileName()
{
return Path.Combine(Folder, string.Format("{1}-{0:dd-MM-yyyy_HH-mm-ss}{2}.log", DateTime.Now, Tag, FILE_SESSION_EXTENSION));
}
///
/// Delete the old log file if is existed.
///
private void RemoveOldLogFile()
{
try
{
if (Directory.Exists(Folder))
{
string[] fileEntries = Directory.GetFiles(Folder, "*.log");
foreach (string fileName in fileEntries)
{
try
{
File.Delete(fileName);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
#endregion
}
}