Skip to content

Commit

Permalink
Logging is an optional component
Browse files Browse the repository at this point in the history
Fixes #63
  • Loading branch information
fubar-coder committed Apr 19, 2019
1 parent a9b911c commit eddadb6
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/FubarDev.FtpServer/PasvListenerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Net.Sockets;
using System.Threading.Tasks;

using JetBrains.Annotations;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

Expand All @@ -19,6 +21,7 @@ namespace FubarDev.FtpServer
/// </summary>
public class PasvListenerFactory : IPasvListenerFactory
{
[CanBeNull]
private readonly ILogger<PasvListenerFactory> _log;

private readonly int _pasvMinPort;
Expand All @@ -33,15 +36,15 @@ public class PasvListenerFactory : IPasvListenerFactory
/// </summary>
/// <param name="serverOptions">FTPServer options.</param>
/// <param name="logger">Logger instance.</param>
public PasvListenerFactory(IOptions<FtpServerOptions> serverOptions, ILogger<PasvListenerFactory> logger)
public PasvListenerFactory(IOptions<FtpServerOptions> serverOptions, ILogger<PasvListenerFactory> logger = null)
{
_log = logger;
if (serverOptions.Value.PasvMinPort > 1023 &&
serverOptions.Value.PasvMaxPort >= serverOptions.Value.PasvMinPort)
{
_pasvMinPort = serverOptions.Value.PasvMinPort;
_pasvMaxPort = serverOptions.Value.PasvMaxPort;
_log.LogInformation($"PASV port range set to {_pasvMinPort}:{_pasvMaxPort}");
_log?.LogInformation($"PASV port range set to {_pasvMinPort}:{_pasvMaxPort}");
_pasvPorts = Enumerable.Range(_pasvMinPort, _pasvMaxPort - _pasvMinPort + 1).ToArray();
}

Expand Down Expand Up @@ -106,14 +109,14 @@ private IPasvListener CreateListenerInRange(IFtpConnection connection)
// retry if the socket is already in use, else throw the underlying exception
if (se.SocketErrorCode != SocketError.AddressAlreadyInUse)
{
_log.LogError(se, "Could not create listener");
_log?.LogError(se, "Could not create listener");
throw;
}
}
}

// if we reach this point, we have not been able to create a listener within range
_log.LogWarning("No free ports available for data connection");
_log?.LogWarning("No free ports available for data connection");
throw new SocketException((int)SocketError.AddressAlreadyInUse);
}
}
Expand Down

0 comments on commit eddadb6

Please sign in to comment.