Skip to content

Commit

Permalink
Added command line switches to open multiple logs and to enable debug…
Browse files Browse the repository at this point in the history
… console
  • Loading branch information
jschick04 committed Dec 3, 2024
1 parent def382c commit f227c88
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
21 changes: 12 additions & 9 deletions src/EventLogExpert.UI/Services/DebugLogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,32 @@ public sealed partial class DebugLogService : ITraceLogger, IFileLogger, IDispos

private readonly FileLocationOptions _fileLocationOptions;
private readonly Lock _firstChanceLock = new();
private readonly IStateSelection<SettingsState, LogLevel> _logLevelState;

private bool _firstChanceLoggingEnabled;
private LogLevel _logLevel;

public DebugLogService(
FileLocationOptions fileLocationOptions,
IPreferencesProvider preferencesProvider,
IStateSelection<SettingsState, LogLevel> logLevelState)
{
_fileLocationOptions = fileLocationOptions;
_logLevelState = logLevelState;
// Pulling from preferences to make sure Informational isn't overriding desired logging until SettingsState is populated
_logLevel = preferencesProvider.LogLevelPreference;

_logLevelState.Select(state => state.Config.LogLevel);
logLevelState.Select(state => state.Config.LogLevel);

// HACK: Constructor is called before SettingsState gets LogLevel from preferences
// Injecting preferences means we check the saved preferences every time Trace is called, or
// we save the preference on init and an app restart is required to update the logging level
_logLevelState.StateChanged += (_, _) =>
logLevelState.SelectedValueChanged += (_, logLevel) =>
{
if (_logLevel == logLevel) { return; }

if (_firstChanceLoggingEnabled)
{
AppDomain.CurrentDomain.FirstChanceException -= OnFirstChanceException;
}

_logLevel = logLevel;

InitTracing();
};

Expand Down Expand Up @@ -94,7 +97,7 @@ public async IAsyncEnumerable<string> LoadAsync()

public void Trace(string message, LogLevel level = LogLevel.Information)
{
if (level < _logLevelState.Value) { return; }
if (level < _logLevel) { return; }

string output = $"[{DateTime.Now:o}] [{Environment.CurrentManagedThreadId}] [{level}] {message}";

Expand Down Expand Up @@ -131,7 +134,7 @@ private void InitTracing()
// Disabling first chance exception logging unless LogLevel is at Trace level
// since it is noisy and is logging double information for exceptions that are being handled.
// This also saves any potential performance hit for when we aren't worried about tracking first chance exceptions.
if (_logLevelState.Value > LogLevel.Trace) { return; }
if (_logLevel > LogLevel.Trace) { return; }

_firstChanceLoggingEnabled = true;

Expand Down
2 changes: 1 addition & 1 deletion src/EventLogExpert.UI/Services/GitHubService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task<IEnumerable<GitReleaseModel>> GetReleases()
throw new Exception($"Failed to retrieve GitHub releases. StatusCode: {response.StatusCode}");
}

traceLogger.Trace($"{nameof(GetReleases)} Attempt to retrieve {response.RequestMessage?.RequestUri} succeeded: {response.StatusCode}.", LogLevel.Warning);
traceLogger.Trace($"{nameof(GetReleases)} Attempt to retrieve {response.RequestMessage?.RequestUri} succeeded: {response.StatusCode}.");

var stream = await response.Content.ReadAsStreamAsync();
var content = await JsonSerializer.DeserializeAsync<IEnumerable<GitReleaseModel>>(stream);
Expand Down
7 changes: 5 additions & 2 deletions src/EventLogExpert/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,12 @@ await MainThread.InvokeOnMainThreadAsync(() =>

var args = Environment.GetCommandLineArgs();

if (args.Length > 1)
foreach (var arg in args)
{
OpenLog(args[1], PathType.FilePath).AndForget();
if (arg.EndsWith(".evtx"))
{
OpenLog(arg, PathType.FilePath).AndForget();
}
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/EventLogExpert/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ public static MauiApp CreateMauiApp()

builder.Services.AddMauiBlazorWebView();

#if DEBUG
builder.Services.AddBlazorWebViewDeveloperTools();
#endif
if (Environment.GetCommandLineArgs().Contains("/EnableConsole", StringComparer.OrdinalIgnoreCase) ||
Environment.Version.CompareTo(new Version("9.0.0")) == 0)
{
builder.Services.AddBlazorWebViewDeveloperTools();
}

builder.Services.AddFluxor(options =>
{
Expand Down

0 comments on commit f227c88

Please sign in to comment.