Added sysLogServer functionality

This commit is contained in:
2022-12-15 15:02:10 +01:00
parent 7783dc07e1
commit f8659ad197
17 changed files with 1008 additions and 169 deletions

View File

@@ -16,20 +16,20 @@ namespace EonaCat.Logger.Extensions
/// Adds a file logger named 'File' to the factory.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
public static ILoggingBuilder AddFile(this ILoggingBuilder builder)
public static ILoggingBuilder AddEonaCatFileLogger(this ILoggingBuilder builder)
{
builder.Services.AddSingleton<ILoggerProvider, FileLoggerProvider>();
return builder;
}
/// <summary>
/// Adds a file logger named 'File' to the factory.
/// Adds the EonaCat File Logger named 'EonaCatFileLogger' to the factory.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
/// <param name="filenamePrefix">Sets the filename prefix to use for log files</param>
public static ILoggingBuilder AddFile(this ILoggingBuilder builder, string filenamePrefix)
public static ILoggingBuilder AddEonaCatFileLogger(this ILoggingBuilder builder, string filenamePrefix)
{
builder.AddFile(options => options.FileNamePrefix = filenamePrefix);
builder.AddEonaCatFileLogger(options => options.FileNamePrefix = filenamePrefix);
return builder;
}
@@ -38,13 +38,13 @@ namespace EonaCat.Logger.Extensions
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
/// <param name="configure">Configure an instance of the <see cref="FileLoggerOptions" /> to set logging options</param>
public static ILoggingBuilder AddFile(this ILoggingBuilder builder, Action<FileLoggerOptions> configure)
public static ILoggingBuilder AddEonaCatFileLogger(this ILoggingBuilder builder, Action<FileLoggerOptions> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
builder.AddFile();
builder.AddEonaCatFileLogger();
builder.Services.Configure(configure);
return builder;

View File

@@ -15,6 +15,7 @@ namespace EonaCat.Logger
private int _fileSizeLimit = 200 * 1024 * 1024;
private int _retainedFileCountLimit = 50;
private int _maxRolloverFiles = 10;
private int _maxTries = 3;
public static string DefaultPath => AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
@@ -55,6 +56,20 @@ namespace EonaCat.Logger
}
}
/// <summary>
/// Gets or sets the max times to try to write to the file.
/// Defaults 3.
/// </summary>
public int MaxWriteTries
{
get => _maxTries;
set
{
_maxTries = value;
}
}
/// <summary>
/// Gets or sets a strictly positive value representing the maximum retained file rollovers or null for no limit.
/// Defaults to <c>10</c>.

View File

@@ -1,9 +1,11 @@
using EonaCat.Logger.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime;
using System.Threading;
using System.Threading.Tasks;
@@ -23,9 +25,37 @@ namespace EonaCat.Logger
private readonly int _maxFileSize;
private readonly int _maxRetainedFiles;
private readonly int _maxRolloverFiles;
private readonly int _maxTries;
private int _rollOverCount = 0;
private static readonly object _writeLock = new object();
private string _logFile;
public string LogFile { get; private set; }
/// <summary>
/// The file to which log messages should be appended.
/// </summary>
public string LogFile
{
get
{
return _logFile;
}
set
{
_logFile = value;
if (!string.IsNullOrEmpty(_logFile))
{
string dir = Path.GetDirectoryName(_logFile);
if (!string.IsNullOrEmpty(dir))
{
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
}
}
}
}
/// <summary>
/// Creates an instance of the <see cref="FileLoggerProvider" />
@@ -39,6 +69,7 @@ namespace EonaCat.Logger
_maxFileSize = loggerOptions.FileSizeLimit;
_maxRetainedFiles = loggerOptions.RetainedFileCountLimit;
_maxRolloverFiles = loggerOptions.MaxRolloverFiles;
_maxTries = loggerOptions.MaxWriteTries;
}
/// <inheritdoc />
@@ -77,11 +108,31 @@ namespace EonaCat.Logger
}
}
using (StreamWriter streamWriter = File.AppendText(LogFile))
lock (_writeLock)
{
foreach (LogMessage item in group)
int tries = 0;
bool completed = false;
while (!completed)
{
await streamWriter.WriteAsync(item.Message);
try
{
System.IO.StreamWriter file = new System.IO.StreamWriter(LogFile, true);
foreach (LogMessage item in group)
{
file.WriteAsync(item.Message);
}
file.Close();
completed = true;
}
catch (Exception exc)
{
tries++;
Task.Delay(100);
if (tries >= _maxTries)
throw;
}
}
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using EonaCat.logger.Managers;
using Microsoft.Extensions.Logging;
using System;
using System.Text;
@@ -25,35 +26,21 @@ namespace EonaCat.Logger.Internal
public bool IsEnabled(LogLevel logLevel)
{
if (logLevel == LogLevel.None)
{
return false;
}
return true;
return logLevel != LogLevel.None;
}
public void Log<TState>(DateTimeOffset timestamp, LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
if (!IsEnabled(logLevel)) return;
StringBuilder builder = new StringBuilder();
builder.Append(timestamp.ToString("yyyy-MM-dd HH:mm:ss.fff zzz"));
builder.Append(" [");
builder.Append(logLevel.ToString());
builder.Append("] ");
builder.Append(_category);
builder.Append(": ");
builder.AppendLine(formatter(state, exception));
var message = LogHelper.FormatMessageWithHeader(new Managers.LoggerSettings(), logLevel.FromLogLevel(), formatter(state, exception)) + Environment.NewLine;
if (exception != null)
{
builder.AppendLine(exception.ToString());
{
message = exception.FormatExceptionToMessage() + Environment.NewLine;
}
_provider.AddMessage(timestamp, builder.ToString());
_provider.AddMessage(timestamp, message);
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)

View File

@@ -90,7 +90,7 @@ namespace EonaCat.Logger.Internal
}
catch
{
//cancellation token canceled or CompleteAdding called
// cancellation token canceled or CompleteAdding called
}
}
}
@@ -128,6 +128,12 @@ namespace EonaCat.Logger.Internal
public void Dispose()
{
Stop();
GC.SuppressFinalize(this);
}
~BatchingLoggerProvider()
{
Dispose();
}
public ILogger CreateLogger(string categoryName)

View File

@@ -9,13 +9,13 @@ namespace EonaCatLogger.EonaCatCoreLogger.Models
public class EonaCatLogMessage
{
public DateTime DateTime { get; internal set; }
public string Message { get; internal set; }
public ELogType LogType { get; internal set; }
public DateTime DateTime { get; set; }
public string Message { get; set; }
public ELogType LogType { get; set; }
public override string ToString()
{
return $"[{EnumHelper<ELogType>.ToString(LogType)}] [{DateTime.ToString(Constants.DateTimeFormats.LOGGING)}] {Message}";
return $"[{DateTime.ToString(Constants.DateTimeFormats.LOGGING)}] [{EnumHelper<ELogType>.ToString(LogType)}] {Message}";
}
}
}