sharee.bike-App/TINKLib/Model/Logging/LoggerConfigurationHelper.cs

104 lines
3.9 KiB
C#
Raw Normal View History

2021-05-13 20:03:07 +02:00
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>
2021-06-26 20:57:55 +02:00
private static ILoggingDirectoryManager DirectoryManager { get; set; } = new EmptyDirectoryLoggingManger();
2021-05-13 20:03:07 +02:00
/// <summary> Sets up logging to file.</summary>
2021-06-26 20:57:55 +02:00
/// <param name="loggerConfiguration">Object to set up logging with.</param>
2021-05-13 20:03:07 +02:00
/// <param name="p_oDevice">Object to get file informaton from.</param>
2021-06-26 20:57:55 +02:00
/// <param name="rollingInterval">Specifies rolling type.</param>
/// <param name="retainedFilesCountLimit">Count of file being retained.</param>
2021-05-13 20:03:07 +02:00
/// <returns>Logger object.</returns>
public static LoggerConfiguration File(
2021-06-26 20:57:55 +02:00
this LoggerSinkConfiguration loggerConfiguration,
string logFileFolder,
RollingInterval rollingInterval = RollingInterval.Session,
int retainedFilesCountLimit = 10)
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
if (DirectoryManager is EmptyDirectoryLoggingManger)
2021-05-13 20:03:07 +02:00
{
// Roll file only once per app session.
try
{
2021-06-26 20:57:55 +02:00
DirectoryManager = new LoggingDirectoryManager(
2021-05-13 20:03:07 +02:00
Directory.GetFiles,
Directory.Exists,
(path) => Directory.CreateDirectory(path),
System.IO.File.Delete,
2021-06-26 20:57:55 +02:00
logFileFolder,
2021-05-13 20:03:07 +02:00
Path.DirectorySeparatorChar,
2021-06-26 20:57:55 +02:00
retainedFilesCountLimit);
2021-05-13 20:03:07 +02:00
}
catch (Exception l_oException)
{
Log.Error("Log directory manager could not be instanciated successfully. {@l_oException}", l_oException);
2021-06-26 20:57:55 +02:00
DirectoryManager = new EmptyDirectoryLoggingManger();
2021-05-13 20:03:07 +02:00
}
}
try
{
2021-06-26 20:57:55 +02:00
DirectoryManager.DeleteObsoleteLogs();
2021-05-13 20:03:07 +02:00
}
catch (Exception l_oException)
{
Log.Error("Not all obsolte log files could be deleted successfully. {@l_oException}", l_oException);
}
2021-06-26 20:57:55 +02:00
if (loggerConfiguration == null)
2021-05-13 20:03:07 +02:00
{
return null;
}
2021-06-26 20:57:55 +02:00
return loggerConfiguration.File(
2021-05-13 20:03:07 +02:00
new JsonFormatter(),
2021-06-26 20:57:55 +02:00
DirectoryManager.LogFileName,
2021-05-13 20:03:07 +02:00
/*shared: true, // Leads to exception if activated.*/
rollingInterval: Serilog.RollingInterval.Infinite,
2021-06-26 20:57:55 +02:00
retainedFileCountLimit: retainedFilesCountLimit);
2021-05-13 20:03:07 +02:00
}
/// <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
{
2021-06-26 20:57:55 +02:00
return DirectoryManager.GetLogFiles();
2021-05-13 20:03:07 +02:00
}
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)
{
2021-06-26 20:57:55 +02:00
return DirectoryManager.LogFilePath;
2021-05-13 20:03:07 +02:00
}
}
}