diff --git a/AyCode.Services.Server/SignalRs/AcLoggerSignalRHub.cs b/AyCode.Services.Server/SignalRs/AcLoggerSignalRHub.cs new file mode 100644 index 0000000..792de80 --- /dev/null +++ b/AyCode.Services.Server/SignalRs/AcLoggerSignalRHub.cs @@ -0,0 +1,39 @@ +using AyCode.Core.Consts; +using AyCode.Core.Loggers; +using AyCode.Entities.Server.LogItems; +using Castle.Core.Logging; +using Microsoft.AspNetCore.SignalR; + +namespace AyCode.Services.Server.SignalRs; + + +public class AcLoggerSignalRHub(TLogger logger) : Hub where TLogger : IAcLoggerBase +{ + private TLogger _logger = logger; + + public void AddLogItem(AcLogItem? logItem) + { + try + { + if (logItem == null) + { + _logger.Error("AddLogItem; LogItem == null"); + //_logger.Writer().Detail(""); + + return; + } + + //logItem.LogHeaderId = ??? + logItem.TimeStampUtc = DateTime.UtcNow; + + _logger.Write(logItem); + + //_logger.Writer()?.Write(logItem.AppType, logItem.LogLevel, logItem.Text, logItem.CallerName, logItem.CategoryName, logItem.ErrorType, logItem.Exception); + //_logger.Writer().WriteLogItemAsync(logItem); + } + catch (Exception ex) + { + Console.WriteLine($@"ERROR!!! {nameof(AcLoggerSignalRHub)}->AddLogItem; ex: {ex.Message}{AcEnv.NL}{AcEnv.NL}{ex}"); + } + } +} \ No newline at end of file diff --git a/AyCode.Services/AyCode.Services.csproj b/AyCode.Services/AyCode.Services.csproj index 8947b8e..3ec6f34 100644 --- a/AyCode.Services/AyCode.Services.csproj +++ b/AyCode.Services/AyCode.Services.csproj @@ -7,6 +7,7 @@ + diff --git a/AyCode.Services/Loggers/AcSignaRClientLogItemWriter.cs b/AyCode.Services/Loggers/AcSignaRClientLogItemWriter.cs new file mode 100644 index 0000000..95d5d26 --- /dev/null +++ b/AyCode.Services/Loggers/AcSignaRClientLogItemWriter.cs @@ -0,0 +1,57 @@ +using AyCode.Core.Enums; +using AyCode.Core.Helpers; +using AyCode.Core.Loggers; +using AyCode.Entities; +using AyCode.Entities.LogItems; +using Microsoft.AspNetCore.SignalR.Client; + +namespace AyCode.Services.Loggers +{ + public class AcSignaRClientLogItemWriter : AcLogItemWriterBase, IAcLogWriterClientBase + { + private readonly HubConnection _hubConnection; + + public AcSignaRClientLogItemWriter(string fullHubName, AppType appType = AppType.Web, LogLevel logLevel = LogLevel.Detail, string? categoryName = null) : base(appType, logLevel, categoryName) + { + _hubConnection = new HubConnectionBuilder() + .WithUrl(fullHubName) + //.WithAutomaticReconnect() + //.AddMessagePackProtocol(options => options.SerializerOptions = MessagePackSerializerOptions.Standard.WithSecurity(MessagePackSecurity.UntrustedData)) + .Build(); + + _hubConnection.StartAsync().Forget(); + } + + public async Task StartConnection() + { + if (_hubConnection.State == HubConnectionState.Disconnected) + await _hubConnection.StartAsync(); + + if (_hubConnection.State != HubConnectionState.Connected) + await TaskHelper.WaitToAsync(() => _hubConnection.State == HubConnectionState.Connected, 10000, 10, 25); + } + + public async Task StopConnection() + { + await _hubConnection.StopAsync(); + await _hubConnection.DisposeAsync(); + } + + public override void Write(AppType appType, LogLevel logLevel, string? logText, string? callerMemberName, string? categoryName, string? errorType, string? exMessage) + => WriteLogItem(CreateLogItem(DateTime.UtcNow, appType, Environment.CurrentManagedThreadId, logLevel, logText, callerMemberName, categoryName, errorType, exMessage)); + + protected override async void WriteLogItemCallback(AcLogItemClient logItem) + { + //Ez fontos, mert a signalR nem küldi a DateTime.Kind-ot! A szeró oldalon kap egy utc DateTime-ot, ami a kliens lokális DateTime-ját tartalmazza! - J. + logItem.TimeStampUtc = logItem.TimeStampUtc.ToUniversalTime(); + + await StartConnection(); + + if (_hubConnection.State != HubConnectionState.Connected) + return; + + //var refreshToken = Setting.UserBasicDetails?.RefreshToken ?? Guid.NewGuid().ToString("N"); + _hubConnection.SendAsync("AddLogItem", /*refreshToken,*/ logItem).Forget(); + } + } +} diff --git a/AyCode.Services.Server/SignalRs/AcSignalRClientBase.cs b/AyCode.Services/SignalRs/AcSignalRClientBase.cs similarity index 99% rename from AyCode.Services.Server/SignalRs/AcSignalRClientBase.cs rename to AyCode.Services/SignalRs/AcSignalRClientBase.cs index 3774eb4..dd1eea1 100644 --- a/AyCode.Services.Server/SignalRs/AcSignalRClientBase.cs +++ b/AyCode.Services/SignalRs/AcSignalRClientBase.cs @@ -4,11 +4,10 @@ using AyCode.Core.Extensions; using AyCode.Core.Helpers; using AyCode.Core.Loggers; using AyCode.Interfaces.Entities; -using AyCode.Services.SignalRs; using MessagePack.Resolvers; using Microsoft.AspNetCore.SignalR.Client; -namespace AyCode.Services.Server.SignalRs +namespace AyCode.Services.SignalRs { public abstract class AcSignalRClientBase : IAcSignalRHubClient { @@ -30,6 +29,7 @@ namespace AyCode.Services.Server.SignalRs HubConnection = new HubConnectionBuilder() .WithUrl(fullHubName) + //.WithAutomaticReconnect() //.AddMessagePackProtocol(options => { // options.SerializerOptions = MessagePackSerializerOptions.Standard // .WithResolver(MessagePack.Resolvers.StandardResolver.Instance) diff --git a/AyCode.Services.Server/SignalRs/SignalRRequestModel.cs b/AyCode.Services/SignalRs/SignalRRequestModel.cs similarity index 89% rename from AyCode.Services.Server/SignalRs/SignalRRequestModel.cs rename to AyCode.Services/SignalRs/SignalRRequestModel.cs index 727b6b8..b567044 100644 --- a/AyCode.Services.Server/SignalRs/SignalRRequestModel.cs +++ b/AyCode.Services/SignalRs/SignalRRequestModel.cs @@ -1,4 +1,4 @@ -namespace AyCode.Services.Server.SignalRs; +namespace AyCode.Services.SignalRs; public class SignalRRequestModel {