Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5e1ba1d

Browse files
committed
feat: add logging to App
1 parenta58864e commit5e1ba1d

File tree

8 files changed

+695
-564
lines changed

8 files changed

+695
-564
lines changed

‎App/App.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@
6161
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
6262
</PackageReference>
6363
<PackageReferenceInclude="H.NotifyIcon.WinUI"Version="2.2.0" />
64-
<PackageReferenceInclude="Microsoft.Extensions.DependencyInjection"Version="9.0.1" />
65-
<PackageReferenceInclude="Microsoft.Extensions.Hosting"Version="9.0.1" />
66-
<PackageReferenceInclude="Microsoft.Extensions.Options"Version="9.0.1" />
64+
<PackageReferenceInclude="Microsoft.Extensions.DependencyInjection"Version="9.0.4" />
65+
<PackageReferenceInclude="Microsoft.Extensions.Hosting"Version="9.0.4" />
66+
<PackageReferenceInclude="Microsoft.Extensions.Options"Version="9.0.4" />
6767
<PackageReferenceInclude="Microsoft.WindowsAppSDK"Version="1.6.250108002" />
68+
<PackageReferenceInclude="Serilog.Extensions.Hosting"Version="9.0.0" />
69+
<PackageReferenceInclude="Serilog.Settings.Configuration"Version="9.0.0" />
70+
<PackageReferenceInclude="Serilog.Sinks.File"Version="6.0.0" />
6871
<PackageReferenceInclude="WinUIEx"Version="2.5.1" />
6972
</ItemGroup>
7073

‎App/App.xaml.cs

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
usingSystem;
22
usingSystem.Diagnostics;
33
usingSystem.IO;
4+
usingSystem.Linq;
45
usingSystem.Threading;
56
usingSystem.Threading.Tasks;
67
usingCoder.Desktop.App.Models;
@@ -16,6 +17,8 @@
1617
usingMicrosoft.Win32;
1718
usingMicrosoft.Windows.AppLifecycle;
1819
usingWindows.ApplicationModel.Activation;
20+
usingMicrosoft.Extensions.Logging;
21+
usingSerilog;
1922

2023
namespaceCoder.Desktop.App;
2124

@@ -24,22 +27,51 @@ public partial class App : Application
2427
privatereadonlyIServiceProvider_services;
2528

2629
privatebool_handleWindowClosed=true;
30+
privateconststringMutagenControllerConfigSection="MutagenController";
31+
32+
privateconststringlogTemplate=
33+
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext} - {Message:lj}{NewLine}{Exception}";
2734

2835
#if!DEBUG
29-
privateconststringMutagenControllerConfigSection="AppMutagenController";
36+
privateconststringConfigSubKey=@"SOFTWARE\Coder Desktop\App";
37+
privateconststringlogFilename="app.log";
3038
#else
31-
privateconststringMutagenControllerConfigSection="DebugAppMutagenController";
39+
privateconststringConfigSubKey=@"SOFTWARE\Coder Desktop\DebugApp";
40+
privateconststringlogFilename="debug-app.log";
3241
#endif
3342

43+
privatereadonlyILogger<App>_logger;
44+
3445
publicApp()
3546
{
3647
varbuilder=Host.CreateApplicationBuilder();
3748

3849
(builder.ConfigurationasIConfigurationBuilder).Add(
39-
newRegistryConfigurationSource(Registry.LocalMachine,@"SOFTWARE\Coder Desktop"));
50+
newRegistryConfigurationSource(Registry.LocalMachine,ConfigSubKey));
4051

4152
varservices=builder.Services;
4253

54+
// Logging
55+
builder.Services.AddSerilog((_,loggerConfig)=>
56+
{
57+
loggerConfig.ReadFrom.Configuration(builder.Configuration);
58+
varsinkConfig=builder.Configuration.GetSection("Serilog").GetSection("WriteTo");
59+
if(!sinkConfig.GetChildren().Any())
60+
{
61+
// no log sink defined in the registry, so we'll add one here.
62+
// We can't generally define these in the registry because we don't
63+
// know, a priori, what user will execute Coder Desktop, and therefore
64+
// what directories are writable by them. But, it's nice to be able to
65+
// directly customize Serilog via the registry if you know what you are
66+
// doing.
67+
varlogPath=Path.Combine(
68+
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
69+
"CoderDesktop",
70+
logFilename);
71+
loggerConfig.WriteTo.File(logPath,outputTemplate:logTemplate,rollingInterval:RollingInterval.Day);
72+
}
73+
});
74+
4375
services.AddSingleton<ICredentialManager,CredentialManager>();
4476
services.AddSingleton<IRpcController,RpcController>();
4577

@@ -69,6 +101,7 @@ public App()
69101
services.AddTransient<TrayWindow>();
70102

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

73106
InitializeComponent();
74107
}
@@ -87,6 +120,7 @@ public async Task ExitApplication()
87120

88121
protectedoverridevoidOnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgsargs)
89122
{
123+
_logger.LogInformation("new instance launched");
90124
// Start connecting to the manager in the background.
91125
varrpcController=_services.GetRequiredService<IRpcController>();
92126
if(rpcController.GetState().RpcLifecycle==RpcLifecycle.Disconnected)
@@ -110,13 +144,15 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
110144
_=credentialManager.LoadCredentials(credentialManagerCts.Token).ContinueWith(t=>
111145
{
112146
// TODO: log
113-
#ifDEBUG
114147
if(t.Exception!=null)
115148
{
149+
_logger.LogError(t.Exception,"failed to load credentials");
150+
#ifDEBUG
116151
Debug.WriteLine(t.Exception);
117152
Debugger.Break();
118-
}
119153
#endif
154+
}
155+
120156
credentialManagerCts.Dispose();
121157
},CancellationToken.None);
122158

@@ -126,9 +162,13 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
126162
_=syncSessionController.RefreshState(syncSessionCts.Token).ContinueWith(t=>
127163
{
128164
// TODO: log
165+
if(t.IsCanceled||t.Exception!=null)
166+
{
167+
_logger.LogError(t.Exception,"failed to refresh sync state (canceled = {canceled}",t.IsCanceled);
129168
#ifDEBUG
130-
if(t.IsCanceled||t.Exception!=null)Debugger.Break();
169+
Debugger.Break();
131170
#endif
171+
}
132172
syncSessionCts.Dispose();
133173
},CancellationToken.None);
134174

@@ -148,17 +188,24 @@ public void OnActivated(object? sender, AppActivationArguments args)
148188
{
149189
caseExtendedActivationKind.Protocol:
150190
varprotoArgs=args.DataasIProtocolActivatedEventArgs;
191+
if(protoArgs==null)
192+
{
193+
_logger.LogWarning("URI activation with null data");
194+
return;
195+
}
196+
151197
HandleURIActivation(protoArgs.Uri);
152198
break;
153199

154200
default:
155-
// TODO: log
201+
_logger.LogWarning("activation for {kind}, which is unhandled",args.Kind);
156202
break;
157203
}
158204
}
159205

160206
publicvoidHandleURIActivation(Uriuri)
161207
{
162-
// TODO: handle
208+
// don't log the query string as that's where we include some sensitive information like passwords
209+
_logger.LogInformation("handling URI activation for {path}",uri.AbsolutePath);
163210
}
164211
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp