-
-
Notifications
You must be signed in to change notification settings - Fork 22
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 #175 from dawedawe/use_ilogger
Use `Microsoft.Extensions.Logging` instead of `printf` based logging infrastructure
- Loading branch information
Showing
21 changed files
with
237 additions
and
121 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
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
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
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
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,80 @@ | ||
module FSharp.Analyzers.Cli.CustomLogging | ||
|
||
open System | ||
open System.IO | ||
open System.Runtime.CompilerServices | ||
open Microsoft.Extensions.Logging | ||
open Microsoft.Extensions.Logging.Console | ||
open Microsoft.Extensions.Logging.Abstractions | ||
open Microsoft.Extensions.Options | ||
|
||
type CustomOptions() = | ||
inherit ConsoleFormatterOptions() | ||
|
||
/// if true: no LogLevel as prefix, colored output according to LogLevel | ||
/// if false: LogLevel as prefix, no colored output | ||
member val UseAnalyzersMsgStyle = false with get, set | ||
|
||
type CustomFormatter(options: IOptionsMonitor<CustomOptions>) as this = | ||
inherit ConsoleFormatter("customName") | ||
|
||
let mutable optionsReloadToken: IDisposable = null | ||
let mutable formatterOptions = options.CurrentValue | ||
let origColor = Console.ForegroundColor | ||
|
||
do optionsReloadToken <- options.OnChange(fun x -> this.ReloadLoggerOptions(x)) | ||
|
||
member private _.ReloadLoggerOptions(opts: CustomOptions) = formatterOptions <- opts | ||
|
||
override this.Write<'TState> | ||
( | ||
logEntry: inref<LogEntry<'TState>>, | ||
_scopeProvider: IExternalScopeProvider, | ||
textWriter: TextWriter | ||
) | ||
= | ||
let message = logEntry.Formatter.Invoke(logEntry.State, logEntry.Exception) | ||
|
||
if formatterOptions.UseAnalyzersMsgStyle then | ||
this.SetColor(textWriter, logEntry.LogLevel) | ||
textWriter.WriteLine(message) | ||
this.ResetColor() | ||
else | ||
this.WritePrefix(textWriter, logEntry.LogLevel) | ||
textWriter.WriteLine(message) | ||
|
||
member private _.WritePrefix(textWriter: TextWriter, logLevel: LogLevel) = | ||
match logLevel with | ||
| LogLevel.Trace -> textWriter.Write("trace: ") | ||
| LogLevel.Debug -> textWriter.Write("debug: ") | ||
| LogLevel.Information -> textWriter.Write("info: ") | ||
| LogLevel.Warning -> textWriter.Write("warn: ") | ||
| LogLevel.Error -> textWriter.Write("error: ") | ||
| LogLevel.Critical -> textWriter.Write("critical: ") | ||
| _ -> () | ||
|
||
// see https://learn.microsoft.com/en-us/dotnet/core/extensions/console-log-formatter | ||
member private _.SetColor(textWriter: TextWriter, logLevel: LogLevel) = | ||
let color = | ||
match logLevel with | ||
| LogLevel.Error -> "\x1B[1m\x1B[31m" // ConsoleColor.Red | ||
| LogLevel.Warning -> "\x1B[33m" // ConsoleColor.DarkYellow | ||
| LogLevel.Information -> "\x1B[1m\x1B[34m" // ConsoleColor.Blue | ||
| LogLevel.Trace -> "\x1B[1m\x1B[36m" // ConsoleColor.Cyan | ||
| _ -> "\x1B[37m" // ConsoleColor.Gray | ||
|
||
textWriter.Write(color) | ||
|
||
member private _.ResetColor() = Console.ForegroundColor <- origColor | ||
|
||
interface IDisposable with | ||
member _.Dispose() = optionsReloadToken.Dispose() | ||
|
||
[<Extension>] | ||
type ConsoleLoggerExtensions = | ||
|
||
[<Extension>] | ||
static member AddCustomFormatter(builder: ILoggingBuilder, configure: Action<CustomOptions>) : ILoggingBuilder = | ||
builder | ||
.AddConsole(fun options -> options.FormatterName <- "customName") | ||
.AddConsoleFormatter<CustomFormatter, CustomOptions>(configure) |
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
Oops, something went wrong.