mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-10 20:46:33 +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 DirectoryManager { get; set; } = new EmptyDirectoryLoggingManger();
|
|
|
|
/// <summary> Sets up logging to file.</summary>
|
|
/// <param name="loggerConfiguration">Object to set up logging with.</param>
|
|
/// <param name="p_oDevice">Object to get file informaton from.</param>
|
|
/// <param name="rollingInterval">Specifies rolling type.</param>
|
|
/// <param name="retainedFilesCountLimit">Count of file being retained.</param>
|
|
/// <returns>Logger object.</returns>
|
|
public static LoggerConfiguration File(
|
|
this LoggerSinkConfiguration loggerConfiguration,
|
|
string logFileFolder,
|
|
RollingInterval rollingInterval = RollingInterval.Session,
|
|
int retainedFilesCountLimit = 10)
|
|
{
|
|
if (DirectoryManager is EmptyDirectoryLoggingManger)
|
|
{
|
|
// Roll file only once per app session.
|
|
try
|
|
{
|
|
DirectoryManager = new LoggingDirectoryManager(
|
|
Directory.GetFiles,
|
|
Directory.Exists,
|
|
(path) => Directory.CreateDirectory(path),
|
|
System.IO.File.Delete,
|
|
logFileFolder,
|
|
Path.DirectorySeparatorChar,
|
|
retainedFilesCountLimit);
|
|
}
|
|
catch (Exception l_oException)
|
|
{
|
|
Log.Error("Log directory manager could not be instanciated successfully. {@l_oException}", l_oException);
|
|
DirectoryManager = new EmptyDirectoryLoggingManger();
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
DirectoryManager.DeleteObsoleteLogs();
|
|
}
|
|
catch (Exception l_oException)
|
|
{
|
|
Log.Error("Not all obsolte log files could be deleted successfully. {@l_oException}", l_oException);
|
|
}
|
|
|
|
if (loggerConfiguration == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return loggerConfiguration.File(
|
|
new JsonFormatter(),
|
|
DirectoryManager.LogFileName,
|
|
/*shared: true, // Leads to exception if activated.*/
|
|
rollingInterval: Serilog.RollingInterval.Infinite,
|
|
retainedFileCountLimit: retainedFilesCountLimit);
|
|
}
|
|
|
|
/// <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 DirectoryManager.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 DirectoryManager.LogFilePath;
|
|
}
|
|
}
|
|
}
|