Skip to content

Commit

Permalink
Merge pull request #235 from nenoNaninu/f/cli_console_formatter
Browse files Browse the repository at this point in the history
change console formatter
  • Loading branch information
nenoNaninu authored Dec 1, 2024
2 parents 5a903e4 + 63b68ce commit 3ec42ee
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.8.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.2" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
<PackageVersion Include="Microsoft.Build" Version="17.11.4" />
<PackageVersion Include="Microsoft.Build.Locator" Version="1.7.8" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
Expand Down
38 changes: 38 additions & 0 deletions src/Tapper.Generator/Logging/SimpleConsoleAppFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.IO;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging.Console;

namespace Tapper.Logging;

internal static class ConsoleLoggerExtensions
{
public static ILoggingBuilder AddSimpleConsoleApp(this ILoggingBuilder builder)
{
builder.AddConsole(options => options.FormatterName = SimpleConsoleAppFormatter.FormatterName)
.AddConsoleFormatter<SimpleConsoleAppFormatter, ConsoleFormatterOptions>();

return builder;
}
}

internal class SimpleConsoleAppFormatter : ConsoleFormatter
{
public const string FormatterName = "SimpleConsoleApp";

public SimpleConsoleAppFormatter() : base(FormatterName)
{
}

public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter)
{
string message = logEntry.Formatter(logEntry.State, logEntry.Exception);

if (message is null)
{
return;
}

textWriter.WriteLine(message);
}
}
7 changes: 2 additions & 5 deletions src/Tapper.Generator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using Cocona;
using Microsoft.Build.Locator;
using Microsoft.Extensions.Logging;
using Tapper;
using Tapper.Logging;

MSBuildLocator.RegisterDefaults();

var builder = CoconaApp.CreateBuilder();

builder.Logging.AddSimpleConsole(options =>
{
options.SingleLine = true;
});
builder.Logging.AddSimpleConsoleApp();

var app = builder.Build();

Expand Down
3 changes: 3 additions & 0 deletions src/Tapper.Generator/Tapper.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<PackageReference Include="Microsoft.Build.Locator" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 3ec42ee

Please sign in to comment.