Logger improvements, fixes, etc...
This commit is contained in:
parent
3f326c50dc
commit
8da8b65892
|
|
@ -63,5 +63,14 @@ namespace AyCode.Core.Server.Loggers
|
||||||
|
|
||||||
public static void Error(string? text, Exception? ex = null, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
public static void Error(string? text, Exception? ex = null, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
||||||
=> Instance._logger.Error(text, ex, categoryName ?? DefaultCategoryName, memberName);
|
=> Instance._logger.Error(text, ex, categoryName ?? DefaultCategoryName, memberName);
|
||||||
|
|
||||||
|
public static void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName)
|
||||||
|
=> Write(appType, logLevel, logText, callerMemberName, categoryName, null, null);
|
||||||
|
|
||||||
|
public static void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName, Exception? ex)
|
||||||
|
=> Write(appType, logLevel, logText, callerMemberName, categoryName, ex?.GetType().Name, ex?.ToString());
|
||||||
|
|
||||||
|
public static void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName, string? errorType, string? exMessage)
|
||||||
|
=> Instance._logger.Write(appType, logLevel, logText, callerMemberName, categoryName, errorType, exMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,10 @@ using AyCode.Utils.Extensions;
|
||||||
|
|
||||||
namespace AyCode.Core.Loggers;
|
namespace AyCode.Core.Loggers;
|
||||||
|
|
||||||
public class AcConsoleLogWriter : AcTextLogWriterBase
|
public interface IAcConsoleLogWriter : IAcTextLogWriterBase
|
||||||
|
{}
|
||||||
|
|
||||||
|
public class AcConsoleLogWriter : AcTextLogWriterBase, IAcConsoleLogWriter
|
||||||
{
|
{
|
||||||
protected AcConsoleLogWriter() : this(null)
|
protected AcConsoleLogWriter() : this(null)
|
||||||
{ }
|
{ }
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ public abstract class AcLogWriterBase : IAcLogWriterBase
|
||||||
|
|
||||||
protected AcLogWriterBase(string? categoryName = null)
|
protected AcLogWriterBase(string? categoryName = null)
|
||||||
{
|
{
|
||||||
CategoryName = categoryName;
|
CategoryName = categoryName ?? "...";
|
||||||
|
|
||||||
AppType = AcEnv.AppConfiguration.GetEnum<AppType>("AyCode:Logger:AppType");
|
AppType = AcEnv.AppConfiguration.GetEnum<AppType>("AyCode:Logger:AppType");
|
||||||
|
|
||||||
|
|
@ -34,35 +34,35 @@ public abstract class AcLogWriterBase : IAcLogWriterBase
|
||||||
AppType = appType;
|
AppType = appType;
|
||||||
LogLevel = logLevel;
|
LogLevel = logLevel;
|
||||||
|
|
||||||
CategoryName = categoryName;
|
CategoryName = categoryName ?? "...";
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Detail(string? text, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
public void Detail(string? text, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
||||||
=> PrepareToWrite(LogLevel.Detail, text, memberName, categoryName ?? CategoryName);
|
=> Write(AppType, LogLevel.Detail, text, memberName, categoryName ?? CategoryName);
|
||||||
|
|
||||||
//public void Detail<TCallerClassType>(string? text, [CallerMemberName] string? memberName = null) where TCallerClassType : class
|
//public void Detail<TCallerClassType>(string? text, [CallerMemberName] string? memberName = null) where TCallerClassType : class
|
||||||
// => Detail(text, typeof(TCallerClassType).Name, memberName);
|
// => Detail(text, typeof(TCallerClassType).Name, memberName);
|
||||||
|
|
||||||
public void Debug(string? text, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
public void Debug(string? text, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
||||||
=> PrepareToWrite(LogLevel.Debug, text, memberName, categoryName ?? CategoryName);
|
=> Write(AppType, LogLevel.Debug, text, memberName, categoryName ?? CategoryName);
|
||||||
|
|
||||||
//public void Debug<TCallerClassType>(string? text, [CallerMemberName] string? memberName = null) where TCallerClassType : class
|
//public void Debug<TCallerClassType>(string? text, [CallerMemberName] string? memberName = null) where TCallerClassType : class
|
||||||
// => Debug(text, typeof(TCallerClassType).Name, memberName);
|
// => Debug(text, typeof(TCallerClassType).Name, memberName);
|
||||||
|
|
||||||
public void Info(string? text, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
public void Info(string? text, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
||||||
=> PrepareToWrite(LogLevel.Info, text, memberName, categoryName ?? CategoryName);
|
=> Write(AppType, LogLevel.Info, text, memberName, categoryName ?? CategoryName);
|
||||||
|
|
||||||
//public void Info<TCallerClassType>(string? text, [CallerMemberName] string? memberName = null) where TCallerClassType : class
|
//public void Info<TCallerClassType>(string? text, [CallerMemberName] string? memberName = null) where TCallerClassType : class
|
||||||
// => Info(text, typeof(TCallerClassType).Name, memberName);
|
// => Info(text, typeof(TCallerClassType).Name, memberName);
|
||||||
|
|
||||||
public void Warning(string? text, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
public void Warning(string? text, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
||||||
=> PrepareToWrite(LogLevel.Warning, text, memberName, categoryName ?? CategoryName);
|
=> Write(AppType, LogLevel.Warning, text, memberName, categoryName ?? CategoryName);
|
||||||
|
|
||||||
//public void Warning<TCallerClassType>(string? text, [CallerMemberName] string? memberName = null) where TCallerClassType : class
|
//public void Warning<TCallerClassType>(string? text, [CallerMemberName] string? memberName = null) where TCallerClassType : class
|
||||||
// => Warning(text, typeof(TCallerClassType).Name, memberName);
|
// => Warning(text, typeof(TCallerClassType).Name, memberName);
|
||||||
|
|
||||||
public void Suggest(string? text, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
public void Suggest(string? text, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
||||||
=> PrepareToWrite(LogLevel.Suggest, text, memberName, categoryName ?? CategoryName);
|
=> Write(AppType, LogLevel.Suggest, text, memberName, categoryName ?? CategoryName);
|
||||||
|
|
||||||
//public void Suggest<TCallerClassType>(string? text, [CallerMemberName] string? memberName = null) where TCallerClassType : class
|
//public void Suggest<TCallerClassType>(string? text, [CallerMemberName] string? memberName = null) where TCallerClassType : class
|
||||||
// => Suggest(text, typeof(TCallerClassType).Name, memberName);
|
// => Suggest(text, typeof(TCallerClassType).Name, memberName);
|
||||||
|
|
@ -71,11 +71,17 @@ public abstract class AcLogWriterBase : IAcLogWriterBase
|
||||||
// => Error(text, ex, typeof(TCallerClassType).Name, memberName);
|
// => Error(text, ex, typeof(TCallerClassType).Name, memberName);
|
||||||
|
|
||||||
public void Error(string? text, Exception? ex = null, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
public void Error(string? text, Exception? ex = null, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
||||||
=> PrepareToWrite(LogLevel.Error, text, memberName, categoryName ?? CategoryName, ex);
|
=> Write(AppType, LogLevel.Error, text, memberName, categoryName ?? CategoryName, ex);
|
||||||
|
|
||||||
|
public void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName)
|
||||||
|
=> Write(appType, logLevel, logText, callerMemberName, categoryName, null, null);
|
||||||
|
|
||||||
protected virtual void PrepareToWrite(LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName = null, Exception? ex = null)
|
public void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName, Exception? ex)
|
||||||
|
=> Write(appType, logLevel, logText, callerMemberName, categoryName, ex?.GetType().Name, ex?.ToString());
|
||||||
|
|
||||||
|
public virtual void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName, string? errorType, string? exMessage)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using AyCode.Core.Consts;
|
using AyCode.Core.Consts;
|
||||||
using AyCode.Core.Enums;
|
using AyCode.Core.Enums;
|
||||||
|
using static System.Net.Mime.MediaTypeNames;
|
||||||
|
|
||||||
namespace AyCode.Core.Loggers;
|
namespace AyCode.Core.Loggers;
|
||||||
|
|
||||||
|
|
@ -18,7 +19,7 @@ public abstract class AcLoggerBase : IAcLoggerBase
|
||||||
|
|
||||||
protected AcLoggerBase(string? categoryName)
|
protected AcLoggerBase(string? categoryName)
|
||||||
{
|
{
|
||||||
CategoryName = categoryName;
|
CategoryName = categoryName ?? "...";
|
||||||
|
|
||||||
AppType = AcEnv.AppConfiguration.GetEnum<AppType>("AyCode:Logger:AppType");
|
AppType = AcEnv.AppConfiguration.GetEnum<AppType>("AyCode:Logger:AppType");
|
||||||
LogLevel = AcEnv.AppConfiguration.GetEnum<LogLevel>("AyCode:Logger:LogLevel");
|
LogLevel = AcEnv.AppConfiguration.GetEnum<LogLevel>("AyCode:Logger:LogLevel");
|
||||||
|
|
@ -41,7 +42,7 @@ public abstract class AcLoggerBase : IAcLoggerBase
|
||||||
AppType = appType;
|
AppType = appType;
|
||||||
LogLevel = logLevel;
|
LogLevel = logLevel;
|
||||||
|
|
||||||
CategoryName = categoryName;
|
CategoryName = categoryName ?? "...";
|
||||||
|
|
||||||
foreach (var acLogWriterBase in logWriters)
|
foreach (var acLogWriterBase in logWriters)
|
||||||
LogWriters.Add(acLogWriterBase);
|
LogWriters.Add(acLogWriterBase);
|
||||||
|
|
@ -90,12 +91,22 @@ public abstract class AcLoggerBase : IAcLoggerBase
|
||||||
//public virtual void Suggest<TCallerClassType>(string? text, [CallerMemberName] string? memberName = null) where TCallerClassType : class
|
//public virtual void Suggest<TCallerClassType>(string? text, [CallerMemberName] string? memberName = null) where TCallerClassType : class
|
||||||
// => Suggest(text, typeof(TCallerClassType).Name, memberName);
|
// => Suggest(text, typeof(TCallerClassType).Name, memberName);
|
||||||
|
|
||||||
|
//public virtual void Error<TCallerClassType>(string? text, Exception? ex = null, [CallerMemberName] string? memberName = null) where TCallerClassType : class
|
||||||
|
// => Error(text, ex, typeof(TCallerClassType).Name, memberName);
|
||||||
|
|
||||||
public virtual void Error(string? text, Exception? ex = null, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
public virtual void Error(string? text, Exception? ex = null, string? categoryName = null, [CallerMemberName] string? memberName = null)
|
||||||
{
|
{
|
||||||
if (LogLevel <= LogLevel.Error) LogWriters.ForEach(x => x.Error(text, ex, categoryName ?? CategoryName, memberName));
|
if (LogLevel <= LogLevel.Error) LogWriters.ForEach(x => x.Error(text, ex, categoryName ?? CategoryName, memberName));
|
||||||
}
|
}
|
||||||
|
|
||||||
//public virtual void Error<TCallerClassType>(string? text, Exception? ex = null, [CallerMemberName] string? memberName = null) where TCallerClassType : class
|
public void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName)
|
||||||
// => Error(text, ex, typeof(TCallerClassType).Name, memberName);
|
=> Write(appType, logLevel, logText, callerMemberName, categoryName, null, null);
|
||||||
|
|
||||||
|
public void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName, Exception? ex)
|
||||||
|
=> Write(appType, logLevel, logText, callerMemberName, categoryName, ex?.GetType().Name, ex?.ToString());
|
||||||
|
|
||||||
|
public virtual void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName, string? errorType, string? exMessage)
|
||||||
|
{
|
||||||
|
if (LogLevel <= logLevel) LogWriters.ForEach(x => x.Write(appType, logLevel, logText, callerMemberName, categoryName ?? CategoryName, errorType, exMessage));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -3,7 +3,10 @@ using AyCode.Utils.Extensions;
|
||||||
|
|
||||||
namespace AyCode.Core.Loggers;
|
namespace AyCode.Core.Loggers;
|
||||||
|
|
||||||
public abstract class AcTextLogWriterBase : AcLogWriterBase
|
public interface IAcTextLogWriterBase : IAcLogWriterBase
|
||||||
|
{}
|
||||||
|
|
||||||
|
public abstract class AcTextLogWriterBase : AcLogWriterBase, IAcTextLogWriterBase
|
||||||
{
|
{
|
||||||
protected AcTextLogWriterBase() : this(null)
|
protected AcTextLogWriterBase() : this(null)
|
||||||
{ }
|
{ }
|
||||||
|
|
@ -14,11 +17,11 @@ public abstract class AcTextLogWriterBase : AcLogWriterBase
|
||||||
protected AcTextLogWriterBase(AppType appType, LogLevel logLevel, string? categoryName = null) : base(appType, logLevel, categoryName)
|
protected AcTextLogWriterBase(AppType appType, LogLevel logLevel, string? categoryName = null) : base(appType, logLevel, categoryName)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
protected override void PrepareToWrite(LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName = null, Exception? ex = null)
|
public override void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName, string? errorType, string? ex)
|
||||||
{
|
{
|
||||||
if (logLevel < LogLevel) return;
|
if (logLevel < LogLevel) return;
|
||||||
|
|
||||||
WriteText(GetDiagnosticText(logLevel, logText, callerMemberName, categoryName));
|
WriteText(GetDiagnosticText(appType, logLevel, logText, callerMemberName, categoryName, null, null));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -27,23 +30,29 @@ public abstract class AcTextLogWriterBase : AcLogWriterBase
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected virtual string GetDiagnosticText(LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName = null, Exception? ex = null)
|
protected virtual string GetDiagnosticText(LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName = null, Exception? ex = null)
|
||||||
|
=> GetDiagnosticText(AppType, logLevel, logText, callerMemberName, categoryName, ex?.GetType().Name, ex?.ToString());
|
||||||
|
|
||||||
|
protected virtual string GetDiagnosticText(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName, string? errorType, string? exMessage)
|
||||||
{
|
{
|
||||||
if (LogLevel > logLevel) return string.Empty;
|
if (LogLevel > logLevel) return string.Empty;
|
||||||
|
|
||||||
var threadId = Environment.CurrentManagedThreadId;
|
var threadId = Environment.CurrentManagedThreadId;
|
||||||
|
|
||||||
if(categoryName.IsNullOrWhiteSpace()) categoryName = "...";
|
if(categoryName.IsNullOrWhiteSpace()) categoryName = CategoryName;
|
||||||
|
|
||||||
return $"[{DateTime.Now:HH:mm:ss.fff}] [{AppType.ToString()[0]}] {"[" + logLevel + "]",-9} {"[" + categoryName + "->" + callerMemberName + "]",-54} {"[" + threadId + "]",5} {logText}{ErrorText(ex)}";
|
return $"[{DateTime.Now:HH:mm:ss.fff}] [{appType.ToString()[0]}] {"[" + logLevel + "]",-9} {"[" + categoryName + "->" + callerMemberName + "]",-54} {"[" + threadId + "]",5} {logText}{ErrorText(errorType, exMessage)}";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual string ErrorText(Exception? ex)
|
protected virtual string ErrorText(Exception? ex)
|
||||||
{
|
{
|
||||||
if (ex == null) return string.Empty;
|
return ex == null ? string.Empty : ErrorText(ex.GetType().Name, ex.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
var errorType = ex.GetType().Name;
|
protected virtual string ErrorText(string? errorType, string? exMessage)
|
||||||
return string.IsNullOrWhiteSpace(errorType) ? string.Empty : $"{Environment.NewLine}[{errorType.ToUpper()}]: {ex}";
|
{
|
||||||
|
if (errorType.IsNullOrWhiteSpace() && exMessage.IsNullOrWhiteSpace()) return string.Empty;
|
||||||
|
|
||||||
|
return string.IsNullOrWhiteSpace(errorType) ? string.Empty : $"{Environment.NewLine}[{errorType.ToUpper()}]: {exMessage}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Runtime.CompilerServices;
|
using AyCode.Core.Enums;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace AyCode.Core.Loggers;
|
namespace AyCode.Core.Loggers;
|
||||||
|
|
||||||
|
|
@ -23,4 +24,9 @@ public interface IAcLogWriterBase
|
||||||
|
|
||||||
public void Error(string? text, Exception? ex = null, string? categoryName = null, [CallerMemberName] string? memberName = null);
|
public void Error(string? text, Exception? ex = null, string? categoryName = null, [CallerMemberName] string? memberName = null);
|
||||||
//public void Error<TCallerClassType>(string? text, Exception? ex = null, [CallerMemberName] string? memberName = null) where TCallerClassType : class;
|
//public void Error<TCallerClassType>(string? text, Exception? ex = null, [CallerMemberName] string? memberName = null) where TCallerClassType : class;
|
||||||
|
|
||||||
|
public void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName);
|
||||||
|
public void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName, Exception? ex);
|
||||||
|
public void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName, string? errorType, string? exMessage);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -5,6 +5,7 @@ using AyCode.Core.Loggers;
|
||||||
using AyCode.Database.DbContexts.Loggers;
|
using AyCode.Database.DbContexts.Loggers;
|
||||||
using AyCode.Entities;
|
using AyCode.Entities;
|
||||||
using AyCode.Entities.LogItems;
|
using AyCode.Entities.LogItems;
|
||||||
|
using AyCode.Entities.Server.LogItems;
|
||||||
using AyCode.Interfaces.Entities;
|
using AyCode.Interfaces.Entities;
|
||||||
|
|
||||||
namespace AyCode.Database;
|
namespace AyCode.Database;
|
||||||
|
|
@ -23,20 +24,10 @@ public class AcDbLogItemWriter<TLoggerDbContext, TLogItem> : AcLogItemWriterBase
|
||||||
public AcDbLogItemWriter(AppType appType, LogLevel logLevel, string? categoryName = null) : base(appType, logLevel, categoryName)
|
public AcDbLogItemWriter(AppType appType, LogLevel logLevel, string? categoryName = null) : base(appType, logLevel, categoryName)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
protected override void WriteLogItem(TLogItem logItem, Action? callback = null)
|
protected override void WriteLogItemCallback(TLogItem logItem)
|
||||||
{
|
{
|
||||||
try
|
_ctx.LogItems.Add(logItem);
|
||||||
{
|
_ctx.SaveChanges();
|
||||||
base.WriteLogItem(logItem, () =>
|
|
||||||
{
|
|
||||||
_ctx.LogItems.Add(logItem);
|
|
||||||
_ctx.SaveChanges();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine("ERRORORROROR! " + AcEnv.NL + ex.Message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//protected override void WriteLogItem(TLogItem logItem, Action<IEnumerable<TLogItem>>? callback = null)
|
//protected override void WriteLogItem(TLogItem logItem, Action<IEnumerable<TLogItem>>? callback = null)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
using AyCode.Database.DbSets.Loggers;
|
using AyCode.Database.DbSets.Loggers;
|
||||||
using AyCode.Entities.LogItems;
|
using AyCode.Entities.LogItems;
|
||||||
|
using AyCode.Entities.Server.LogItems;
|
||||||
using AyCode.Interfaces.Entities;
|
using AyCode.Interfaces.Entities;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
using AyCode.Database.DbSets.Loggers;
|
using AyCode.Database.DbSets.Loggers;
|
||||||
using AyCode.Entities.LogItems;
|
using AyCode.Entities.LogItems;
|
||||||
|
using AyCode.Entities.Server.LogItems;
|
||||||
using AyCode.Interfaces.Entities;
|
using AyCode.Interfaces.Entities;
|
||||||
|
|
||||||
namespace AyCode.Database.DbContexts.Loggers;
|
namespace AyCode.Database.DbContexts.Loggers;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using AyCode.Entities.LogItems;
|
using AyCode.Entities.LogItems;
|
||||||
|
using AyCode.Entities.Server.LogItems;
|
||||||
using AyCode.Interfaces.Entities;
|
using AyCode.Interfaces.Entities;
|
||||||
using AyCode.Interfaces.Users;
|
using AyCode.Interfaces.Users;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\AyCode.Core\AyCode.Core.csproj" />
|
||||||
|
<ProjectReference Include="..\AyCode.Entities\AyCode.Entities.csproj" />
|
||||||
<ProjectReference Include="..\AyCode.Utils\AyCode.Utils.csproj" />
|
<ProjectReference Include="..\AyCode.Utils\AyCode.Utils.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using AyCode.Core.Enums;
|
||||||
|
using AyCode.Core.Loggers;
|
||||||
|
using AyCode.Entities.LogItems;
|
||||||
|
|
||||||
|
namespace AyCode.Entities.Server.LogItems;
|
||||||
|
|
||||||
|
[Table("LogItem")]
|
||||||
|
public class AcLogItem : AcLogItemClient, IAcLogItem
|
||||||
|
{
|
||||||
|
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
public int LogHeaderId { get; set; }
|
||||||
|
public DateTime TimeStampUtc { get; set; }
|
||||||
|
|
||||||
|
public AcLogItem()
|
||||||
|
{
|
||||||
|
TimeStampUtc = DateTime.UtcNow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
using AyCode.Core.Enums;
|
||||||
|
using AyCode.Core.Loggers;
|
||||||
|
using AyCode.Entities.LogItems;
|
||||||
|
using AyCode.Interfaces.Entities;
|
||||||
|
|
||||||
|
namespace AyCode.Entities.Server.LogItems;
|
||||||
|
|
||||||
|
public interface IAcLogItem : IAcLogItemClient, IEntityInt
|
||||||
|
{
|
||||||
|
public int LogHeaderId { get; set; }
|
||||||
|
public DateTime TimeStampUtc { get; set; }
|
||||||
|
}
|
||||||
|
|
@ -5,10 +5,11 @@ using AyCode.Entities.LogItems;
|
||||||
using AyCode.Interfaces.Entities;
|
using AyCode.Interfaces.Entities;
|
||||||
using AyCode.Utils.Extensions;
|
using AyCode.Utils.Extensions;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
using AyCode.Core.Consts;
|
||||||
|
|
||||||
namespace AyCode.Entities;
|
namespace AyCode.Entities;
|
||||||
|
|
||||||
public abstract class AcLogItemWriterBase<TLogItem> : AcLogWriterBase where TLogItem : class, IAcLogItem
|
public abstract class AcLogItemWriterBase<TLogItem> : AcLogWriterBase where TLogItem : class, IAcLogItemClient
|
||||||
{
|
{
|
||||||
protected readonly Mutex MutexLock = new();//new(false, "GLOBAL");
|
protected readonly Mutex MutexLock = new();//new(false, "GLOBAL");
|
||||||
|
|
||||||
|
|
@ -22,38 +23,62 @@ public abstract class AcLogItemWriterBase<TLogItem> : AcLogWriterBase where TLog
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
protected TLogItem CreateLogItem(int threadId, DateTime utcNow, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName = null, Exception? ex = null)
|
protected virtual TLogItem CreateLogItem(AppType appType, int threadId, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName = null, string? errorType = null, string? exMessage = null)
|
||||||
{
|
{
|
||||||
var logItem = Activator.CreateInstance<TLogItem>();
|
var logItem = Activator.CreateInstance<TLogItem>();
|
||||||
|
|
||||||
logItem.TimeStampUtc = utcNow;
|
logItem.AppType = appType;
|
||||||
logItem.AppType = AppType;
|
|
||||||
logItem.LogLevel = logLevel;
|
logItem.LogLevel = logLevel;
|
||||||
logItem.CategoryName = categoryName;
|
logItem.CategoryName = categoryName ?? CategoryName;
|
||||||
logItem.CallerName = callerMemberName;
|
logItem.CallerName = callerMemberName;
|
||||||
logItem.Text = logText;
|
logItem.Text = logText;
|
||||||
logItem.Exception = ex?.Message;
|
|
||||||
logItem.ThreadId = threadId;
|
logItem.ThreadId = threadId;
|
||||||
|
|
||||||
|
logItem.ErrorType = errorType;
|
||||||
|
logItem.Exception = exMessage;
|
||||||
|
|
||||||
return logItem;
|
return logItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void PrepareToWrite(LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName = null, Exception? ex = null)
|
public override void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName, string? errorType, string? exMessage)
|
||||||
|
=> WriteAsync(appType, logLevel, logText, callerMemberName, categoryName, errorType, exMessage);
|
||||||
|
|
||||||
|
protected virtual void WriteAsync(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName, string? errorType, string? exMessage)
|
||||||
{
|
{
|
||||||
if (logLevel < LogLevel) return;
|
if (logLevel < LogLevel) return;
|
||||||
|
|
||||||
var utcNow = DateTime.UtcNow;
|
|
||||||
var threadId = Environment.CurrentManagedThreadId;
|
var threadId = Environment.CurrentManagedThreadId;
|
||||||
|
|
||||||
TaskHelper.RunOnThreadPool(() => WriteLogItem(CreateLogItem(threadId, utcNow, logLevel, logText, callerMemberName, categoryName)));
|
TaskHelper.RunOnThreadPool(() => WriteLogItem(CreateLogItem(appType, threadId, logLevel, logText, callerMemberName, categoryName, errorType, exMessage)));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void WriteLogItem(TLogItem logItem, Action? callback = null)
|
public void WriteLogItemAsync(TLogItem logItem)
|
||||||
{
|
{
|
||||||
using (MutexLock.UseWaitOne())
|
if (logItem.LogLevel < LogLevel) return;
|
||||||
|
|
||||||
|
TaskHelper.RunOnThreadPool(() => WriteLogItem(logItem));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WriteLogItem(TLogItem logItem)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
callback?.Invoke();
|
if (logItem.LogLevel < LogLevel) return;
|
||||||
|
|
||||||
|
using (MutexLock.UseWaitOne())
|
||||||
|
{
|
||||||
|
WriteLogItemCallback(logItem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("ERRORORROROR! " + AcEnv.NL + ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void WriteLogItemCallback(TLogItem logItem)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
//private volatile bool _isLocked = false;
|
//private volatile bool _isLocked = false;
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Messages\" />
|
<Folder Include="Messages\" />
|
||||||
<Folder Include="LogItems\" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -6,19 +6,14 @@ using AyCode.Core.Loggers;
|
||||||
|
|
||||||
namespace AyCode.Entities.LogItems;
|
namespace AyCode.Entities.LogItems;
|
||||||
|
|
||||||
[Table("LogItem")]
|
public class AcLogItemClient : IAcLogItemClient
|
||||||
public class AcLogItem : IAcLogItem
|
|
||||||
{
|
{
|
||||||
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
|
||||||
public int Id { get; set; }
|
|
||||||
|
|
||||||
public int LogHeaderId { get; set; }
|
|
||||||
public DateTime TimeStampUtc { get; set; }
|
|
||||||
public AppType AppType { get; set; }
|
public AppType AppType { get; set; }
|
||||||
public LogLevel LogLevel { get; set; }
|
public LogLevel LogLevel { get; set; }
|
||||||
public int ThreadId { get; set; }
|
public int ThreadId { get; set; }
|
||||||
public string? CategoryName { get; set; }
|
public string? CategoryName { get; set; }
|
||||||
public string? CallerName { get; set; }
|
public string? CallerName { get; set; }
|
||||||
public string? Text { get; set; }
|
public string? Text { get; set; }
|
||||||
|
public string? ErrorType { get; set; }
|
||||||
public string? Exception { get; set; }
|
public string? Exception { get; set; }
|
||||||
}
|
}
|
||||||
|
|
@ -4,10 +4,8 @@ using AyCode.Interfaces.Entities;
|
||||||
|
|
||||||
namespace AyCode.Entities.LogItems;
|
namespace AyCode.Entities.LogItems;
|
||||||
|
|
||||||
public interface IAcLogItem : IEntityInt
|
public interface IAcLogItemClient
|
||||||
{
|
{
|
||||||
public int LogHeaderId { get; set; }
|
|
||||||
public DateTime TimeStampUtc { get; set; }
|
|
||||||
public AppType AppType { get; set; }
|
public AppType AppType { get; set; }
|
||||||
public LogLevel LogLevel { get; set; }
|
public LogLevel LogLevel { get; set; }
|
||||||
public int ThreadId { get; set; }
|
public int ThreadId { get; set; }
|
||||||
|
|
@ -15,4 +13,5 @@ public interface IAcLogItem : IEntityInt
|
||||||
public string? CallerName { get; set; }
|
public string? CallerName { get; set; }
|
||||||
public string? Text { get; set; }
|
public string? Text { get; set; }
|
||||||
public string? Exception { get; set; }
|
public string? Exception { get; set; }
|
||||||
|
public string? ErrorType { get; set; }
|
||||||
}
|
}
|
||||||
|
|
@ -8,7 +8,7 @@ using System;
|
||||||
|
|
||||||
namespace AyCode.Services.Loggers;
|
namespace AyCode.Services.Loggers;
|
||||||
|
|
||||||
public abstract class AcHttpClientLogItemWriter<TLogItem> : AcLogItemWriterBase<TLogItem> where TLogItem : class, IAcLogItem
|
public abstract class AcHttpClientLogItemWriter<TLogItem> : AcLogItemWriterBase<TLogItem> where TLogItem : class, IAcLogItemClient
|
||||||
{
|
{
|
||||||
protected HttpClient _httpClient;
|
protected HttpClient _httpClient;
|
||||||
protected readonly HttpClientHandler _httpClientHandler;
|
protected readonly HttpClientHandler _httpClientHandler;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue