Skip to content

Commit 434ccb2

Browse files
committed
Create logger for the network stream readers and writers
1 parent d83f4b4 commit 434ccb2

4 files changed

Lines changed: 38 additions & 9 deletions

File tree

src/FubarDev.FtpServer/ConnectionHandlers/SecureConnectionAdapter.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
using FubarDev.FtpServer.Authentication;
1111

12+
using Microsoft.Extensions.Logging;
13+
1214
namespace FubarDev.FtpServer.ConnectionHandlers
1315
{
1416
/// <summary>
@@ -21,6 +23,7 @@ internal class SecureConnectionAdapter : IFtpSecureConnectionAdapter
2123
private readonly ISslStreamWrapperFactory _sslStreamWrapperFactory;
2224

2325
private readonly CancellationToken _connectionClosed;
26+
private readonly ILoggerFactory? _loggerFactory;
2427
private IFtpConnectionAdapter _activeCommunicationService;
2528

2629
/// <summary>
@@ -30,20 +33,24 @@ internal class SecureConnectionAdapter : IFtpSecureConnectionAdapter
3033
/// <param name="connectionPipe">The pipe to the connection object.</param>
3134
/// <param name="sslStreamWrapperFactory">The SSL stream wrapper factory.</param>
3235
/// <param name="connectionClosed">The cancellation token for a closed connection.</param>
36+
/// <param name="loggerFactory">The logger factory.</param>
3337
public SecureConnectionAdapter(
3438
IDuplexPipe socketPipe,
3539
IDuplexPipe connectionPipe,
3640
ISslStreamWrapperFactory sslStreamWrapperFactory,
37-
CancellationToken connectionClosed)
41+
CancellationToken connectionClosed,
42+
ILoggerFactory? loggerFactory = null)
3843
{
3944
_socketPipe = socketPipe;
4045
_connectionPipe = connectionPipe;
4146
_sslStreamWrapperFactory = sslStreamWrapperFactory;
4247
_connectionClosed = connectionClosed;
48+
_loggerFactory = loggerFactory;
4349
_activeCommunicationService = new PassThroughConnectionAdapter(
4450
socketPipe,
4551
connectionPipe,
46-
connectionClosed);
52+
connectionClosed,
53+
loggerFactory);
4754
}
4855

4956
/// <inheritdoc />
@@ -60,7 +67,8 @@ await StopAsync(cancellationToken)
6067
_activeCommunicationService = new PassThroughConnectionAdapter(
6168
_socketPipe,
6269
_connectionPipe,
63-
_connectionClosed);
70+
_connectionClosed,
71+
_loggerFactory);
6472
await StartAsync(cancellationToken)
6573
.ConfigureAwait(false);
6674
}
@@ -77,14 +85,16 @@ await StopAsync(cancellationToken)
7785
_connectionPipe,
7886
_sslStreamWrapperFactory,
7987
certificate,
80-
_connectionClosed);
88+
_connectionClosed,
89+
_loggerFactory);
8190
}
8291
catch
8392
{
8493
_activeCommunicationService = new PassThroughConnectionAdapter(
8594
_socketPipe,
8695
_connectionPipe,
87-
_connectionClosed);
96+
_connectionClosed,
97+
_loggerFactory);
8898
throw;
8999
}
90100
finally

src/FubarDev.FtpServer/ConnectionHandlers/SslStreamConnectionAdapter.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal class SslStreamConnectionAdapter : IFtpConnectionAdapter
2727
private readonly X509Certificate2 _certificate;
2828

2929
private readonly CancellationToken _connectionClosed;
30+
private readonly ILoggerFactory? _loggerFactory;
3031

3132
private SslCommunicationInfo? _info;
3233

@@ -35,13 +36,15 @@ public SslStreamConnectionAdapter(
3536
IDuplexPipe connectionPipe,
3637
ISslStreamWrapperFactory sslStreamWrapperFactory,
3738
X509Certificate2 certificate,
38-
CancellationToken connectionClosed)
39+
CancellationToken connectionClosed,
40+
ILoggerFactory? loggerFactory = null)
3941
{
4042
_socketPipe = socketPipe;
4143
_connectionPipe = connectionPipe;
4244
_sslStreamWrapperFactory = sslStreamWrapperFactory;
4345
_certificate = certificate;
4446
_connectionClosed = connectionClosed;
47+
_loggerFactory = loggerFactory;
4548
}
4649

4750
/// <inheritdoc />
@@ -62,15 +65,24 @@ public async Task StartAsync(CancellationToken cancellationToken)
6265
_socketPipe.Output);
6366
var sslStream = await _sslStreamWrapperFactory.WrapStreamAsync(rawStream, false, _certificate, cancellationToken)
6467
.ConfigureAwait(false);
68+
69+
var receiverLogger = _loggerFactory
70+
?.CreateLogger(typeof(SslStreamConnectionAdapter).FullName + ":Receiver");
6571
var receiverService = new NonClosingNetworkStreamReader(
6672
sslStream,
6773
_connectionPipe.Output,
6874
_socketPipe.Input,
69-
_connectionClosed);
75+
_connectionClosed,
76+
receiverLogger);
77+
78+
var transmitterLogger = _loggerFactory
79+
?.CreateLogger(typeof(SslStreamConnectionAdapter).FullName + ":Transmitter");
7080
var transmitterService = new NonClosingNetworkStreamWriter(
7181
sslStream,
7282
_connectionPipe.Input,
73-
_connectionClosed);
83+
_connectionClosed,
84+
transmitterLogger);
85+
7486
var info = new SslCommunicationInfo(transmitterService, receiverService, sslStream);
7587
_info = info;
7688

src/FubarDev.FtpServer/FtpConnection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ public FtpConnection(
182182
socketPipe,
183183
connectionPipe,
184184
sslStreamWrapperFactory,
185-
_cancellationTokenSource.Token),
185+
_cancellationTokenSource.Token,
186+
loggerFactory),
186187
applicationOutputPipe.Writer);
187188

188189
parentFeatures.Set<IConnectionFeature>(connectionFeature);

src/FubarDev.FtpServer/Networking/PausableFtpService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,24 +169,28 @@ protected abstract Task ExecuteAsync(
169169
protected virtual Task OnStopRequestingAsync(
170170
CancellationToken cancellationToken)
171171
{
172+
Logger?.LogTrace("STOP requesting");
172173
return Task.CompletedTask;
173174
}
174175

175176
protected virtual Task OnStopRequestedAsync(
176177
CancellationToken cancellationToken)
177178
{
179+
Logger?.LogTrace("STOP requested");
178180
return Task.CompletedTask;
179181
}
180182

181183
protected virtual Task OnPauseRequestingAsync(
182184
CancellationToken cancellationToken)
183185
{
186+
Logger?.LogTrace("PAUSE requesting");
184187
return Task.CompletedTask;
185188
}
186189

187190
protected virtual Task OnPauseRequestedAsync(
188191
CancellationToken cancellationToken)
189192
{
193+
Logger?.LogTrace("PAUSE requested");
190194
return Task.CompletedTask;
191195
}
192196

@@ -199,12 +203,14 @@ protected virtual Task OnContinueRequestingAsync(
199203
protected virtual Task OnPausedAsync(
200204
CancellationToken cancellationToken)
201205
{
206+
Logger?.LogTrace("PAUSED");
202207
return Task.CompletedTask;
203208
}
204209

205210
protected virtual Task OnStoppedAsync(
206211
CancellationToken cancellationToken)
207212
{
213+
Logger?.LogTrace("STOPPED");
208214
return Task.CompletedTask;
209215
}
210216

0 commit comments

Comments
 (0)