-
Notifications
You must be signed in to change notification settings - Fork 247
Add logging infrastructure #851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
07aa1b8
Add logging infrastructure
axunonb f157faa
Refactor logging framework and update tests (#852)
axunonb b412cf9
Remove unused `ILoggerT`, implement `IDisposable` pattern for `NullLo…
axunonb d659711
Switch logging framework from NLog to Serilog (#853)
axunonb 3fdb55c
Remove obsolete TestLoggingManager CTOR overloads
axunonb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 @@ | ||
| // | ||
| // Copyright ical.net project maintainers and contributors. | ||
| // Licensed under the MIT license. | ||
| // | ||
| #nullable enable | ||
| using System; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Ical.Net.Tests.Logging.Adapters; | ||
|
|
||
| #pragma warning disable CA2254 // template should be a static expression | ||
|
|
||
| internal class MicrosoftLoggerAdapter(ILogger msLogger) : Ical.Net.Logging.ILogger | ||
| { | ||
| public void Log(Ical.Net.Logging.LogLevel level, string messageTemplate, params object[] args) | ||
| => msLogger.Log(ConvertLogLevel(level), messageTemplate, args); | ||
|
|
||
| public void Log(Ical.Net.Logging.LogLevel level, Exception exception, string messageTemplate, params object[] args) | ||
| => msLogger.Log(ConvertLogLevel(level), exception, messageTemplate, args); | ||
|
|
||
| public bool IsEnabled(Ical.Net.Logging.LogLevel level) => | ||
| msLogger.IsEnabled(ConvertLogLevel(level)); | ||
|
|
||
| private static LogLevel ConvertLogLevel(Ical.Net.Logging.LogLevel level) | ||
| { | ||
| return level switch | ||
| { | ||
| Ical.Net.Logging.LogLevel.Trace => LogLevel.Trace, | ||
| Ical.Net.Logging.LogLevel.Debug => LogLevel.Debug, | ||
| Ical.Net.Logging.LogLevel.Information => LogLevel.Information, | ||
| Ical.Net.Logging.LogLevel.Warning => LogLevel.Warning, | ||
| Ical.Net.Logging.LogLevel.Error => LogLevel.Error, | ||
| Ical.Net.Logging.LogLevel.Critical => LogLevel.Critical, | ||
| _ => LogLevel.None | ||
| }; | ||
| } | ||
| } | ||
| #pragma warning restore CA2254 |
67 changes: 67 additions & 0 deletions
67
Ical.Net.Tests/Logging/Adapters/MicrosoftLoggerFactoryAdapter.cs
This file contains hidden or 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,67 @@ | ||
| // | ||
| // Copyright ical.net project maintainers and contributors. | ||
| // Licensed under the MIT license. | ||
| // | ||
| #nullable enable | ||
| using System; | ||
|
|
||
| namespace Ical.Net.Tests.Logging.Adapters; | ||
|
|
||
| /// <summary> | ||
| /// Adapts an <see cref="Microsoft.Extensions.Logging.ILoggerFactory"/> | ||
| /// to the <see cref="Ical.Net.Logging.ILoggerFactory"/> interface. | ||
| /// </summary> | ||
| /// <remarks>This class provides a bridge between the <see cref="Microsoft.Extensions.Logging"/> framework and the | ||
| /// <see cref="Ical.Net.Logging.ILoggerFactory"/> abstraction. It allows the creation of | ||
| /// <see cref="Ical.Net.Logging.ILoggerFactory"/> instances using the underlying | ||
| /// <see cref="Microsoft.Extensions.Logging.ILoggerFactory"/>. | ||
| /// </remarks> | ||
| internal class MicrosoftLoggerFactoryAdapter : Ical.Net.Logging.ILoggerFactory | ||
| { | ||
| private readonly Microsoft.Extensions.Logging.ILoggerFactory _msLoggerFactory; | ||
| private bool _disposed; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MicrosoftLoggerFactoryAdapter"/> class, using the specified | ||
| /// Microsoft.Extensions.Logging <see cref="Microsoft.Extensions.Logging.ILoggerFactory"/>. | ||
| /// </summary> | ||
| /// <param name="msLoggerFactory">The <see cref="Microsoft.Extensions.Logging.ILoggerFactory"/> instance used to create loggers. | ||
| /// This parameter cannot be <see langword="null"/>. | ||
| /// </param> | ||
| public MicrosoftLoggerFactoryAdapter(Microsoft.Extensions.Logging.ILoggerFactory msLoggerFactory) | ||
| { | ||
| _msLoggerFactory = msLoggerFactory; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a logger instance for the specified category name. | ||
| /// </summary> | ||
| /// <param name="categoryName">The name of the category for the logger.</param> | ||
| /// <returns>A <see cref="Ical.Net.Logging.ILogger"/> instance configured to log messages for the specified category.</returns> | ||
| public Ical.Net.Logging.ILogger CreateLogger(string categoryName) | ||
| { | ||
| var msLogger = _msLoggerFactory.CreateLogger(categoryName); | ||
| return new MicrosoftLoggerAdapter(msLogger); | ||
| } | ||
|
|
||
| protected virtual void Dispose(bool disposing) | ||
| { | ||
| if (_disposed) return; | ||
|
|
||
| if (disposing) | ||
| _msLoggerFactory.Dispose(); | ||
| _disposed = true; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void Dispose() | ||
| { | ||
| Dispose(true); | ||
| GC.SuppressFinalize(this); | ||
| } | ||
|
|
||
| ~MicrosoftLoggerFactoryAdapter() | ||
| { | ||
| Dispose(false); | ||
| } | ||
| } | ||
This file contains hidden or 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,14 @@ | ||
| // Copyright ical.net project maintainers and contributors. | ||
| // Licensed under the MIT license. | ||
|
|
||
| #nullable enable | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Ical.Net.Tests.Logging; | ||
|
|
||
| internal record Filter | ||
| { | ||
| public LogLevel MinLogLevel { get; set; } = LogLevel.Trace; | ||
| public LogLevel MaxLogLevel { get; set; } = LogLevel.Critical; | ||
| public string LoggerNamePattern { get; set; } = "*"; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would we suppress the finalizer?