diff --git a/Tokenizer.Tests/ConcatenationTests.cs b/Tokenizer.Tests/ConcatenationTests.cs index 52171c6..5a6a834 100644 --- a/Tokenizer.Tests/ConcatenationTests.cs +++ b/Tokenizer.Tests/ConcatenationTests.cs @@ -17,7 +17,7 @@ private class Foo [SetUp] public void SetUp() { - SerilogConfig.Init(); + LogConfig.Init(); tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true }); } diff --git a/Tokenizer.Tests/HintTests.cs b/Tokenizer.Tests/HintTests.cs index 3d9e323..15a018e 100644 --- a/Tokenizer.Tests/HintTests.cs +++ b/Tokenizer.Tests/HintTests.cs @@ -17,7 +17,7 @@ private class Student [SetUp] public void SetUp() { - SerilogConfig.Init(); + LogConfig.Init(); tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true }); } diff --git a/Tokenizer.Tests/ListTests.cs b/Tokenizer.Tests/ListTests.cs index bff3aab..b7118ca 100644 --- a/Tokenizer.Tests/ListTests.cs +++ b/Tokenizer.Tests/ListTests.cs @@ -11,7 +11,7 @@ public class ListTests [SetUp] public void SetUp() { - SerilogConfig.Init(); + LogConfig.Init(); tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true }); } diff --git a/Tokenizer.Tests/LogConfig.cs b/Tokenizer.Tests/LogConfig.cs new file mode 100644 index 0000000..bbc291b --- /dev/null +++ b/Tokenizer.Tests/LogConfig.cs @@ -0,0 +1,21 @@ + +using Microsoft.Extensions.Logging; +using Tokens.Logging; + +namespace Tokens +{ + class LogConfig + { + public static void Init() + { + // LoggerFactory should be disposed with the test case. + var loggerFactory = LoggerFactory.Create(builder => + { + builder.SetMinimumLevel(LogLevel.Trace); + builder.AddConsole(options => options.DisableColors = true); + }); + LogProvider.LogLevel = LogLevel.Trace; + LogProvider.Factory = loggerFactory; + } + } +} diff --git a/Tokenizer.Tests/MultilineTests.cs b/Tokenizer.Tests/MultilineTests.cs index 2d849d2..70bed36 100644 --- a/Tokenizer.Tests/MultilineTests.cs +++ b/Tokenizer.Tests/MultilineTests.cs @@ -20,7 +20,7 @@ private class Student [SetUp] public void SetUp() { - SerilogConfig.Init(); + LogConfig.Init(); tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true }); } diff --git a/Tokenizer.Tests/SampleTests.cs b/Tokenizer.Tests/SampleTests.cs index 9088499..4bf2377 100644 --- a/Tokenizer.Tests/SampleTests.cs +++ b/Tokenizer.Tests/SampleTests.cs @@ -13,7 +13,7 @@ public class SampleTests [SetUp] public void SetUp() { - SerilogConfig.Init(); + LogConfig.Init(); tokenizer = new Tokenizer(); } diff --git a/Tokenizer.Tests/SerilogConfig.cs b/Tokenizer.Tests/SerilogConfig.cs deleted file mode 100644 index ab6c8af..0000000 --- a/Tokenizer.Tests/SerilogConfig.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Serilog; - -namespace Tokens -{ - class SerilogConfig - { - public static void Init() - { - Log.Logger = new LoggerConfiguration() - .MinimumLevel - .Verbose() - .WriteTo - .Console() - .CreateLogger(); - } - } -} diff --git a/Tokenizer.Tests/SplitTests.cs b/Tokenizer.Tests/SplitTests.cs index 0b70895..7974397 100644 --- a/Tokenizer.Tests/SplitTests.cs +++ b/Tokenizer.Tests/SplitTests.cs @@ -16,7 +16,7 @@ private class Foo [SetUp] public void SetUp() { - SerilogConfig.Init(); + LogConfig.Init(); tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true }); } diff --git a/Tokenizer.Tests/TokenMatcherTests.cs b/Tokenizer.Tests/TokenMatcherTests.cs index 2ff0fb1..556d37d 100644 --- a/Tokenizer.Tests/TokenMatcherTests.cs +++ b/Tokenizer.Tests/TokenMatcherTests.cs @@ -19,7 +19,7 @@ private class Person [SetUp] public void SetUp() { - SerilogConfig.Init(); + LogConfig.Init(); matcher = new TokenMatcher(); parser = new TokenParser(); diff --git a/Tokenizer.Tests/Tokenizer.Tests.csproj b/Tokenizer.Tests/Tokenizer.Tests.csproj index 24776de..660466f 100644 --- a/Tokenizer.Tests/Tokenizer.Tests.csproj +++ b/Tokenizer.Tests/Tokenizer.Tests.csproj @@ -1,17 +1,18 @@  - netcoreapp2.1 + net6.0 Tokens Library + default + + - - diff --git a/Tokenizer.Tests/TokenizerTests.cs b/Tokenizer.Tests/TokenizerTests.cs index 99b9e9c..26e1cff 100644 --- a/Tokenizer.Tests/TokenizerTests.cs +++ b/Tokenizer.Tests/TokenizerTests.cs @@ -48,7 +48,7 @@ private class Teacher : Student [SetUp] public void SetUp() { - SerilogConfig.Init(); + LogConfig.Init(); tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true }); } diff --git a/Tokenizer.Tests/Transformers/ToDateTimeUtcTransformerTests.cs b/Tokenizer.Tests/Transformers/ToDateTimeUtcTransformerTests.cs index 68284d5..0897311 100644 --- a/Tokenizer.Tests/Transformers/ToDateTimeUtcTransformerTests.cs +++ b/Tokenizer.Tests/Transformers/ToDateTimeUtcTransformerTests.cs @@ -11,7 +11,7 @@ public class ToDateTimeUtcTransformerTests [SetUp] public void SetUp() { - SerilogConfig.Init(); + LogConfig.Init(); @operator = new ToDateTimeUtcTransformer(); } diff --git a/Tokenizer.Tests/Types/BoolTests.cs b/Tokenizer.Tests/Types/BoolTests.cs index b8fa323..2dc34b4 100644 --- a/Tokenizer.Tests/Types/BoolTests.cs +++ b/Tokenizer.Tests/Types/BoolTests.cs @@ -17,7 +17,7 @@ private class Student [SetUp] public void SetUp() { - SerilogConfig.Init(); + LogConfig.Init(); tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true }); } diff --git a/Tokenizer.Tests/Types/EnumTests.cs b/Tokenizer.Tests/Types/EnumTests.cs index 80f92a5..a1c0918 100644 --- a/Tokenizer.Tests/Types/EnumTests.cs +++ b/Tokenizer.Tests/Types/EnumTests.cs @@ -24,7 +24,7 @@ private enum Grade [SetUp] public void SetUp() { - SerilogConfig.Init(); + LogConfig.Init(); tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true }); } diff --git a/Tokenizer.Tests/Validators/IsNumericValidatorTests.cs b/Tokenizer.Tests/Validators/IsNumericValidatorTests.cs index febd217..61f9176 100644 --- a/Tokenizer.Tests/Validators/IsNumericValidatorTests.cs +++ b/Tokenizer.Tests/Validators/IsNumericValidatorTests.cs @@ -11,7 +11,7 @@ public class IsNumericValidatorTests [SetUp] public void SetUp() { - SerilogConfig.Init(); + LogConfig.Init(); validator = new IsNumericValidator(); } diff --git a/Tokenizer/Logging/LogExtensions.cs b/Tokenizer/Logging/LogExtensions.cs index 5076dc2..63bc719 100644 --- a/Tokenizer/Logging/LogExtensions.cs +++ b/Tokenizer/Logging/LogExtensions.cs @@ -2,19 +2,40 @@ using System.Collections.Generic; using System.ComponentModel; using System.Text; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; namespace Tokens.Logging { static class VerboseLogExtensions { - public static void Verbose(this ILog log, string message, params object[] args) + public static void Verbose(this ILogger log, string message, params object[] args) { - log.TraceFormat(string.Concat(LogIndentation.Indentation, message), args); + try + { + log.LogTrace(string.Format(string.Concat(LogIndentation.Indentation, message), args)); + } + catch (System.FormatException e) + { + log.LogWarning($"String has incorrect format: '{message}'"); + } } - public static void Verbose(this ILog log, Exception exception, string message, params object[] args) + public static void Verbose(this ILogger log, Exception exception, string message, params object[] args) { - log.TraceException(string.Concat(LogIndentation.Indentation, message), exception, args); + try + { + log.LogError(string.Format(string.Concat(LogIndentation.Indentation, message), exception, args)); + } + catch (System.FormatException e) + { + log.LogWarning($"String has incorrect format: '{message}'"); + } + } + + public static bool IsDebugEnabled(this ILogger log) + { + return LogProvider.LogLevel == LogLevel.Debug || LogProvider.LogLevel == LogLevel.Trace; } } } diff --git a/Tokenizer/Logging/LogProvider.cs b/Tokenizer/Logging/LogProvider.cs new file mode 100644 index 0000000..f25e950 --- /dev/null +++ b/Tokenizer/Logging/LogProvider.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; + +namespace Tokens.Logging; + +public class LogProvider +{ + public static LogLevel LogLevel = LogLevel.Information; + public static ILoggerFactory Factory = new NullLoggerFactory(); + public static ILogger For() + { + return Factory.CreateLogger(); + } +} \ No newline at end of file diff --git a/Tokenizer/Parsers/TokenParser.cs b/Tokenizer/Parsers/TokenParser.cs index 2755562..a115bc7 100644 --- a/Tokenizer/Parsers/TokenParser.cs +++ b/Tokenizer/Parsers/TokenParser.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Linq; using System.Text; +using Microsoft.Extensions.Logging; using Tokens.Exceptions; using Tokens.Extensions; using Tokens.Logging; @@ -20,7 +21,7 @@ internal class TokenParser private readonly List transformers; private readonly List validators; - private readonly ILog log; + private readonly ILogger log; public TokenizerOptions Options { get; set; } diff --git a/Tokenizer/Token.cs b/Tokenizer/Token.cs index ef22628..d6b89c2 100644 --- a/Tokenizer/Token.cs +++ b/Tokenizer/Token.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Microsoft.Extensions.Logging; using Tokens.Enumerators; using Tokens.Exceptions; using Tokens.Extensions; @@ -13,7 +14,7 @@ namespace Tokens /// public class Token { - private static readonly ILog Log; + private static readonly ILogger Log; private string content; /// @@ -21,7 +22,7 @@ public class Token /// static Token() { - Log = LogProvider.GetLogger(nameof(Token)); + Log = LogProvider.For(); } /// diff --git a/Tokenizer/TokenMatcher.cs b/Tokenizer/TokenMatcher.cs index 4ec7174..b34d89a 100644 --- a/Tokenizer/TokenMatcher.cs +++ b/Tokenizer/TokenMatcher.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using Microsoft.Extensions.Logging; using Tokens.Exceptions; using Tokens.Logging; using Tokens.Parsers; @@ -16,14 +17,14 @@ public class TokenMatcher { private readonly Tokenizer tokenizer; private readonly TokenParser parser; - private readonly ILog log; + private readonly ILogger log; public TokenMatcher() { parser = new TokenParser(); Templates = new TemplateCollection(); tokenizer = new Tokenizer(); - log = LogProvider.GetLogger(typeof(TokenMatcher)); + log = LogProvider.For(); } public TokenMatcher(TokenizerOptions options) : this() @@ -81,7 +82,7 @@ public TokenMatcherResult Match(string input, string[] tags) Template = template }; - log.ErrorException($"Error processing template: {template.Name}", e); + log.LogError($"Error processing template: {template.Name}", e); throw exception; } @@ -145,7 +146,7 @@ public TokenMatcherResult Match(string input, string[] tags) Template = template }; - log.ErrorException($"Error processing template: {template.Name}", e); + log.LogError($"Error processing template: {template.Name}", e); throw exception; } diff --git a/Tokenizer/Tokenizer.cs b/Tokenizer/Tokenizer.cs index f0985a9..5af79ae 100644 --- a/Tokenizer/Tokenizer.cs +++ b/Tokenizer/Tokenizer.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Text; +using Microsoft.Extensions.Logging; using Tokens.Enumerators; using Tokens.Logging; using Tokens.Parsers; @@ -18,7 +19,7 @@ namespace Tokens public class Tokenizer { private readonly TokenParser parser; - private readonly ILog log; + private readonly ILogger log; /// /// Gets or sets the options. diff --git a/Tokenizer/Tokenizer.csproj b/Tokenizer/Tokenizer.csproj index 35786c7..4526dcf 100644 --- a/Tokenizer/Tokenizer.csproj +++ b/Tokenizer/Tokenizer.csproj @@ -1,6 +1,5 @@  - netstandard2.0;net452 true 2.2.2.0 2.2.2 @@ -11,13 +10,9 @@ Tokenizer extracts structured information from blocks of text and reflects them onto .NET objects Chris Wood 2021 Tokens + net6.0 + default - - - all - runtime; build; native; contentfiles; analyzers - - LICENSE.txt @@ -25,4 +20,7 @@ + + +