sharee.bike-App/TINKLib/Model/Logging/LoggerConfigurationHelper.cs
Anja Müller-Meißner 0468955d49 Version 3.0.338
2022-09-08 09:55:14 +02:00

110 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using Serilog;
using Serilog.Configuration;
using Serilog.Formatting.Json;
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
{
// Max count of retained logs count.
private const int RETAINEDFILECOUNT = 10;
// Max size of logging file.
private const int MAXLOGFILESSIZEBYTESBYTES = 1024 * 1024 * 8; // 8MB
/// <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 = RETAINEDFILECOUNT)
{
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,
fileSizeLimitBytes: MAXLOGFILESSIZEBYTESBYTES / RETAINEDFILECOUNT,
/*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;
}
}
}