-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from nenoNaninu/f/cli_console_formatter
change console formatter
- Loading branch information
Showing
4 changed files
with
46 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters