Skip to content

Commit

Permalink
Logging single thread implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
bberka committed Feb 14, 2023
1 parent 06b8302 commit fbb970c
Show file tree
Hide file tree
Showing 71 changed files with 354 additions and 300 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified .vs/EasMe/v17/.suo
Binary file not shown.
74 changes: 0 additions & 74 deletions src/EasMe.Logging/AsyncThreadLogger.cs

This file was deleted.

86 changes: 42 additions & 44 deletions src/EasMe.Logging/EasLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,30 @@
using System;
using EasMe.Logging.Models;
using Microsoft.Extensions.Hosting;
using log4net;

namespace EasMe.Logging
{


//private static readonly EasLog logger = IEasLog.CreateLogger(typeof(AdminManager).Namespace + "." + typeof(AdminManager).Name);
/// <summary>
/// Simple static json and console logger with useful options.
/// </summary>
public sealed class EasLog : AsyncThreadLogger<LogModel>
{
//internal static EasLogConfiguration Configuration { get; set; } = IEasLog.Config;
//private static int? _OverSizeExt = 0;
public class EasLog : IEasLog
{

//internal static bool _IsCreated = false;
internal string _LogSource;
private static readonly object _fileLock = new();
internal EasLog(string source)

private static readonly EasTask EasTask = new ();
private static readonly List<Exception> _exceptions = new();
public static IReadOnlyCollection<Exception> Exceptions => _exceptions;

internal EasLog(string source)
{
_LogSource = source;
}
internal EasLog()
{
_LogSource = "Sys";
}


private static string GetExactLogPath(LogSeverity severity)
{
var date = DateTime.Now.ToString(EasLogFactory.Config.DateFormatString);
Expand Down Expand Up @@ -475,50 +473,50 @@ public void Trace(object obj1, object obj2, object obj3, object obj4, object obj
if (!LogSeverity.TRACE.IsLoggable()) return;
WriteLog(_LogSource, LogSeverity.TRACE, null, obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8);
}


private void WriteLog(string source, LogSeverity severity, Exception? exception = null, params object[] param)
{
var paramToLog = param.ToLogString();
var model = new LogModel(severity, source, paramToLog, exception);
LogMessage(model);
}


public static List<Exception> Errors { get; } = new();
public void Flush()
{
EasTask.Flush();
}



protected override void AsyncLogMessage(LogModel log)
private static void WriteLog(string source, LogSeverity severity, Exception? exception = null, params object[] param)
{
if (EasLogFactory.Config.ConsoleAppender) EasLogConsole.Log(log.Severity, log.ToJsonString() ?? "");
var logFilePath = GetExactLogPath(log.Severity);
try
var loggingAction = new Action(() =>
{

var folderPath = Path.GetDirectoryName(logFilePath);
if (folderPath is not null)
var paramToLog = param.ToLogString();
var log = new LogModel(severity, source, paramToLog, exception);
if (EasLogFactory.Config.ConsoleAppender) EasLogConsole.Log(log.Severity, log.ToJsonString() ?? "");
var logFilePath = GetExactLogPath(log.Severity);
try
{
if (!Directory.Exists(folderPath))

var folderPath = Path.GetDirectoryName(logFilePath);
if (folderPath is not null)
{
Directory.CreateDirectory(folderPath);
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
}

lock (_fileLock)
{
File.AppendAllText(logFilePath, log.ToJsonString() + "\n");
}
}
lock (_fileLock)
{
File.AppendAllText(logFilePath, log.ToJsonString() + "\n");
}
}
catch (Exception ex)
{
lock (Errors)
catch (Exception ex)
{
Errors.Add(ex);
lock (_exceptions)
{
_exceptions.Add(ex);
}
}
}


});
EasTask.AddToQueue(loggingAction);
}
}


}

5 changes: 4 additions & 1 deletion src/EasMe.Logging/EasLogConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ namespace EasMe.Logging
{
public class EasLogConfiguration
{

internal EasLogConfiguration()
{

}
/// <summary>
/// Gets or sets a value indicating whether to log the request body.
/// </summary>
Expand Down
41 changes: 27 additions & 14 deletions src/EasMe.Logging/EasLogConsole.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using EasMe.Enums;
using EasMe.Extensions;
using EasMe.Logging.Internal;
using System.Reflection.Metadata;

namespace EasMe.Logging
{
Expand All @@ -11,14 +12,14 @@ namespace EasMe.Logging
/// </summary>
public static class EasLogConsole
{
private static ConsoleColor FatalColor = ConsoleColor.DarkMagenta;
private static ConsoleColor ErrorColor = ConsoleColor.Red;
private static ConsoleColor BaseColor = ConsoleColor.White;
private static ConsoleColor WarningColor = ConsoleColor.Yellow;
private static ConsoleColor InfoColor = ConsoleColor.Green;
private static ConsoleColor DebugColor = ConsoleColor.Blue;
private static ConsoleColor TraceColor = ConsoleColor.Cyan;
private static ConsoleColor ExceptionColor = ConsoleColor.Magenta;
private const ConsoleColor FatalColor = ConsoleColor.DarkMagenta;
private const ConsoleColor ErrorColor = ConsoleColor.Red;
private const ConsoleColor BaseColor = ConsoleColor.White;
private const ConsoleColor WarningColor = ConsoleColor.Yellow;
private const ConsoleColor InfoColor = ConsoleColor.Green;
private const ConsoleColor DebugColor = ConsoleColor.Blue;
private const ConsoleColor TraceColor = ConsoleColor.Cyan;
private const ConsoleColor ExceptionColor = ConsoleColor.Magenta;
public static void Log(string message)
{
Console.WriteLine(message);
Expand Down Expand Up @@ -77,22 +78,34 @@ public static void Log(LogSeverity severity, string message)
break;
}
}
public static void Log(LogSeverity severity, string message, params string[] param)
public static void Log(LogSeverity severity, string message, params object[] param)
{
var paramStr = param.ToLogString(severity);
var logStr = param.ToLogString(severity) + " " + message;
switch (severity)
{
case LogSeverity.ERROR:
Log(paramStr + " " + message, ErrorColor);
Log(logStr, ErrorColor);
break;
case LogSeverity.WARN:
Log(paramStr + " " + message, WarningColor);
Log(logStr, WarningColor);
break;
case LogSeverity.INFO:
Log(paramStr + " " + message, InfoColor);
Log(logStr, InfoColor);
break;
case LogSeverity.TRACE:
Log(logStr,TraceColor);
break;
case LogSeverity.DEBUG:
Log(logStr, DebugColor);
break;
case LogSeverity.EXCEPTION:
Log(logStr, ExceptionColor);
break;
case LogSeverity.FATAL:
Log(logStr, FatalColor);
break;
default:
Log(paramStr + " " + message, BaseColor);
Log(logStr, BaseColor);
break;
}
}
Expand Down
85 changes: 19 additions & 66 deletions src/EasMe.Logging/EasLogFactory.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,31 @@
using EasMe.Enums;
using log4net.Appender;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace EasMe.Logging
{
public static class EasLogFactory
{

public readonly static EasLog StaticLogger = CreateLogger("StaticLogger");
public static readonly IEasLog StaticLogger = CreateLogger();

internal static EasLogConfiguration Config { get; set; } = new EasLogConfiguration();
private static bool _isConfigured = false;
/// <summary>
/// Creates logger with given LogSource string.
/// </summary>
/// <param name="logSource"></param>
/// <returns></returns>
public static EasLog CreateLogger(string logSource)
{
return new EasLog(logSource);
}

/// <summary>
/// EasLog logging configuration. Call this method in your startup. If you don't call it it will use default values.
/// </summary>
/// <param name="config"></param>
public static void LoadConfig(EasLogConfiguration config)
{
if (_isConfigured) throw new InvalidOperationException("EasLog configuration already loaded.");
Config = config;
_isConfigured = true;
}
/// <summary>
/// EasLog logging configuration. Call this method in your startup. If you don't call it it will use default values.
/// </summary>
/// <param name="config"></param>
public static void LoadConfig(Action<EasLogConfiguration> action)
public static IEasLog CreateLogger()
{
var methodInfo = new StackTrace().GetFrame(1)?.GetMethod();
var className = methodInfo?.ReflectedType?.FullName;
return new EasLog(className ?? "Sys");
}


/// <summary>
/// EasLog logging configuration. Call this method in your startup. If you don't call it it will use default values.
/// </summary>
/// <param name="action"></param>
public static void Configure(Action<EasLogConfiguration> action)
{
if (_isConfigured) throw new InvalidOperationException("EasLog configuration already loaded.");
var config = new EasLogConfiguration();
Expand All @@ -43,54 +34,16 @@ public static void LoadConfig(Action<EasLogConfiguration> action)
_isConfigured = true;
}

public static void LoadConfig_WebLogging(LogSeverity severity, string name, bool traceLogging, bool seperateLogLevelToFolder)
{
if (_isConfigured) throw new InvalidOperationException("EasLog configuration already loaded.");
Config = new EasLogConfiguration
{
AddRequestUrlToStart = true,
ConsoleAppender = false,
ExceptionHideSensitiveInfo = false,
LogFileName = name,
MinimumLogLevel = severity,
WebInfoLogging = true,
TraceLogging = traceLogging,
SeparateLogLevelToFolder = seperateLogLevelToFolder,
};
_isConfigured = true;
}
public static void LoadConfig_Logging(
LogSeverity severity,
string name,
bool traceLogging = false,
bool seperateLogLevelToFolder = false,
bool consoleAppender = false,
bool isJson = false,
bool exceptionHideSensitiveInfo = false)
{
if (_isConfigured) throw new InvalidOperationException("EasLog configuration already loaded.");
Config = new EasLogConfiguration
{
AddRequestUrlToStart = false,
ConsoleAppender = consoleAppender,
ExceptionHideSensitiveInfo = exceptionHideSensitiveInfo,
LogFileName = name,
MinimumLogLevel = severity,
WebInfoLogging = false,
TraceLogging = traceLogging,
SeparateLogLevelToFolder = seperateLogLevelToFolder,
};
_isConfigured = true;
}
public static void LoadConfig_TraceDefault(bool isWeb)

public static void ConfigureTraceDefault(bool isWeb)
{
if (_isConfigured) throw new InvalidOperationException("EasLog configuration already loaded.");
Config = new EasLogConfiguration
{
AddRequestUrlToStart = isWeb,
ConsoleAppender = true,
ExceptionHideSensitiveInfo = false,
LogFileName = "Debug_",
LogFileName = "Trace_",
MinimumLogLevel = LogSeverity.TRACE,
WebInfoLogging = isWeb,
TraceLogging = true,
Expand Down
Loading

0 comments on commit fbb970c

Please sign in to comment.