feat(desktop): 日志改为落文件(软件目录 logs\log_日期.txt),界面移除日志模块
- AppLog:不再做内存缓冲/事件,直接按天追加到 <软件目录>\logs\log_yyyy-MM-dd.txt 行格式 [yyyy-MM-dd HH:mm:ss] 内容;写失败一律吞掉,不影响主流程 装到只读目录(如 Program Files)时自动退回 %LOCALAPPDATA%\YuncangTrace\logs - 新增 AppLog.Clear():删除目录下全部 log_*.txt(当天文件也能删,之后重建) - MainWindow:删掉「运行日志」ListBox 与整套集合/订阅代码,Grid 行 4→3 查询卡改成 Grid(结果框自适应高度),窗口 620→460 - 菜单「工具」增加「打开日志目录」「清理日志」 - build.ps1 补 -p:AppEnvironment:原来 Release 发布时环境恒为 Production, 开发包日志/关于里也显示「生产环境」(现在与 -Environment 一致) - test-update.ps1:更新完成后轮询 60s 等服务与进程起来,避免误报「无响应」
This commit is contained in:
parent
7fdbe5afc1
commit
21dfc7f651
@ -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">
|
||||
<Grid Margin="18">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="*" />
|
||||
@ -21,6 +20,9 @@
|
||||
<MenuItem Header="工具(_T)">
|
||||
<MenuItem Header="测试查询" Click="FocusQuery_Click" />
|
||||
<Separator />
|
||||
<MenuItem Header="打开日志目录" Click="OpenLogFolder_Click" />
|
||||
<MenuItem Header="清理日志" Click="ClearLogs_Click" />
|
||||
<Separator />
|
||||
<MenuItem Header="清理更新缓存" Click="ClearUpdateCache_Click" />
|
||||
</MenuItem>
|
||||
|
||||
@ -53,37 +55,26 @@
|
||||
</StackPanel>
|
||||
</DockPanel>
|
||||
|
||||
<!-- ② 查询测试 -->
|
||||
<!-- ② 查询测试(占满剩余高度;日志只写文件,界面上不再展示) -->
|
||||
<Border Grid.Row="2" Background="White" BorderBrush="{StaticResource LineBrush}"
|
||||
BorderThickness="1" CornerRadius="8" Padding="12,10" Margin="0,0,0,10">
|
||||
<StackPanel>
|
||||
<DockPanel>
|
||||
BorderThickness="1" CornerRadius="8" Padding="12,10">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="*" />
|
||||
</Grid.RowDefinitions>
|
||||
<DockPanel Grid.Row="0" Margin="0,0,0,10">
|
||||
<Button DockPanel.Dock="Right" Content="查询" Padding="16,5" Margin="8,0,0,0" Click="Query_Click" />
|
||||
<TextBlock Text="测试查询" Foreground="{StaticResource MutedBrush}" VerticalAlignment="Center" Margin="0,0,10,0" />
|
||||
<TextBox x:Name="QueryBox" Padding="6,3" VerticalContentAlignment="Center"
|
||||
ToolTip="输入运单号后回车" KeyDown="QueryBox_KeyDown" />
|
||||
</DockPanel>
|
||||
<TextBox x:Name="ResultBox" Margin="0,10,0,0" Height="130" IsReadOnly="True"
|
||||
<TextBox Grid.Row="1" x:Name="ResultBox" IsReadOnly="True"
|
||||
AcceptsReturn="True" AcceptsTab="True" TextWrapping="NoWrap"
|
||||
VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"
|
||||
FontFamily="Consolas, Microsoft YaHei UI" FontSize="12" Padding="8"
|
||||
Background="#F9FAFB" BorderBrush="{StaticResource LineBrush}" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Border>
|
||||
|
||||
<!-- ③ 日志 -->
|
||||
<Grid Grid.Row="3">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<TextBlock Grid.Row="0" Text="运行日志" Foreground="{StaticResource MutedBrush}" Margin="2,0,0,4" />
|
||||
<Border Grid.Row="1" Background="White" BorderBrush="{StaticResource LineBrush}"
|
||||
BorderThickness="1" CornerRadius="8" Padding="4">
|
||||
<ListBox x:Name="LogList" BorderThickness="0" Background="Transparent"
|
||||
FontFamily="Consolas, Microsoft YaHei UI" FontSize="12"
|
||||
ScrollViewer.HorizontalScrollBarVisibility="Auto" />
|
||||
</Border>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Window>
|
||||
|
||||
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 主窗口:本机服务状态、启动协议、测试查询、日志、当前登录用户
|
||||
/// 主窗口:本机服务状态、测试查询、当前登录用户
|
||||
///
|
||||
/// 日志不进界面:只写文件(<c>软件目录\logs\log_yyyy-MM-dd.txt</c>),
|
||||
/// 需要看的时候用菜单「工具 → 打开日志目录」。
|
||||
///
|
||||
/// 关窗口**不是退出**:只是隐藏到托盘(本机服务得继续跑)。
|
||||
/// 真正退出:托盘菜单「退出」,或点「退出登录」后再取消登录。
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
private readonly ObservableCollection<string> _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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -114,6 +109,36 @@ public partial class MainWindow : Window
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>用资源管理器打开日志目录(按天一个 log_yyyy-MM-dd.txt)</summary>
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>日志(HTTP 服务线程也会触发,切回 UI 线程再改集合)</summary>
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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`(当天文件也能删,之后会重建)。
|
||||
|
||||
## 环境配置(开发 / 生产)
|
||||
|
||||
|
||||
@ -1,47 +1,113 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace YuncangTrace.LocalService.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 进程级日志(托盘 / 登录窗 / 主窗 / HTTP 服务都往这里写)
|
||||
///
|
||||
/// 服务在登录之前就起来了,那时主窗口还不存在 —— 所以日志要有个全局缓冲,
|
||||
/// 主窗口打开时用 <see cref="History"/> 把历史补上。
|
||||
/// 只落文件、不再进界面:**按天一个文件** <c>logs\log_2026-09-22.txt</c>,就放在软件目录下
|
||||
/// (<see cref="AppContext.BaseDirectory"/>)。装到只读位置(如 Program Files)时自动退回
|
||||
/// <c>%LOCALAPPDATA%\YuncangTrace\logs</c>。
|
||||
///
|
||||
/// 写日志失败绝不能影响主流程 —— 所有 IO 异常都吞掉。
|
||||
/// </summary>
|
||||
public static class AppLog
|
||||
{
|
||||
private const int MaxLines = 300;
|
||||
private const string FolderName = "logs";
|
||||
|
||||
private static readonly object Gate = new();
|
||||
private static readonly Queue<string> Buffer = new();
|
||||
private static readonly Encoding Utf8NoBom = new UTF8Encoding(false);
|
||||
|
||||
/// <summary>新日志(订阅方负责切回 UI 线程)</summary>
|
||||
public static event Action<string>? Written;
|
||||
private static string? _logDirectory;
|
||||
|
||||
/// <summary>写入一行日志</summary>
|
||||
/// <summary>日志目录(首次访问时创建)</summary>
|
||||
public static string LogDirectory => _logDirectory ??= ResolveDirectory();
|
||||
|
||||
/// <summary>写入一行日志(按天分文件,多线程安全)</summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>最近的历史日志(窗口初始化时灌进去)</summary>
|
||||
public static IReadOnlyList<string> History
|
||||
{
|
||||
get
|
||||
try
|
||||
{
|
||||
lock (Gate)
|
||||
{
|
||||
return Buffer.ToArray();
|
||||
File.AppendAllText(Path.Combine(LogDirectory, FileNameOf(now)), line, Utf8NoBom);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 磁盘满 / 无权限:日志丢了也不能影响功能
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理日志:删除目录下全部 <c>log_*.txt</c>,返回删掉的文件数。
|
||||
/// 当天正在写的文件也能删,下次 <see cref="Write"/> 会重建。
|
||||
/// </summary>
|
||||
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<string>
|
||||
{
|
||||
// 首选:软件目录(与 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];
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)" }
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user