Skip to content

feat: add logging to App #78

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
9 changes: 6 additions & 3 deletions App/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="H.NotifyIcon.WinUI" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.4" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.4" />
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.4" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.250108002" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="WinUIEx" Version="2.5.1" />
</ItemGroup>

Expand Down
63 changes: 55 additions & 8 deletions App/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Coder.Desktop.App.Models;
Expand All @@ -16,6 +17,8 @@
using Microsoft.Win32;
using Microsoft.Windows.AppLifecycle;
using Windows.ApplicationModel.Activation;
using Microsoft.Extensions.Logging;
using Serilog;

namespace Coder.Desktop.App;

Expand All @@ -24,22 +27,51 @@ public partial class App : Application
private readonly IServiceProvider _services;

private bool _handleWindowClosed = true;
private const string MutagenControllerConfigSection = "MutagenController";

private const string logTemplate =
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext} - {Message:lj}{NewLine}{Exception}";

#if !DEBUG
private const string MutagenControllerConfigSection = "AppMutagenController";
private const string ConfigSubKey = @"SOFTWARE\Coder Desktop\App";
private const string logFilename = "app.log";
#else
private const string MutagenControllerConfigSection = "DebugAppMutagenController";
private const string ConfigSubKey = @"SOFTWARE\Coder Desktop\DebugApp";
private const string logFilename = "debug-app.log";
#endif

private readonly ILogger<App> _logger;

public App()
{
var builder = Host.CreateApplicationBuilder();

(builder.Configuration as IConfigurationBuilder).Add(
new RegistryConfigurationSource(Registry.LocalMachine, @"SOFTWARE\Coder Desktop"));
new RegistryConfigurationSource(Registry.LocalMachine, ConfigSubKey));

var services = builder.Services;

// Logging
builder.Services.AddSerilog((_, loggerConfig) =>
{
loggerConfig.ReadFrom.Configuration(builder.Configuration);
var sinkConfig = builder.Configuration.GetSection("Serilog").GetSection("WriteTo");
if (!sinkConfig.GetChildren().Any())
{
// no log sink defined in the registry, so we'll add one here.
// We can't generally define these in the registry because we don't
// know, a priori, what user will execute Coder Desktop, and therefore
// what directories are writable by them. But, it's nice to be able to
// directly customize Serilog via the registry if you know what you are
// doing.
var logPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"CoderDesktop",
logFilename);
loggerConfig.WriteTo.File(logPath, outputTemplate: logTemplate, rollingInterval: RollingInterval.Day);
}
});

services.AddSingleton<ICredentialManager, CredentialManager>();
services.AddSingleton<IRpcController, RpcController>();

Expand Down Expand Up @@ -69,6 +101,7 @@ public App()
services.AddTransient<TrayWindow>();

_services = services.BuildServiceProvider();
_logger = (ILogger<App>)(_services.GetService(typeof(ILogger<App>))!);

InitializeComponent();
}
Expand All @@ -87,6 +120,7 @@ public async Task ExitApplication()

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
_logger.LogInformation("new instance launched");
// Start connecting to the manager in the background.
var rpcController = _services.GetRequiredService<IRpcController>();
if (rpcController.GetState().RpcLifecycle == RpcLifecycle.Disconnected)
Expand All @@ -110,13 +144,15 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
_ = credentialManager.LoadCredentials(credentialManagerCts.Token).ContinueWith(t =>
{
// TODO: log
#if DEBUG
if (t.Exception != null)
{
_logger.LogError(t.Exception, "failed to load credentials");
#if DEBUG
Debug.WriteLine(t.Exception);
Debugger.Break();
}
#endif
}

credentialManagerCts.Dispose();
}, CancellationToken.None);

Expand All @@ -126,9 +162,13 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
_ = syncSessionController.RefreshState(syncSessionCts.Token).ContinueWith(t =>
{
// TODO: log
if (t.IsCanceled || t.Exception != null)
{
_logger.LogError(t.Exception, "failed to refresh sync state (canceled = {canceled})", t.IsCanceled);
#if DEBUG
if (t.IsCanceled || t.Exception != null) Debugger.Break();
Debugger.Break();
#endif
}
syncSessionCts.Dispose();
}, CancellationToken.None);

Expand All @@ -148,17 +188,24 @@ public void OnActivated(object? sender, AppActivationArguments args)
{
case ExtendedActivationKind.Protocol:
var protoArgs = args.Data as IProtocolActivatedEventArgs;
if (protoArgs == null)
{
_logger.LogWarning("URI activation with null data");
return;
}

HandleURIActivation(protoArgs.Uri);
break;

default:
// TODO: log
_logger.LogWarning("activation for {kind}, which is unhandled", args.Kind);
break;
}
}

public void HandleURIActivation(Uri uri)
{
// TODO: handle
// don't log the query string as that's where we include some sensitive information like passwords
_logger.LogInformation("handling URI activation for {path}", uri.AbsolutePath);
}
}
Loading
Loading