Skip to content

ILogger support #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Generate-ReleaseNotes.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
rem See https://github.com/StefH/GitHubReleaseNotes for more information.

SET version=3.0.23
SET version=3.1.0

GitHubReleaseNotes --output ReleaseNotes.md --skip-empty-releases --exclude-labels question invalid doc --version %version%
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ CommandLine Parser Library lets you easily define strongly typed command line ar
See [Quick Start](https://github.com/j-maly/CommandLineParser/wiki) on how to use the library.

Supports the following Frameworks:
* NET 2.0
* NET 3.5
* NET 4.0
* NET 4.5
* NET 4.5.2 and higher
* NETStandard 1.3 and higher
* NETStandard 2.0
* NETStandard 2.1

Expand All @@ -32,7 +26,9 @@ For application with one or two arguments, you could probably manage with some s

This is the way you define arguments for your application:
```csharp
CommandLineParser.CommandLineParser parser = new CommandLineParser.CommandLineParser();
var factory = LoggerFactory.Create(b => b.AddConsole());
ILogger<CommandLineParser.CommandLineParser> logger = factory.CreateLogger<CommandLineParser.CommandLineParser>();
CommandLineParser.CommandLineParser parser = new CommandLineParser.CommandLineParser(logger);
//switch argument is meant for true/false logic
SwitchArgument showArgument = new SwitchArgument('s', "show", "Set whether show or not", true);
ValueArgument<decimal> version = new ValueArgument<decimal>('v', "version", "Set desired version");
Expand Down Expand Up @@ -60,7 +56,7 @@ try
}
catch (CommandLineException e)
{
Console.WriteLine(e.Message);
logger.LogWarning(e.Message);
}
```
You can find more examples of use in the [wiki](https://github.com/j-maly/CommandLineParser/wiki).
Expand Down
12 changes: 6 additions & 6 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
os: Visual Studio 2022

version: 3.0.23.{build}
version: 3.1.0.{build}

configuration:
- Debug
Expand All @@ -15,13 +15,13 @@ build_script:
- cmd: dotnet --info

- cmd: dotnet restore src\CommandLineArgumentsParser\CommandLineArgumentsParser.csproj
- cmd: dotnet build src\CommandLineArgumentsParser\CommandLineArgumentsParser.csproj --configuration %CONFIGURATION% --framework net452
- cmd: dotnet build src\CommandLineArgumentsParser\CommandLineArgumentsParser.csproj --configuration %CONFIGURATION% --framework netstandard1.3
- cmd: dotnet build src\CommandLineArgumentsParser\CommandLineArgumentsParser.csproj --configuration %CONFIGURATION% --framework netstandard2.0
- cmd: dotnet build src\CommandLineArgumentsParser\CommandLineArgumentsParser.csproj --configuration %CONFIGURATION% --framework netstandard2.1

- cmd: dotnet restore src\ParserTest\ParserTest.csproj
- cmd: dotnet build src\ParserTest\ParserTest.csproj --configuration %CONFIGURATION% --framework net452
- cmd: dotnet build src\ParserTest\ParserTest.csproj --configuration %CONFIGURATION% --framework netcoreapp1.1
- cmd: dotnet build src\ParserTest\ParserTest.csproj --configuration %CONFIGURATION% --framework netcoreapp2.1
- cmd: dotnet build src\ParserTest\ParserTest.csproj --configuration %CONFIGURATION% --framework net472
- cmd: dotnet build src\ParserTest\ParserTest.csproj --configuration %CONFIGURATION% --framework net48
- cmd: dotnet build src\ParserTest\ParserTest.csproj --configuration %CONFIGURATION% --framework netcoreapp3.1
- cmd: dotnet build src\ParserTest\ParserTest.csproj --configuration %CONFIGURATION% --framework net6.0

- cmd: dotnet restore src\Tests\Tests.csproj
Expand Down
5 changes: 3 additions & 2 deletions src/CommandLineArgumentsParser/Arguments/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using CommandLineParser.Compatibility;
using CommandLineParser.Exceptions;
using Microsoft.Extensions.Logging;

namespace CommandLineParser.Arguments;

Expand Down Expand Up @@ -251,9 +252,9 @@ public virtual void Parse(IList<string> args, ref int i)
}

/// <summary>
/// Prints information about the argument value to the console.
/// Prints information about the argument value to the log.
/// </summary>
public abstract void PrintValueInfo();
public abstract void PrintValueInfo(ILogger logger);

/// <summary>
/// Initializes the argument. Sets <see cref="Parsed"/> to false. Override in inherited classes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using CommandLineParser.Compatibility;
using CommandLineParser.Exceptions;
using Microsoft.Extensions.Logging;

namespace CommandLineParser.Arguments
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;

namespace CommandLineParser.Arguments
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Logging;
using System.IO;

namespace CommandLineParser.Arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using CommandLineParser.Compatibility;
using CommandLineParser.Exceptions;
using Microsoft.Extensions.Logging;

namespace CommandLineParser.Arguments
{
Expand Down
1 change: 1 addition & 0 deletions src/CommandLineArgumentsParser/Arguments/FileArgument.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Logging;
using System;
using System.IO;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text.RegularExpressions;
using CommandLineParser.Compatibility;
using CommandLineParser.Exceptions;
using Microsoft.Extensions.Logging;

namespace CommandLineParser.Arguments;

Expand Down
7 changes: 4 additions & 3 deletions src/CommandLineArgumentsParser/Arguments/SwitchArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using CommandLineParser.Compatibility;
using CommandLineParser.Exceptions;
using Microsoft.Extensions.Logging;

namespace CommandLineParser.Arguments
{
Expand Down Expand Up @@ -113,11 +114,11 @@ public override void UpdateBoundObject()
}

/// <summary>
/// Prints information about the argument value to the console.
/// Prints information about the argument value to the ILogger.
/// </summary>
public override void PrintValueInfo()
public override void PrintValueInfo(ILogger logger)
{
Console.WriteLine(Messages.EXC_ARG_SWITCH_PRINT, Name, Value ? "1" : "0");
logger.LogInformation(Messages.IL_EXC_ARG_SWITCH_PRINT, Name, Value ? "1" : "0");
}

/// <summary>
Expand Down
9 changes: 5 additions & 4 deletions src/CommandLineArgumentsParser/Arguments/ValueArgument.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommandLineParser.Compatibility;
using CommandLineParser.Exceptions;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down Expand Up @@ -442,18 +443,18 @@ public override void Init()
}

/// <summary>
/// Prints information about the argument value to the console.
/// Prints information about the argument value to the log.
/// </summary>
public override void PrintValueInfo()
public override void PrintValueInfo(ILogger logger)
{
if (!AllowMultiple)
{
Console.WriteLine(Messages.EXC_ARG_VALUE_PRINT, Name, _stringValue, _value, typeof(TValue).Name);
logger.LogInformation(Messages.IL_EXC_ARG_VALUE_PRINT, Name, _stringValue, _value, typeof(TValue).Name);
}
else
{
string valuesString = string.Join(", ", _values.Select(v => v.ToString()).ToArray());
Console.WriteLine(Messages.EXC_ARG_VALUE_PRINT_MULTIPLE, Name, Values.Count, valuesString, typeof(TValue).Name);
logger.LogInformation(Messages.IL_EXC_ARG_VALUE_PRINT_MULTIPLE, Name, Values.Count, valuesString, typeof(TValue).Name);
}
}
}
Expand Down
31 changes: 5 additions & 26 deletions src/CommandLineArgumentsParser/CommandLineArgumentsParser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Authors>Jakub Maly;Stef Heyenrath;NTTAKR</Authors>
<Copyright>Copyright Jakub Maly &amp; Stef Heyenrath © 2018-2022</Copyright>
<Company>Jakub Maly</Company>
<TargetFrameworks>net20;net35;net40;net45;net452;netstandard1.3;netstandard2.0;netstandard2.1</TargetFrameworks>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please bring these older frameworks back.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of these older frameworks are supported. If someone needs to use this library with older frameworks, they can continue using the older version 3.0.x. Since we are using Microsoft.Extensions.Logging.Abstractions, this will force us to support 4.6.1 and above only. That was the reason to retain only .netstandard 2.0 and 2.1 and bump the version to 3.1.x.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@askids
As general remark for NuGet packages, you don't know who is using this library which what framework, so even if frameworks are old, you cannot just remove these.

To solve this, you can make an own interface ILogger + ILogger<T> which mimics the Microsoft.Extensions.Logging.Abstractions for the older frameworks.
With this solution, you can use Microsoft.Extensions.Logging.Abstractions for .netstandard 2.0 and higher, and just use the custom ILogger for the older versions.

And a default implementation for a ConsoleLogger is required to keep the behavior and usage the same without having to use a new constructor.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As general remark for NuGet packages, you don't know who is using this library which what framework, so even if frameworks are old, you cannot just remove these.

I get that. But I don't see this as a version that is continuing the support for old consumers or adding feature to it. This version is meant to be for those people who are upgrading/transitioning to the new model. Adding support for namesake for the old version is not going to help any existing consumers solve any new problem. Why would they even want to upgrade to this version which is not adding any new features for them or fixing any issues? If you want, we can bump the version from 3.1 to 4.0 to show that its a breaking change and update the documentation accordingly.

<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<AssemblyName>CommandLineArgumentsParser</AssemblyName>
<RootNamespace>CommandLineParser</RootNamespace>
<PackageId>CommandLineArgumentsParser</PackageId>
Expand All @@ -16,7 +16,7 @@
<PackageReleaseNotes>See ReleaseNotes.md</PackageReleaseNotes>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/j-maly/CommandLineParser</RepositoryUrl>
<Version>3.0.23</Version>
<Version>3.1.0</Version>
<DebugType>full</DebugType>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>CommandLineArgumentsParser.snk</AssemblyOriginatorKeyFile>
Expand All @@ -30,30 +30,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net20' ">
<PackageReference Include="LinqBridge" Version="1.3.0" />
</ItemGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="Microsoft.CSharp" Version="4.3.0" />
<PackageReference Include="System.Collections" Version="4.3.0" />
<PackageReference Include="System.Console" Version="4.3.0" />
<PackageReference Include="System.Globalization" Version="4.3.0" />
<PackageReference Include="System.IO.FileSystem" Version="4.3.0" />
<PackageReference Include="System.Linq" Version="4.3.0" />
<PackageReference Include="System.Reflection.Extensions" Version="4.3.0" />
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.3.0" />
<PackageReference Include="System.Resources.ResourceManager" Version="4.3.0" />
<PackageReference Include="System.Runtime.Extensions" Version="4.3.0" />
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.3.0" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

</Project>
Loading