mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-01-18 18:14:20 +01:00
Removed console log adapter (unused)
This commit is contained in:
parent
8c75917f6f
commit
f18b167f6f
|
@ -15,7 +15,7 @@ public class SerilogAdapter : LogAdapterBase
|
|||
_messageTemplateParser = new MessageTemplateParser();
|
||||
}
|
||||
|
||||
public const string SampleTemplate = "[{Timestamp:HH:mm:ss} - {Caller} -> {AsmCaller}] | {Level:u3}] {Message:lj}{NewLine}{Exception}";
|
||||
public const string SampleTemplate = "[{Timestamp:HH:mm:ss} - {Caller} -> {Source}] | {Level:u3}] {Message:lj}{NewLine}{Exception}";
|
||||
|
||||
private readonly global::Serilog.ILogger _serilogLogger;
|
||||
private readonly MessageTemplateParser _messageTemplateParser;
|
||||
|
@ -26,7 +26,7 @@ public class SerilogAdapter : LogAdapterBase
|
|||
return;
|
||||
var baseLogger = capsule.Logger as Logger;
|
||||
var logger = _serilogLogger
|
||||
.ForContext("AsmCaller", baseLogger?.CallingAsmInfo.AssemblyName ?? "Static")
|
||||
.ForContext("Source", baseLogger?.Source.AssemblyName ?? "Static")
|
||||
.ForContext("Caller", baseLogger?.Identifier);
|
||||
|
||||
var template = _messageTemplateParser.Parse(capsule.Message);
|
||||
|
@ -39,6 +39,7 @@ public class SerilogAdapter : LogAdapterBase
|
|||
switch (capsule.Severity)
|
||||
{
|
||||
case LogSeverity.Trace:
|
||||
default:
|
||||
logger.Write(new LogEvent(capsule.TimeStamp, LogEventLevel.Verbose, null, template, properties ?? ArraySegment<LogEventProperty>.Empty, ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom()));
|
||||
break;
|
||||
case LogSeverity.Debug:
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
namespace DotBased;
|
||||
|
||||
public class Based
|
||||
{
|
||||
void Test()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
|
@ -1,5 +1,8 @@
|
|||
namespace DotBased.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Some simple extensions used for the string class
|
||||
/// </summary>
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static bool IsNullOrWhiteSpace(this string s) => string.IsNullOrWhiteSpace(s);
|
||||
|
|
|
@ -12,11 +12,6 @@ public abstract class LogAdapterBase
|
|||
public string Id { get; } = Guid.NewGuid().ToString();
|
||||
public string AdapterName { get; }
|
||||
|
||||
/*private string[] GetMessageProperties(string message)
|
||||
{
|
||||
return [];
|
||||
}*/
|
||||
|
||||
public abstract void HandleLog(object? sender, LogCapsule? capsule);
|
||||
|
||||
public override int GetHashCode() => HashCode.Combine(Id, AdapterName);
|
||||
|
|
|
@ -21,7 +21,7 @@ public class LogProcessor : IDisposable
|
|||
private readonly ManualResetEvent _threadShutdownEvent = new ManualResetEvent(false);
|
||||
|
||||
/// <summary>
|
||||
/// Stop the LogProcessor, the processor cannot be resumed after stopped!
|
||||
/// Stop the LogProcessor, the processor cannot be resumed after it is stopped!
|
||||
/// </summary>
|
||||
public void Stop()
|
||||
{
|
||||
|
@ -54,7 +54,7 @@ public class LogProcessor : IDisposable
|
|||
if (_threadShutdownEvent.WaitOne(0))
|
||||
break;
|
||||
|
||||
if (_processorQueue.Any())
|
||||
if (_processorQueue.Count != 0)
|
||||
{
|
||||
var capsule = _processorQueue.Dequeue();
|
||||
if (LogService.ShouldLog(LogService.Options.Severity, capsule.Severity))
|
||||
|
|
|
@ -30,22 +30,24 @@ public static class LogService
|
|||
public static ILogger RegisterLogger(string identifier)
|
||||
{
|
||||
var asm = Assembly.GetCallingAssembly();
|
||||
var logger = new Logger(identifier, CallingAssemblyInfo.LoadFromAsm(asm), ref _loggerSendEvent);
|
||||
var logger = new Logger(identifier, CallingSource.LoadFromAsm(asm), ref _loggerSendEvent);
|
||||
Loggers.Add(logger);
|
||||
return logger;
|
||||
}
|
||||
}
|
||||
|
||||
public struct CallingAssemblyInfo
|
||||
public struct CallingSource
|
||||
{
|
||||
private CallingAssemblyInfo(Assembly asm)
|
||||
private CallingSource(Assembly asm)
|
||||
{
|
||||
var asmName = asm.GetName();
|
||||
AssemblySource = asm;
|
||||
var asmName = AssemblySource.GetName();
|
||||
AssemblyName = asmName.Name ?? "Unknown";
|
||||
AssemblyFullName = asmName.FullName;
|
||||
}
|
||||
public static CallingAssemblyInfo LoadFromAsm(Assembly asm) => new CallingAssemblyInfo(asm);
|
||||
public static CallingSource LoadFromAsm(Assembly asm) => new CallingSource(asm);
|
||||
|
||||
public Assembly AssemblySource { get; }
|
||||
public string AssemblyName { get; }
|
||||
public string AssemblyFullName { get; set; }
|
||||
}
|
|
@ -2,15 +2,15 @@ namespace DotBased.Logging;
|
|||
|
||||
public class Logger : ILogger
|
||||
{
|
||||
public Logger(string identifier, CallingAssemblyInfo asmInfo, ref Action<LogCapsule> logProcessorHandler)
|
||||
public Logger(string identifier, CallingSource source, ref Action<LogCapsule> logProcessorHandler)
|
||||
{
|
||||
Identifier = identifier;
|
||||
CallingAsmInfo = asmInfo;
|
||||
Source = source;
|
||||
_processLog = logProcessorHandler;
|
||||
}
|
||||
|
||||
public string Identifier { get; }
|
||||
public CallingAssemblyInfo CallingAsmInfo { get; }
|
||||
public CallingSource Source { get; }
|
||||
|
||||
private readonly Action<LogCapsule> _processLog;
|
||||
|
||||
|
@ -93,5 +93,5 @@ public class Logger : ILogger
|
|||
});
|
||||
}
|
||||
|
||||
public override int GetHashCode() => HashCode.Combine(Identifier, CallingAsmInfo.AssemblyFullName);
|
||||
public override int GetHashCode() => HashCode.Combine(Identifier, Source.AssemblyFullName);
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
using System.Text;
|
||||
|
||||
namespace DotBased.Logging;
|
||||
|
||||
public class SimpleConsoleLogAdapter : LogAdapterBase
|
||||
{
|
||||
public SimpleConsoleLogAdapter(string adapterName) : base(adapterName)
|
||||
{
|
||||
Console.OutputEncoding = Encoding.UTF8;
|
||||
}
|
||||
|
||||
private readonly ConsoleColor _defaultColor = Console.ForegroundColor;
|
||||
private const ConsoleColor TimestampColor = ConsoleColor.DarkBlue;
|
||||
private const ConsoleColor BrackedColor = ConsoleColor.Gray;
|
||||
private const ConsoleColor MessageColor = ConsoleColor.DarkGray;
|
||||
private ConsoleColor _severityColor = ConsoleColor.Cyan;
|
||||
|
||||
public override void HandleLog(object? sender, LogCapsule? capsule)
|
||||
{
|
||||
if (capsule == null) return;
|
||||
|
||||
Console.ForegroundColor = BrackedColor;
|
||||
Console.Write("[");
|
||||
Console.ForegroundColor = TimestampColor;
|
||||
Console.Write($"{capsule.TimeStamp}");
|
||||
Console.ForegroundColor = BrackedColor;
|
||||
Console.Write("] ");
|
||||
|
||||
_severityColor = capsule.Severity.ToConsoleColor();
|
||||
WriteSeverity(capsule.Severity);
|
||||
|
||||
if (capsule.Severity is LogSeverity.Error or LogSeverity.Fatal)
|
||||
WriteException(capsule);
|
||||
else
|
||||
WriteMessage(capsule);
|
||||
Console.ForegroundColor = _defaultColor;
|
||||
}
|
||||
|
||||
private void WriteSeverity(LogSeverity severity)
|
||||
{
|
||||
Console.ForegroundColor = BrackedColor;
|
||||
Console.Write("[");
|
||||
Console.ForegroundColor = _severityColor;
|
||||
Console.Write($"{severity}");
|
||||
Console.ForegroundColor = BrackedColor;
|
||||
Console.Write("] ");
|
||||
}
|
||||
|
||||
private void WriteMessage(LogCapsule capsule)
|
||||
{
|
||||
if (capsule.Parameters == null || !capsule.Parameters.Any())
|
||||
{
|
||||
Console.ForegroundColor = MessageColor;
|
||||
Console.WriteLine($"{capsule.Message}");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteException(LogCapsule capsule)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Blue;
|
||||
Console.WriteLine("\u23F7");
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine("==============================================================================================");
|
||||
WriteMessage(capsule);
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine(capsule.Exception);
|
||||
Console.WriteLine("==============================================================================================");
|
||||
Console.ForegroundColor = _defaultColor;
|
||||
}
|
||||
}
|
||||
|
||||
public static class LogSeverityExt
|
||||
{
|
||||
public static ConsoleColor ToConsoleColor(this LogSeverity severity)
|
||||
{
|
||||
var color = severity switch
|
||||
{
|
||||
LogSeverity.Trace or LogSeverity.Info => ConsoleColor.White,
|
||||
LogSeverity.Debug => ConsoleColor.Magenta,
|
||||
LogSeverity.Warning => ConsoleColor.Yellow,
|
||||
LogSeverity.Error => ConsoleColor.Red,
|
||||
LogSeverity.Fatal => ConsoleColor.DarkRed,
|
||||
_ => ConsoleColor.White
|
||||
};
|
||||
return color;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user