Skip to content

Commit

Permalink
Merge pull request #1114 from jnyrup/logging
Browse files Browse the repository at this point in the history
Extract dependency on MELA to own package
  • Loading branch information
robinrodricks authored Dec 23, 2022
2 parents fc354da + 5c248df commit 7a40a73
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 51 deletions.
19 changes: 19 additions & 0 deletions FluentFTP.Logging/FluentFTP.Logging.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>10.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<!-- Should reference the first version of FluentFTP that exposes IFluentFtpLogger -->
<!--<PackageReference Include="FluentFTP" Version="42.2.0" />-->
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
<!-- this is just to make it compile, FluentFTP should be referenced through a <PackageReference> -->
<ProjectReference Include="..\FluentFTP\FluentFTP.csproj" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions FluentFTP.Logging/MicrosoftLoggingAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.Extensions.Logging;

namespace FluentFTP.Logging {
public sealed class MicrosoftLoggingAdapter : IFluentLogger {
private readonly ILogger adaptee;

public MicrosoftLoggingAdapter(ILogger adaptee) =>
this.adaptee = adaptee;

public void Log(LogEntry entry) =>
adaptee.Log(ToLevel(entry.Severity), 0, entry.Message, entry.Exception, (s, _) => s);

private static LogLevel ToLevel(FtpTraceLevel s) => s switch {
FtpTraceLevel.Verbose => LogLevel.Debug,
FtpTraceLevel.Info => LogLevel.Information,
FtpTraceLevel.Warn => LogLevel.Warning,
FtpTraceLevel.Error => LogLevel.Error,
_ => LogLevel.Information
};
}
}
14 changes: 12 additions & 2 deletions FluentFTP.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentFTP", "FluentFTP\Flue
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpExamples", "FluentFTP.CSharpExamples\CSharpExamples.csproj", "{49B11591-C942-479F-A864-AB2738E50EF0}"
EndProject
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "VBExamples", "FluentFTP.VBExamples\VBExamples.vbproj", "{FC62AA30-F9F0-4DC1-A9EA-51A24C624973}"
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "VBExamples", "FluentFTP.VBExamples\VBExamples.vbproj", "{FC62AA30-F9F0-4DC1-A9EA-51A24C624973}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentFTP.Tests", "FluentFTP.Tests\FluentFTP.Tests.csproj", "{0B17384E-6849-46D0-B880-2F97FE11A1B3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentFTP.Xunit", "FluentFTP.Xunit\FluentFTP.Xunit.csproj", "{78BBB136-352C-4C06-A133-630C8AC57575}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentFTP.Xunit", "FluentFTP.Xunit\FluentFTP.Xunit.csproj", "{78BBB136-352C-4C06-A133-630C8AC57575}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentFTP.Logging", "FluentFTP.Logging\FluentFTP.Logging.csproj", "{611163CE-C9A0-45A6-9090-F7068AEF8D6B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -61,6 +63,14 @@ Global
{78BBB136-352C-4C06-A133-630C8AC57575}.Release|Any CPU.Build.0 = Release|Any CPU
{78BBB136-352C-4C06-A133-630C8AC57575}.Release|x64.ActiveCfg = Release|Any CPU
{78BBB136-352C-4C06-A133-630C8AC57575}.Release|x64.Build.0 = Release|Any CPU
{611163CE-C9A0-45A6-9090-F7068AEF8D6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{611163CE-C9A0-45A6-9090-F7068AEF8D6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{611163CE-C9A0-45A6-9090-F7068AEF8D6B}.Debug|x64.ActiveCfg = Debug|Any CPU
{611163CE-C9A0-45A6-9090-F7068AEF8D6B}.Debug|x64.Build.0 = Debug|Any CPU
{611163CE-C9A0-45A6-9090-F7068AEF8D6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{611163CE-C9A0-45A6-9090-F7068AEF8D6B}.Release|Any CPU.Build.0 = Release|Any CPU
{611163CE-C9A0-45A6-9090-F7068AEF8D6B}.Release|x64.ActiveCfg = Release|Any CPU
{611163CE-C9A0-45A6-9090-F7068AEF8D6B}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
8 changes: 4 additions & 4 deletions FluentFTP/Client/AsyncFtpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Net;
using System.Threading;
using FluentFTP.Client.BaseClient;
using Microsoft.Extensions.Logging;
using FluentFTP.Logging;

namespace FluentFTP {

Expand Down Expand Up @@ -32,7 +32,7 @@ public AsyncFtpClient() : base(null) {
/// <summary>
/// Creates a new instance of an async FTP Client, with the given host and credentials.
/// </summary>
public AsyncFtpClient(string host, int port = 0, FtpConfig config = null, ILogger logger = null) : base(config) {
public AsyncFtpClient(string host, int port = 0, FtpConfig config = null, IFluentLogger logger = null) : base(config) {

// set host
Host = host ?? throw new ArgumentNullException(nameof(host));
Expand All @@ -49,7 +49,7 @@ public AsyncFtpClient(string host, int port = 0, FtpConfig config = null, ILogge
/// <summary>
/// Creates a new instance of an async FTP Client, with the given host and credentials.
/// </summary>
public AsyncFtpClient(string host, string user, string pass, int port = 0, FtpConfig config = null, ILogger logger = null) : base(config) {
public AsyncFtpClient(string host, string user, string pass, int port = 0, FtpConfig config = null, IFluentLogger logger = null) : base(config) {

// set host
Host = host ?? throw new ArgumentNullException(nameof(host));
Expand All @@ -71,7 +71,7 @@ public AsyncFtpClient(string host, string user, string pass, int port = 0, FtpCo
/// <summary>
/// Creates a new instance of an async FTP Client, with the given host and credentials.
/// </summary>
public AsyncFtpClient(string host, NetworkCredential credentials, int port = 0, FtpConfig config = null, ILogger logger = null) : base(config) {
public AsyncFtpClient(string host, NetworkCredential credentials, int port = 0, FtpConfig config = null, IFluentLogger logger = null) : base(config) {

// set host
Host = host ?? throw new ArgumentNullException(nameof(host));
Expand Down
31 changes: 4 additions & 27 deletions FluentFTP/Client/BaseClient/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using System.Reflection;
using FluentFTP.Helpers;
using Microsoft.Extensions.Logging;
using FluentFTP.Logging;

namespace FluentFTP.Client.BaseClient {
public partial class BaseFtpClient {
Expand All @@ -28,7 +28,7 @@ protected void LogFunction(string function, object[] args = null) {
var fullMessage = ("> " + function + "(" + args.ItemsToString().Join(", ") + ")");

// log to modern logger if given
m_logger?.LogInformation(fullMessage);
m_logger?.Log(FtpTraceLevel.Info, fullMessage);

// log to legacy logger if given
m_legacyLogger?.Invoke(FtpTraceLevel.Verbose, fullMessage);
Expand All @@ -48,7 +48,7 @@ protected void Log(FtpTraceLevel eventType, string message) {

// log to modern logger if given
if (m_logger != null) {
LogToLogger(eventType, message);
m_logger.Log(eventType, message);
}

// log to legacy logger if given
Expand All @@ -69,7 +69,7 @@ protected void LogWithPrefix(FtpTraceLevel eventType, string message) {

// log to attached logger if given
if (m_logger != null) {
LogToLogger(eventType, fullMessage);
m_logger.Log(eventType, message);
}

// log to legacy logger if given
Expand All @@ -79,29 +79,6 @@ protected void LogWithPrefix(FtpTraceLevel eventType, string message) {
LogToDebugOrConsole(fullMessage);
}

/// <summary>
/// Log a message to the attached logger.
/// </summary>
private void LogToLogger(FtpTraceLevel eventType, string message) {
switch (eventType) {
case FtpTraceLevel.Verbose:
m_logger.LogDebug(message);
break;

case FtpTraceLevel.Info:
m_logger.LogInformation(message);
break;

case FtpTraceLevel.Warn:
m_logger.LogWarning(message);
break;

case FtpTraceLevel.Error:
m_logger.LogError(message);
break;
}
}

/// <summary>
/// Log a message to the debug output and console.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions FluentFTP/Client/BaseClient/Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
using FluentFTP.Servers;
using FluentFTP.Helpers;
using System.Net.Sockets;
using Microsoft.Extensions.Logging;
using System.Threading;
using FluentFTP.Logging;

namespace FluentFTP.Client.BaseClient {

#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

public partial class BaseFtpClient {

private ILogger m_logger = null;
private IFluentLogger m_logger = null;

/// <summary>
/// Should the function calls be logged in Verbose mode?
/// </summary>
public ILogger Logger {
public IFluentLogger Logger {
get => m_logger;
set => m_logger = value;
}
Expand Down
8 changes: 4 additions & 4 deletions FluentFTP/Client/FtpClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Net;
using FluentFTP.Client.BaseClient;
using Microsoft.Extensions.Logging;
using FluentFTP.Logging;

namespace FluentFTP {

Expand Down Expand Up @@ -31,7 +31,7 @@ public FtpClient() : base(null) {
/// <summary>
/// Creates a new instance of a synchronous FTP Client, with the given host and credentials.
/// </summary>
public FtpClient(string host, int port = 0, FtpConfig config = null, ILogger logger = null) : base(config) {
public FtpClient(string host, int port = 0, FtpConfig config = null, IFluentLogger logger = null) : base(config) {

// set host
Host = host ?? throw new ArgumentNullException(nameof(host));
Expand All @@ -48,7 +48,7 @@ public FtpClient(string host, int port = 0, FtpConfig config = null, ILogger log
/// <summary>
/// Creates a new instance of a synchronous FTP Client, with the given host and credentials.
/// </summary>
public FtpClient(string host, string user, string pass, int port = 0, FtpConfig config = null, ILogger logger = null) : base(config) {
public FtpClient(string host, string user, string pass, int port = 0, FtpConfig config = null, IFluentLogger logger = null) : base(config) {

// set host
Host = host ?? throw new ArgumentNullException(nameof(host));
Expand All @@ -70,7 +70,7 @@ public FtpClient(string host, string user, string pass, int port = 0, FtpConfig
/// <summary>
/// Creates a new instance of a synchronous FTP Client, with the given host and credentials.
/// </summary>
public FtpClient(string host, NetworkCredential credentials, int port = 0, FtpConfig config = null, ILogger logger = null) : base(config) {
public FtpClient(string host, NetworkCredential credentials, int port = 0, FtpConfig config = null, IFluentLogger logger = null) : base(config) {

// set host
Host = host ?? throw new ArgumentNullException(nameof(host));
Expand Down
3 changes: 1 addition & 2 deletions FluentFTP/Client/Interfaces/IAsyncFtpClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using FluentFTP.Rules;
using FluentFTP.Servers;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down Expand Up @@ -31,7 +30,7 @@ public interface IAsyncFtpClient : IDisposable, IBaseFtpClient {
Task<List<FtpProfile>> AutoDetect(bool firstOnly = true, bool cloneConnection = true, CancellationToken token = default(CancellationToken));
Task Connect(CancellationToken token = default(CancellationToken));
Task Connect(FtpProfile profile, CancellationToken token = default(CancellationToken));
Task Connect(bool reConnect, CancellationToken token = default(CancellationToken));
Task Connect(bool reConnect, CancellationToken token = default(CancellationToken));
Task Disconnect(CancellationToken token = default(CancellationToken));
Task<FtpReply> Execute(string command, CancellationToken token = default(CancellationToken));
Task<FtpReply> GetReply(CancellationToken token = default(CancellationToken));
Expand Down
6 changes: 3 additions & 3 deletions FluentFTP/Client/Interfaces/IBaseFtpClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FluentFTP.Rules;
using FluentFTP.Logging;
using FluentFTP.Rules;
using FluentFTP.Servers;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand All @@ -22,7 +22,7 @@ public interface IBaseFtpClient {
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

FtpConfig Config { get; set; }
ILogger Logger { get; set; }
IFluentLogger Logger { get; set; }
bool IsDisposed { get; }
bool IsConnected { get; }
string Host { get; set; }
Expand Down
3 changes: 1 addition & 2 deletions FluentFTP/Client/Interfaces/IFtpClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using FluentFTP.Rules;
using FluentFTP.Servers;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down Expand Up @@ -31,7 +30,7 @@ public interface IFtpClient : IDisposable, IBaseFtpClient {
FtpProfile AutoConnect();
void Connect();
void Connect(FtpProfile profile);
void Connect(bool reConnect);
void Connect(bool reConnect);
void Disconnect();
FtpReply Execute(string command);
FtpReply GetReply();
Expand Down
4 changes: 0 additions & 4 deletions FluentFTP/FluentFTP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.0" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Configuration)'=='Release' And '$(TargetFramework)'=='net45'">
<Exec Command="copy /Y &quot;$(ProjectDir)bin\Release\net45\FluentFTP.dll&quot; &quot;$(SolutionDir)Powershell\FluentFTP.dll&quot;" />
</Target>
Expand Down
5 changes: 5 additions & 0 deletions FluentFTP/Logging/IFluentLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace FluentFTP.Logging {
public interface IFluentLogger {
void Log(LogEntry entry);
}
}
15 changes: 15 additions & 0 deletions FluentFTP/Logging/LogEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace FluentFTP.Logging {
public readonly struct LogEntry {
public FtpTraceLevel Severity { get; }
public string Message { get; }
public Exception Exception { get; }

public LogEntry(FtpTraceLevel severity, string msg, Exception ex = null) {
Severity = severity;
Message = msg;
Exception = ex;
}
}
}
8 changes: 8 additions & 0 deletions FluentFTP/Logging/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace FluentFTP.Logging {
public static class LoggerExtensions {
public static void Log(this IFluentLogger logger, FtpTraceLevel eventType, string message, Exception ex = null) =>
logger.Log(new LogEntry(eventType, message, ex));
}
}

0 comments on commit 7a40a73

Please sign in to comment.