diff --git a/desktop/MainWindow.xaml b/desktop/MainWindow.xaml
index 83e9303..5abda5d 100644
--- a/desktop/MainWindow.xaml
+++ b/desktop/MainWindow.xaml
@@ -2,15 +2,14 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="云仓视频溯源软件(本机服务)"
- Height="620" Width="780"
- MinHeight="520" MinWidth="680"
+ Height="460" Width="780"
+ MinHeight="400" MinWidth="680"
WindowStartupLocation="CenterScreen"
Background="{StaticResource BgBrush}"
FontFamily="Microsoft YaHei UI, Segoe UI"
FontSize="13">
-
@@ -21,6 +20,9 @@
@@ -53,37 +55,26 @@
-
+
-
-
+ BorderThickness="1" CornerRadius="8" Padding="12,10">
+
+
+
+
+
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/desktop/MainWindow.xaml.cs b/desktop/MainWindow.xaml.cs
index 6e0d491..e73140d 100644
--- a/desktop/MainWindow.xaml.cs
+++ b/desktop/MainWindow.xaml.cs
@@ -1,6 +1,6 @@
using System;
-using System.Collections.ObjectModel;
using System.ComponentModel;
+using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Input;
@@ -10,14 +10,16 @@ using YuncangTrace.LocalService.Services;
namespace YuncangTrace.LocalService;
///
-/// 主窗口:本机服务状态、启动协议、测试查询、日志、当前登录用户
+/// 主窗口:本机服务状态、测试查询、当前登录用户
+///
+/// 日志不进界面:只写文件(软件目录\logs\log_yyyy-MM-dd.txt),
+/// 需要看的时候用菜单「工具 → 打开日志目录」。
///
/// 关窗口**不是退出**:只是隐藏到托盘(本机服务得继续跑)。
/// 真正退出:托盘菜单「退出」,或点「退出登录」后再取消登录。
///
public partial class MainWindow : Window
{
- private readonly ObservableCollection _logs = new();
private readonly LocalHttpServer _server;
private readonly OrderQueryService _queryService = new();
@@ -45,18 +47,11 @@ public partial class MainWindow : Window
UserText.Text = $"已登录:{session.DisplayName}";
UserText.ToolTip = $"服务器:{settings.ServerBaseUrl}";
- // 日志:先灌历史(服务在登录前就起来了),再订阅新的
- foreach (var line in AppLog.History) _logs.Add(line);
-
- LogList.ItemsSource = _logs;
- AppLog.Written += OnLogWritten;
-
Loaded += (_, _) =>
{
UpdateServerState();
};
Closing += OnClosing;
- Closed += (_, _) => AppLog.Written -= OnLogWritten;
}
///
@@ -114,6 +109,36 @@ public partial class MainWindow : Window
}
}
+ /// 用资源管理器打开日志目录(按天一个 log_yyyy-MM-dd.txt)
+ private void OpenLogFolder_Click(object sender, RoutedEventArgs e)
+ {
+ try
+ {
+ Directory.CreateDirectory(AppLog.LogDirectory);
+ Process.Start(new ProcessStartInfo("explorer.exe", $"\"{AppLog.LogDirectory}\"") { UseShellExecute = true });
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show(this, $"打开日志目录失败:{ex.Message}\n\n{AppLog.LogDirectory}",
+ "打开日志目录", MessageBoxButton.OK, MessageBoxImage.Warning);
+ }
+ }
+
+ private void ClearLogs_Click(object sender, RoutedEventArgs e)
+ {
+ var answer = MessageBox.Show(this,
+ $"确定删除全部日志文件吗?\n\n{AppLog.LogDirectory}",
+ "清理日志", MessageBoxButton.YesNo, MessageBoxImage.Question);
+ if (answer != MessageBoxResult.Yes) return;
+
+ var removed = AppLog.Clear();
+ AppLog.Write($"已清理日志:删除 {removed} 个文件");
+
+ MessageBox.Show(this,
+ removed > 0 ? $"已删除 {removed} 个日志文件。" : "没有可清理的日志文件。",
+ "清理日志", MessageBoxButton.OK, MessageBoxImage.Information);
+ }
+
private void Restart_Click(object sender, RoutedEventArgs e) => RestartRequested?.Invoke();
private void CheckUpdate_Click(object sender, RoutedEventArgs e) => CheckUpdateRequested?.Invoke();
@@ -190,26 +215,4 @@ public partial class MainWindow : Window
AppLog.Write($"复制失败:{ex.Message}");
}
}
-
- /// 日志(HTTP 服务线程也会触发,切回 UI 线程再改集合)
- private void OnLogWritten(string line)
- {
- if (!Dispatcher.CheckAccess())
- {
- Dispatcher.BeginInvoke(new Action(() => Append(line)));
- return;
- }
-
- Append(line);
- }
-
- private void Append(string line)
- {
- _logs.Add(line);
-
- // 只留最近 300 行
- while (_logs.Count > 300) _logs.RemoveAt(0);
-
- if (_logs.Count > 0) LogList.ScrollIntoView(_logs[^1]);
- }
}
diff --git a/desktop/README.md b/desktop/README.md
index 7d7a691..88888a8 100644
--- a/desktop/README.md
+++ b/desktop/README.md
@@ -20,7 +20,7 @@ desktop/
├── Assets/app.ico # 程序/托盘图标(tools/make-icon.ps1 生成)
├── App.xaml / App.xaml.cs # 防多开 + 托盘 + 登录流程 + 本机服务生命周期
├── LoginWindow.xaml / .xaml.cs # 服务器地址 / 账号 / 密码 / 记住账号 / 自动恢复登录
-├── MainWindow.xaml / .xaml.cs # 服务状态、地址、协议注册、测试查询、日志、退出登录
+├── MainWindow.xaml / .xaml.cs # 服务状态、测试查询、菜单、退出登录
└── Services/
├── LocalHttpServer.cs # HttpListener + CORS + PNA
├── OrderQueryService.cs # ★ 按运单号查数据(接真实数据源的地方)
@@ -30,7 +30,7 @@ desktop/
├── ProtocolHelper.cs # yuncang:// 注册/反注册、解析 code
├── SingleInstance.cs # 互斥体 + 命名管道转发参数(防多开)
├── TrayIconService.cs # 托盘图标与菜单
- ├── AppLog.cs # 全局日志缓冲(窗口没开时的日志也不丢)
+ ├── AppLog.cs # 日志落文件:软件目录\logs\log_yyyy-MM-dd.txt(按天一个)
└── JsonUtil.cs # JSON 序列化(中文不转义)
```
@@ -53,9 +53,14 @@ desktop/
> 也**没有「复制唤醒地址」**:唤醒地址由插件生成,不需要在界面上手测。
>
> 主界面顶部菜单:
-> - **工具**:测试查询(聚焦输入框)、清理更新缓存(`%TEMP%\YuncangTraceUpdate`)
+> - **工具**:测试查询(聚焦输入框)、打开日志目录、清理日志、清理更新缓存(`%TEMP%\YuncangTraceUpdate`)
> - **服务**:服务状态(只读)、重启软件
> - **帮助**:检查更新、关于(版本 / 环境 / 运行状态)
+>
+> **日志不在界面上显示**,只写文件(按天一个,方便直接发给别人排查):
+> `<软件目录>\logs\log_2026-09-22.txt`,行格式 `[2026-09-22 14:03:11] 内容`。
+> 软件目录不可写时(如装到 Program Files)自动退回 `%LOCALAPPDATA%\YuncangTrace\logs`。
+> 「工具 → 打开日志目录」用资源管理器打开它,「清理日志」删掉全部 `log_*.txt`(当天文件也能删,之后会重建)。
## 环境配置(开发 / 生产)
diff --git a/desktop/Services/AppLog.cs b/desktop/Services/AppLog.cs
index d23a431..0c432a0 100644
--- a/desktop/Services/AppLog.cs
+++ b/desktop/Services/AppLog.cs
@@ -1,47 +1,113 @@
using System;
using System.Collections.Generic;
+using System.IO;
+using System.Text;
namespace YuncangTrace.LocalService.Services;
///
/// 进程级日志(托盘 / 登录窗 / 主窗 / HTTP 服务都往这里写)
///
-/// 服务在登录之前就起来了,那时主窗口还不存在 —— 所以日志要有个全局缓冲,
-/// 主窗口打开时用 把历史补上。
+/// 只落文件、不再进界面:**按天一个文件** logs\log_2026-09-22.txt,就放在软件目录下
+/// ()。装到只读位置(如 Program Files)时自动退回
+/// %LOCALAPPDATA%\YuncangTrace\logs。
+///
+/// 写日志失败绝不能影响主流程 —— 所有 IO 异常都吞掉。
///
public static class AppLog
{
- private const int MaxLines = 300;
+ private const string FolderName = "logs";
private static readonly object Gate = new();
- private static readonly Queue Buffer = new();
+ private static readonly Encoding Utf8NoBom = new UTF8Encoding(false);
- /// 新日志(订阅方负责切回 UI 线程)
- public static event Action? Written;
+ private static string? _logDirectory;
- /// 写入一行日志
+ /// 日志目录(首次访问时创建)
+ public static string LogDirectory => _logDirectory ??= ResolveDirectory();
+
+ /// 写入一行日志(按天分文件,多线程安全)
public static void Write(string message)
{
- var line = $"[{DateTime.Now:HH:mm:ss}] {message}";
+ var now = DateTime.Now;
+ var line = $"[{now:yyyy-MM-dd HH:mm:ss}] {message}{Environment.NewLine}";
- lock (Gate)
- {
- Buffer.Enqueue(line);
- while (Buffer.Count > MaxLines) Buffer.Dequeue();
- }
-
- Written?.Invoke(line);
- }
-
- /// 最近的历史日志(窗口初始化时灌进去)
- public static IReadOnlyList History
- {
- get
+ try
{
lock (Gate)
{
- return Buffer.ToArray();
+ File.AppendAllText(Path.Combine(LogDirectory, FileNameOf(now)), line, Utf8NoBom);
}
}
+ catch
+ {
+ // 磁盘满 / 无权限:日志丢了也不能影响功能
+ }
+ }
+
+ ///
+ /// 清理日志:删除目录下全部 log_*.txt,返回删掉的文件数。
+ /// 当天正在写的文件也能删,下次 会重建。
+ ///
+ public static int Clear()
+ {
+ var removed = 0;
+
+ try
+ {
+ lock (Gate)
+ {
+ var dir = LogDirectory;
+ if (!Directory.Exists(dir)) return 0;
+
+ foreach (var file in Directory.EnumerateFiles(dir, "log_*.txt"))
+ {
+ try
+ {
+ File.Delete(file);
+ removed++;
+ }
+ catch
+ {
+ // 个别文件被占用(比如用户用记事本打开着)就跳过
+ }
+ }
+ }
+ }
+ catch
+ {
+ // 目录不可读:当作没有日志
+ }
+
+ return removed;
+ }
+
+ private static string FileNameOf(DateTime time) => $"log_{time:yyyy-MM-dd}.txt";
+
+ private static string ResolveDirectory()
+ {
+ var candidates = new List
+ {
+ // 首选:软件目录(与 exe 同级)
+ Path.Combine(AppContext.BaseDirectory, FolderName),
+
+ // 兜底:用户数据目录(exe 所在目录不可写时)
+ Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "YuncangTrace", FolderName),
+ };
+
+ foreach (var dir in candidates)
+ {
+ try
+ {
+ Directory.CreateDirectory(dir);
+ return dir;
+ }
+ catch
+ {
+ // 试下一个
+ }
+ }
+
+ return candidates[candidates.Count - 1];
}
}
diff --git a/desktop/installer/build.ps1 b/desktop/installer/build.ps1
index 2037ccd..e910789 100644
--- a/desktop/installer/build.ps1
+++ b/desktop/installer/build.ps1
@@ -113,6 +113,7 @@ try {
-p:PublishSingleFile=true `
-p:IncludeNativeLibrariesForSelfExtract=true `
-p:Version=$Version `
+ -p:AppEnvironment=$Environment `
-p:ServerBaseUrl=$ServerBaseUrl `
-o $publishDir
if ($LASTEXITCODE -ne 0) { throw "dotnet publish 失败($LASTEXITCODE)" }
diff --git a/desktop/installer/test-update.ps1 b/desktop/installer/test-update.ps1
index 8b94461..b6e5294 100644
--- a/desktop/installer/test-update.ps1
+++ b/desktop/installer/test-update.ps1
@@ -107,9 +107,16 @@ if ([version]$finalBase -eq [version]$target) {
Write-Host "✘ 自动更新未完成:当前 $finalBase,目标 $target(已等 ${TimeoutSeconds}s)" -ForegroundColor Red
}
-# 6) 服务与进程
-$localCode = & curl.exe -s -o NUL -w "%{http_code}" --max-time 5 "http://127.0.0.1:17300/api/query/"
-$processes = @(Get-CimInstance Win32_Process | Where-Object { $_.Name -like '*Yuncang*' })
+# 6) 服务与进程(新进程是 msiexec 装完后再拉起的,可能比版本变化晚几秒 → 轮询等一会儿)
+$probeDeadline = (Get-Date).AddSeconds(60)
+$localCode = $null
+$processes = @()
+while ((Get-Date) -lt $probeDeadline) {
+ $localCode = & curl.exe -s -o NUL -w "%{http_code}" --max-time 3 "http://127.0.0.1:17300/api/query/"
+ $processes = @(Get-CimInstance Win32_Process | Where-Object { $_.Name -like '*Yuncang*' })
+ if ($processes.Count -gt 0 -and $localCode -and $localCode -ne '000') { break }
+ Start-Sleep -Seconds 3
+}
if ($localCode -and $localCode -ne '000') {
Write-Host "✔ 本机服务 17300 有响应(HTTP $localCode)" -ForegroundColor Green