-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add FileLoggingProvider for MacCatalyst UI test logging #33518
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d93ca8f
Add FileLoggingProvider for MacCatalyst UI test logging
PureWeen d088a72
Address Copilot review feedback on FileLoggingProvider
PureWeen 646b8f8
Allow 'maccatalyst' as platform alias in BuildAndRunHostApp.ps1
PureWeen 017e688
Fix pr agent description formatting
PureWeen 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
Some comments aren't visible on the classic Files Changed page.
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
100 changes: 100 additions & 0 deletions
100
src/Controls/tests/TestCases.HostApp/FileLoggingProvider.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,100 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Maui.Controls.Sample; | ||
|
|
||
| /// <summary> | ||
| /// A simple file-based logging provider for capturing ILogger output during UI tests. | ||
| /// Enabled when the MAUI_LOG_FILE environment variable is set to the desired log file path. | ||
| /// </summary> | ||
| internal class FileLoggingProvider : ILoggerProvider | ||
| { | ||
| private readonly StreamWriter _writer; | ||
| private readonly LogLevel _minLevel; | ||
|
|
||
| public FileLoggingProvider(string filePath, LogLevel minLevel = LogLevel.Debug) | ||
| { | ||
| _minLevel = minLevel; | ||
| var directory = Path.GetDirectoryName(filePath); | ||
| if (!string.IsNullOrEmpty(directory)) | ||
| { | ||
| Directory.CreateDirectory(directory); | ||
| } | ||
| _writer = new StreamWriter(filePath, append: false) { AutoFlush = true }; | ||
| _writer.WriteLine($"=== MAUI HostApp File Logger Started at {DateTime.Now} ==="); | ||
| _writer.WriteLine($"Log file: {filePath}"); | ||
| _writer.WriteLine($"Minimum log level: {minLevel}"); | ||
| _writer.WriteLine(); | ||
| } | ||
|
|
||
| public ILogger CreateLogger(string categoryName) | ||
| { | ||
| return new FileLogger(categoryName, _writer, _minLevel); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| _writer?.Dispose(); | ||
| } | ||
| } | ||
|
|
||
| internal class FileLogger : ILogger | ||
| { | ||
| private readonly string _categoryName; | ||
| private readonly StreamWriter _writer; | ||
| private readonly LogLevel _minLevel; | ||
| private static readonly object _lock = new object(); | ||
PureWeen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public FileLogger(string categoryName, StreamWriter writer, LogLevel minLevel) | ||
| { | ||
| _categoryName = categoryName; | ||
| _writer = writer; | ||
| _minLevel = minLevel; | ||
| } | ||
|
|
||
| public IDisposable BeginScope<TState>(TState state) where TState : notnull => NullScope.Instance; | ||
|
|
||
| public bool IsEnabled(LogLevel logLevel) => logLevel >= _minLevel; | ||
|
|
||
| public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) | ||
| { | ||
| if (!IsEnabled(logLevel)) | ||
| return; | ||
|
|
||
| var message = formatter(state, exception); | ||
| var levelString = logLevel switch | ||
| { | ||
| LogLevel.Trace => "TRACE", | ||
| LogLevel.Debug => "DEBUG", | ||
| LogLevel.Information => "INFO", | ||
| LogLevel.Warning => "WARN", | ||
| LogLevel.Error => "ERROR", | ||
| LogLevel.Critical => "CRIT", | ||
| _ => "NONE" | ||
| }; | ||
|
|
||
| var timestamp = DateTime.Now.ToString("HH:mm:ss.fff"); | ||
| var logLine = $"[{timestamp}] [{levelString}] {_categoryName}: {message}"; | ||
|
|
||
| lock (_lock) | ||
| { | ||
| _writer.WriteLine(logLine); | ||
| if (exception != null) | ||
| { | ||
| _writer.WriteLine($" Exception: {exception}"); | ||
| } | ||
| } | ||
|
|
||
| // Also write to console for local debugging visibility | ||
| Console.WriteLine(logLine); | ||
| if (exception != null) | ||
| { | ||
| Console.WriteLine($" Exception: {exception}"); | ||
PureWeen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| private class NullScope : IDisposable | ||
| { | ||
| public static NullScope Instance { get; } = new NullScope(); | ||
| public void Dispose() { } | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.