Skip to content
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 Tokenizer.Tests/ConcatenationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private class Foo
[SetUp]
public void SetUp()
{
SerilogConfig.Init();
LogConfig.Init();

tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true });
}
Expand Down
2 changes: 1 addition & 1 deletion Tokenizer.Tests/HintTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private class Student
[SetUp]
public void SetUp()
{
SerilogConfig.Init();
LogConfig.Init();

tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true });
}
Expand Down
2 changes: 1 addition & 1 deletion Tokenizer.Tests/ListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ListTests
[SetUp]
public void SetUp()
{
SerilogConfig.Init();
LogConfig.Init();

tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true });
}
Expand Down
21 changes: 21 additions & 0 deletions Tokenizer.Tests/LogConfig.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
2 changes: 1 addition & 1 deletion Tokenizer.Tests/MultilineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private class Student
[SetUp]
public void SetUp()
{
SerilogConfig.Init();
LogConfig.Init();

tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true });
}
Expand Down
2 changes: 1 addition & 1 deletion Tokenizer.Tests/SampleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class SampleTests
[SetUp]
public void SetUp()
{
SerilogConfig.Init();
LogConfig.Init();

tokenizer = new Tokenizer();
}
Expand Down
17 changes: 0 additions & 17 deletions Tokenizer.Tests/SerilogConfig.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Tokenizer.Tests/SplitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private class Foo
[SetUp]
public void SetUp()
{
SerilogConfig.Init();
LogConfig.Init();

tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true });
}
Expand Down
2 changes: 1 addition & 1 deletion Tokenizer.Tests/TokenMatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private class Person
[SetUp]
public void SetUp()
{
SerilogConfig.Init();
LogConfig.Init();

matcher = new TokenMatcher();
parser = new TokenParser();
Expand Down
7 changes: 4 additions & 3 deletions Tokenizer.Tests/Tokenizer.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Tokens</RootNamespace>
<ApplicationIcon />
<OutputType>Library</OutputType>
<StartupObject />
<LangVersion>default</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Tokenizer\Tokenizer.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion Tokenizer.Tests/TokenizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private class Teacher : Student
[SetUp]
public void SetUp()
{
SerilogConfig.Init();
LogConfig.Init();

tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ToDateTimeUtcTransformerTests
[SetUp]
public void SetUp()
{
SerilogConfig.Init();
LogConfig.Init();

@operator = new ToDateTimeUtcTransformer();
}
Expand Down
2 changes: 1 addition & 1 deletion Tokenizer.Tests/Types/BoolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private class Student
[SetUp]
public void SetUp()
{
SerilogConfig.Init();
LogConfig.Init();

tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true });
}
Expand Down
2 changes: 1 addition & 1 deletion Tokenizer.Tests/Types/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private enum Grade
[SetUp]
public void SetUp()
{
SerilogConfig.Init();
LogConfig.Init();

tokenizer = new Tokenizer(new TokenizerOptions{ EnableLogging = true });
}
Expand Down
2 changes: 1 addition & 1 deletion Tokenizer.Tests/Validators/IsNumericValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class IsNumericValidatorTests
[SetUp]
public void SetUp()
{
SerilogConfig.Init();
LogConfig.Init();

validator = new IsNumericValidator();
}
Expand Down
29 changes: 25 additions & 4 deletions Tokenizer/Logging/LogExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
14 changes: 14 additions & 0 deletions Tokenizer/Logging/LogProvider.cs
Original file line number Diff line number Diff line change
@@ -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<T> For<T>()
{
return Factory.CreateLogger<T>();
}
}
3 changes: 2 additions & 1 deletion Tokenizer/Parsers/TokenParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,7 +21,7 @@ internal class TokenParser
private readonly List<Type> transformers;
private readonly List<Type> validators;

private readonly ILog log;
private readonly ILogger<TokenParser> log;

public TokenizerOptions Options { get; set; }

Expand Down
5 changes: 3 additions & 2 deletions Tokenizer/Token.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Tokens.Enumerators;
using Tokens.Exceptions;
using Tokens.Extensions;
Expand All @@ -13,15 +14,15 @@ namespace Tokens
/// </summary>
public class Token
{
private static readonly ILog Log;
private static readonly ILogger<Token> Log;
private string content;

/// <summary>
/// Sets the token logger
/// </summary>
static Token()
{
Log = LogProvider.GetLogger(nameof(Token));
Log = LogProvider.For<Token>();
}

/// <summary>
Expand Down
9 changes: 5 additions & 4 deletions Tokenizer/TokenMatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using Microsoft.Extensions.Logging;
using Tokens.Exceptions;
using Tokens.Logging;
using Tokens.Parsers;
Expand All @@ -16,14 +17,14 @@ public class TokenMatcher
{
private readonly Tokenizer tokenizer;
private readonly TokenParser parser;
private readonly ILog log;
private readonly ILogger<TokenMatcher> log;

public TokenMatcher()
{
parser = new TokenParser();
Templates = new TemplateCollection();
tokenizer = new Tokenizer();
log = LogProvider.GetLogger(typeof(TokenMatcher));
log = LogProvider.For<TokenMatcher>();
}

public TokenMatcher(TokenizerOptions options) : this()
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion Tokenizer/Tokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +19,7 @@ namespace Tokens
public class Tokenizer
{
private readonly TokenParser parser;
private readonly ILog log;
private readonly ILogger<Tokenizer> log;

/// <summary>
/// Gets or sets the options.
Expand Down
12 changes: 5 additions & 7 deletions Tokenizer/Tokenizer.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net452</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.2.2.0</Version>
<PackageVersion>2.2.2</PackageVersion>
Expand All @@ -11,18 +10,17 @@
<Description>Tokenizer extracts structured information from blocks of text and reflects them onto .NET objects</Description>
<Copyright>Chris Wood 2021</Copyright>
<RootNamespace>Tokens</RootNamespace>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>default</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LibLog" Version="5.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<PropertyGroup>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
</PropertyGroup>
<ItemGroup>
<None Include="LICENSE.txt" Pack="true" PackagePath="$(PackageLicenseFile)" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.2" />
</ItemGroup>
</Project>