11using Immense . RemoteControl . Shared . Models ;
22using Microsoft . AspNetCore . SignalR . Client ;
3+ using Microsoft . Extensions . Logging ;
34using Remotely . Agent . Interfaces ;
45using Remotely . Agent . Models ;
56using Remotely . Shared . Models ;
1516
1617namespace Remotely . Agent . Services
1718{
18- public class ChatClientService
19+ public interface IChatClientService
20+ {
21+ Task SendMessage ( string senderName , string message , string orgName , string orgId , bool disconnected , string senderConnectionID , HubConnection hubConnection ) ;
22+ }
23+
24+ public class ChatClientService : IChatClientService
1925 {
2026 private readonly IAppLauncher _appLauncher ;
27+ private readonly ILogger < ChatClientService > _logger ;
2128 private readonly MemoryCache _chatClients = new ( "ChatClients" ) ;
22- private readonly SemaphoreSlim _messageLock = new ( 1 , 1 ) ;
23-
24- public ChatClientService ( IAppLauncher appLauncher )
25- {
26- _appLauncher = appLauncher ;
27- }
29+ private readonly SemaphoreSlim _messageLock = new ( 1 , 1 ) ;
2830
29- private CacheItemPolicy CacheItemPolicy { get ; } = new ( )
31+ private readonly CacheItemPolicy _cacheItemPolicy = new ( )
3032 {
3133 SlidingExpiration = TimeSpan . FromMinutes ( 10 ) ,
3234 RemovedCallback = new CacheEntryRemovedCallback ( args =>
@@ -48,6 +50,13 @@ public ChatClientService(IAppLauncher appLauncher)
4850 } )
4951 } ;
5052
53+ public ChatClientService (
54+ IAppLauncher appLauncher ,
55+ ILogger < ChatClientService > logger )
56+ {
57+ _appLauncher = appLauncher ;
58+ _logger = logger ;
59+ }
5160
5261 public async Task SendMessage (
5362 string senderName ,
@@ -60,7 +69,7 @@ public async Task SendMessage(
6069 {
6170 if ( ! await _messageLock . WaitAsync ( 30000 ) )
6271 {
63- Logger . Write ( "Timed out waiting for chat message lock." , Shared . Enums . EventType . Warning ) ;
72+ _logger . LogWarning ( "Timed out waiting for chat message lock." ) ;
6473 return ;
6574 }
6675
@@ -80,24 +89,24 @@ public async Task SendMessage(
8089
8190 if ( procID > 0 )
8291 {
83- Logger . Write ( $ "Chat app started. Process ID: { procID } ") ;
92+ _logger . LogInformation ( "Chat app started. Process ID: {procID}" , procID ) ;
8493 }
8594 else
8695 {
87- Logger . Write ( $ "Chat app did not start successfully.") ;
96+ _logger . LogError ( $ "Chat app did not start successfully.") ;
8897 return ;
8998 }
9099
91100 var clientPipe = new NamedPipeClientStream ( "." , pipeName , PipeDirection . InOut , PipeOptions . Asynchronous ) ;
92101 clientPipe . Connect ( 15000 ) ;
93102 if ( ! clientPipe . IsConnected )
94103 {
95- Logger . Write ( "Failed to connect to chat host." ) ;
104+ _logger . LogError ( "Failed to connect to chat host." ) ;
96105 return ;
97106 }
98107 chatSession = new ChatSession ( ) { PipeStream = clientPipe , ProcessID = procID } ;
99108 _ = Task . Run ( async ( ) => { await ReadFromStream ( chatSession . PipeStream , senderConnectionID , hubConnection ) ; } ) ;
100- _chatClients . Add ( senderConnectionID , chatSession , CacheItemPolicy ) ;
109+ _chatClients . Add ( senderConnectionID , chatSession , _cacheItemPolicy ) ;
101110 }
102111
103112 chatSession = ( ChatSession ) _chatClients . Get ( senderConnectionID ) ;
@@ -116,7 +125,7 @@ public async Task SendMessage(
116125 }
117126 catch ( Exception ex )
118127 {
119- Logger . Write ( ex ) ;
128+ _logger . LogError ( ex , "Error while sending chat message." ) ;
120129 }
121130 finally
122131 {
0 commit comments