Files
KennethCheng e53c0585f7 fix: 修复 SDK 2.4.0 + WPF/WinForms 双开下的编译错误
dotnet 8.0.424 SDK + Microsoft.WindowsAppSDK 2.4.0 实测 build 通过(0 警告 0 错误)。

修复内容:

- csproj:TargetFramework 从 net8.0-windows 改为 net8.0-windows10.0.19041.0,新增 SupportedOSPlatformVersion=10.0.17763.0(WindowsAppSDK 2.4.0 强制要求 TFM 带 Windows 版本号)

- 4 个 Services 文件加 using System.IO(Logger/ConfigService/StateService/GotifyWebSocketService)。WPF+WinForms 双开项目模板不自动 import System.IO

- GotifyApiService 加 using System.Net.Http(同上,HttpClient 不在 implicit usings)

- App.xaml.cs:using Application = System.Windows.Application(WPF/WinForms Application 撞名)

- MainWindow.xaml.cs:using MessageBox = System.Windows.MessageBox(同上,MessageBox 撞名)

- App.xaml.cs:SendTestNotificationAsync 里 Date 从 .UtcNow.ToUnixTimeMilliseconds() 改回 .UtcNow(原 plan 写法有 bug,long 塞不进 DateTimeOffset)

- App.xaml.cs:OnNotificationInvoked 重写:WindowsAppSDK 2.4.0 把 args.Arguments 从 string 改成 IDictionary<string,string>,改用 TryGetValue("url", out var url) 取参数

- NotificationService:AddArgument 从单参 "url=escaped" 改成两参 ("url", url)。WindowsAppSDK 2.4.0 不再支持单参 "key=value" 写法

- GotifyWebSocketService:删除 _forceReconnect 字段(写了不读,改成 RequestReconnect 只调 socket.Abort 让外层 RunLoopAsync 自然重连)
2026-09-06 20:22:54 +08:00

43 lines
1.1 KiB
C#

using System.IO;
namespace WindowsGotify.Services;
public sealed class Logger
{
private readonly object _lock = new();
private readonly string _logFile;
public Logger()
{
var dir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"WindowsGotify",
"logs");
Directory.CreateDirectory(dir);
_logFile = Path.Combine(dir, "app.log");
}
public void Info(string message) => Write("INFO", message);
public void Warn(string message) => Write("WARN", message);
public void Error(string message, Exception? ex = null)
=> Write("ERROR", ex is null ? message : $"{message} {ex}");
private void Write(string level, string message)
{
var line = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} [{level}] {message}";
lock (_lock)
{
try
{
File.AppendAllText(_logFile, line + Environment.NewLine);
}
catch
{
// 日志写不进时静默,不影响主流程
}
}
}
}