mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-16 15:16:34 +01:00
103 lines
3.9 KiB
C#
103 lines
3.9 KiB
C#
using Serilog;
|
|
using Serilog.Configuration;
|
|
using Serilog.Formatting.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
|
|
namespace TINK.Model.Logging
|
|
{
|
|
/// <summary> Holds new logging levels. </summary>
|
|
public enum RollingInterval
|
|
{
|
|
/// <summary> Create a new log file for each session (start of app).</summary>
|
|
Session,
|
|
}
|
|
|
|
/// <summary> Provides logging file name helper functionality.</summary>
|
|
public static class LoggerConfigurationHelper
|
|
{
|
|
/// <summary> Holds the log file name. </summary>
|
|
private static ILoggingDirectoryManager m_oDirectoryManager = new EmptyDirectoryLoggingManger();
|
|
|
|
/// <summary> Sets up logging to file.</summary>
|
|
/// <param name="p_oLoggerConfiguration">Object to set up logging with.</param>
|
|
/// <param name="p_oDevice">Object to get file informaton from.</param>
|
|
/// <param name="p_oRollingInterval">Specifies rolling type.</param>
|
|
/// <param name="p_iRetainedFilesCountLimit">Count of file being retained.</param>
|
|
/// <returns>Logger object.</returns>
|
|
public static LoggerConfiguration File(
|
|
this LoggerSinkConfiguration p_oLoggerConfiguration,
|
|
string p_strLogFileFolder,
|
|
RollingInterval p_oRollingInterval = RollingInterval.Session,
|
|
int p_iRetainedFilesCountLimit = 10)
|
|
{
|
|
if (m_oDirectoryManager is EmptyDirectoryLoggingManger)
|
|
{
|
|
// Roll file only once per app session.
|
|
try
|
|
{
|
|
m_oDirectoryManager = new LoggingDirectoryManager(
|
|
Directory.GetFiles,
|
|
Directory.Exists,
|
|
(path) => Directory.CreateDirectory(path),
|
|
System.IO.File.Delete,
|
|
p_strLogFileFolder,
|
|
Path.DirectorySeparatorChar,
|
|
p_iRetainedFilesCountLimit);
|
|
}
|
|
catch (Exception l_oException)
|
|
{
|
|
Log.Error("Log directory manager could not be instanciated successfully. {@l_oException}", l_oException);
|
|
m_oDirectoryManager = new EmptyDirectoryLoggingManger();
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
m_oDirectoryManager.DeleteObsoleteLogs();
|
|
}
|
|
catch (Exception l_oException)
|
|
{
|
|
Log.Error("Not all obsolte log files could be deleted successfully. {@l_oException}", l_oException);
|
|
}
|
|
|
|
if (p_oLoggerConfiguration == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return p_oLoggerConfiguration.File(
|
|
new JsonFormatter(),
|
|
m_oDirectoryManager.LogFileName,
|
|
/*shared: true, // Leads to exception if activated.*/
|
|
rollingInterval: Serilog.RollingInterval.Infinite,
|
|
retainedFileCountLimit: p_iRetainedFilesCountLimit);
|
|
}
|
|
|
|
/// <summary> Gets all log files in logging directory. </summary>
|
|
/// <param name="p_oLogger"></param>
|
|
/// <returns>List of log files.</returns>
|
|
public static IList<string> GetLogFiles(this ILogger p_oLogger)
|
|
{
|
|
try
|
|
{
|
|
return m_oDirectoryManager.GetLogFiles();
|
|
}
|
|
catch (Exception l_oException)
|
|
{
|
|
Log.Error("Getting list of log files failed. Empty list is returned instead. {@l_oException}", l_oException);
|
|
return new List<string>();
|
|
}
|
|
}
|
|
|
|
/// <summary> Gets path where log files are located. </summary>
|
|
/// <param name="p_oLogger"></param>
|
|
/// <returns>List of log files.</returns>
|
|
public static string GetLogFilePath(
|
|
this ILogger p_oLogger)
|
|
{
|
|
return m_oDirectoryManager.LogFilePath;
|
|
}
|
|
}
|
|
}
|