Skip to content

Commit

Permalink
Upgrade nuget packages and enable nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeFontana committed Nov 18, 2024
1 parent b779673 commit eca26ab
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 46 deletions.
6 changes: 3 additions & 3 deletions ConsoleLoggerDemo/ConsoleLoggerDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 9 additions & 3 deletions ConsoleLoggerLibrary/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public bool IsEnabled(LogLevel logLevel)
return logLevel >= _consoleLoggerProvider.LogMinLevel;
}

public IDisposable BeginScope<TState>(TState state)
IDisposable? ILogger.BeginScope<TState>(TState state)
{
return null;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (IsEnabled(logLevel) == false)
{
Expand All @@ -38,7 +38,13 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except

ArgumentException.ThrowIfNullOrEmpty(nameof(formatter));
string message = formatter(state, exception);
LogMessage logMessage = new(message, exception, logLevel, _categoryName, eventId, _consoleLoggerProvider.UseUtcTimestamp);
LogMessage logMessage = new(
message,
exception,
logLevel,
_categoryName,
eventId,
_consoleLoggerProvider.UseUtcTimestamp);
_consoleLoggerProvider.EnqueueMessage(logMessage);
}
}
30 changes: 15 additions & 15 deletions ConsoleLoggerLibrary/ConsoleLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,65 +64,65 @@ public static ILoggingBuilder AddConsoleLogger(this ILoggingBuilder builder, Act
return builder;
}

public static ILoggingBuilder AddConsoleLogger(this ILoggingBuilder builder, IConfiguration configuration, Action<ConsoleLoggerOptions> configure = null)
public static ILoggingBuilder AddConsoleLogger(this ILoggingBuilder builder, IConfiguration configuration, Action<ConsoleLoggerOptions>? configure = null)
{
ConsoleLoggerProvider consoleLoggerProvider = CreateFromConfiguration(configuration, configure = null);
ConsoleLoggerProvider? consoleLoggerProvider = CreateFromConfiguration(configuration, configure = null);

if (consoleLoggerProvider != null)
{
builder.Services.AddSingleton<ILoggerProvider, ConsoleLoggerProvider>(
sp => consoleLoggerProvider);
builder.SetMinimumLevel(consoleLoggerProvider.LogMinLevel);
}

builder.SetMinimumLevel(consoleLoggerProvider.LogMinLevel);
return builder;
}

private static ConsoleLoggerProvider CreateFromConfiguration(IConfiguration configuration, Action<ConsoleLoggerOptions> configure)
private static ConsoleLoggerProvider? CreateFromConfiguration(IConfiguration configuration, Action<ConsoleLoggerOptions>? configure)
{
IConfigurationSection consoleLogger = configuration.GetSection("Logging:ConsoleLogger");
IConfigurationSection? consoleLogger = configuration.GetSection("Logging:ConsoleLogger");

if (consoleLogger == null)
if (consoleLogger is null)
{
return null;
}

ConsoleLoggerOptions options = new();
string minLevel = consoleLogger["LogMinLevel"];
string? minLevel = consoleLogger["LogMinLevel"];

if (string.IsNullOrWhiteSpace(minLevel) == false
if (string.IsNullOrWhiteSpace(minLevel) == false
&& Enum.TryParse(minLevel, out LogLevel level))
{
options.LogMinLevel = level;
}

string useUtcTimestamp = consoleLogger["UseUtcTimestamp"];
string? useUtcTimestamp = consoleLogger["UseUtcTimestamp"];

if (string.IsNullOrWhiteSpace(useUtcTimestamp) == false
&& bool.TryParse(useUtcTimestamp, out bool useUtcTime))
{
options.UseUtcTimestamp = useUtcTime;
}

string multiLineFormat = consoleLogger["MultilineFormat"];
string? multiLineFormat = consoleLogger["MultilineFormat"];

if (string.IsNullOrWhiteSpace(multiLineFormat) == false
if (string.IsNullOrWhiteSpace(multiLineFormat) == false
&& bool.TryParse(multiLineFormat, out bool multiLine))
{
options.MultiLineFormat = multiLine;
}

string indentMultilineMessages = consoleLogger["IndentMultilineMessages"];
string? indentMultilineMessages = consoleLogger["IndentMultilineMessages"];

if (string.IsNullOrWhiteSpace(indentMultilineMessages) == false
if (string.IsNullOrWhiteSpace(indentMultilineMessages) == false
&& bool.TryParse(indentMultilineMessages, out bool indent))
{
options.IndentMultilineMessages = indent;
}

string enableConsoleColors = consoleLogger["EnableConsoleColors"];
string? enableConsoleColors = consoleLogger["EnableConsoleColors"];

if (string.IsNullOrWhiteSpace(enableConsoleColors) == false
if (string.IsNullOrWhiteSpace(enableConsoleColors) == false
&& bool.TryParse(enableConsoleColors, out bool colors))
{
options.EnableConsoleColors = colors;
Expand Down
18 changes: 9 additions & 9 deletions ConsoleLoggerLibrary/ConsoleLoggerLibrary.csproj
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>
<PackageId>CodeFoxtrot.ConsoleLogger</PackageId>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
<Authors>CodeFoxtrot</Authors>
<Company>CodeFoxtrot</Company>
<Product>ConsoleLogger</Product>
<Description>Simple ILogger implementation, providing asynchronous and configurable Console logging capabilities.</Description>
<Copyright>2023</Copyright>
<Copyright>2024</Copyright>
<PackageProjectUrl>https://github.com/CodeFontana/ConsoleLogger</PackageProjectUrl>
<RepositoryUrl>https://github.com/CodeFontana/ConsoleLogger</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>CodeFoxtrot Logging Log ConsoleLogger Logger</PackageTags>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyVersion>1.0.5.0</AssemblyVersion>
<FileVersion>1.0.5.0</FileVersion>
<AssemblyVersion>1.0.6.0</AssemblyVersion>
<FileVersion>1.0.6.0</FileVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion ConsoleLoggerLibrary/ConsoleLoggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public sealed class ConsoleLoggerOptions
[LogLevel.None] = ConsoleColor.White
};

public Func<LogMessage, string> LogEntryFormatter { get; set; }
public Func<LogMessage, string>? LogEntryFormatter { get; set; }
}
26 changes: 13 additions & 13 deletions ConsoleLoggerLibrary/ConsoleLoggerProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Runtime.Versioning;
using Microsoft.Extensions.Logging;

namespace ConsoleLoggerLibrary;

Expand All @@ -17,7 +17,7 @@ internal sealed class ConsoleLoggerProvider : ILoggerProvider, IDisposable
public bool MultiLineFormat { get; set; } = false;
public bool IndentMultilineMessages { get; set; } = true;
public bool EnableConsoleColors { get; set; } = true;
public Func<LogMessage, string> LogEntryFormatter { get; set; }
public Func<LogMessage, string>? LogEntryFormatter { get; set; }

public Dictionary<LogLevel, ConsoleColor> LogLevelColors { get; set; } = new()
{
Expand All @@ -35,17 +35,17 @@ public ConsoleLoggerProvider(LogLevel logMinLevel = LogLevel.Trace,
bool multiLineFormat = false,
bool indentMultilineMessages = true,
bool enableConsoleColors = true,
Func<LogMessage, string> logEntryFormatter = null): this(new()
Func<LogMessage, string>? logEntryFormatter = null) : this(new()
{
LogMinLevel = logMinLevel,
UseUtcTimestamp = useUtcTimestamp,
MultiLineFormat = multiLineFormat,
IndentMultilineMessages = indentMultilineMessages,
EnableConsoleColors = enableConsoleColors,
LogEntryFormatter = logEntryFormatter
LogMinLevel = logMinLevel,
UseUtcTimestamp = useUtcTimestamp,
MultiLineFormat = multiLineFormat,
IndentMultilineMessages = indentMultilineMessages,
EnableConsoleColors = enableConsoleColors,
LogEntryFormatter = logEntryFormatter
})
{

}

public ConsoleLoggerProvider(ConsoleLoggerOptions options)
Expand All @@ -60,9 +60,9 @@ public ConsoleLoggerProvider(ConsoleLoggerOptions options)
_processMessages = Task.Factory.StartNew(DequeueMessages, this, TaskCreationOptions.LongRunning);
}

private static void DequeueMessages(object state)
private static void DequeueMessages(object? state)
{
ConsoleLoggerProvider consoleLogger = (ConsoleLoggerProvider)state;
ConsoleLoggerProvider consoleLogger = (ConsoleLoggerProvider)state!;
consoleLogger.DequeueMessages();
}

Expand Down
4 changes: 2 additions & 2 deletions ConsoleLoggerLibrary/LogMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ConsoleLoggerLibrary;
public sealed class LogMessage
{
public string Message { get; init; }
public Exception Exception { get; init; }
public Exception? Exception { get; init; }
public LogLevel LogLevel { get; init; }
public string CategoryName { get; init; }
public EventId EventId { get; init; }
Expand All @@ -17,7 +17,7 @@ public sealed class LogMessage
/// Default constructor, builds a log message and publishes properties
/// for each distinct part of the message structure.
/// </summary>
public LogMessage(string message, Exception exception, LogLevel logLevel, string categoryName, EventId eventId, bool useUtcTimestamp)
public LogMessage(string message, Exception? exception, LogLevel logLevel, string categoryName, EventId eventId, bool useUtcTimestamp)
{
Message = message;
Exception = exception;
Expand Down

0 comments on commit eca26ab

Please sign in to comment.