From 7bca8a592b3af15e7acf645b50ae78df064f17b7 Mon Sep 17 00:00:00 2001 From: status102 <102887808+status102@users.noreply.github.com> Date: Tue, 3 Mar 2026 14:43:52 +0800 Subject: [PATCH] =?UTF-8?q?perf(wpf):=20scopeLog=20=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E8=BF=9B=E5=87=BA=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MaaWpfGui/Main/AsstProxy.cs | 3 +- src/MaaWpfGui/Utilities/LogScope.cs | 39 +++++++++++++++++++ .../FightSettingsUserControlModel.cs | 2 +- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/MaaWpfGui/Utilities/LogScope.cs diff --git a/src/MaaWpfGui/Main/AsstProxy.cs b/src/MaaWpfGui/Main/AsstProxy.cs index f1f241bff2..a6eda438b4 100644 --- a/src/MaaWpfGui/Main/AsstProxy.cs +++ b/src/MaaWpfGui/Main/AsstProxy.cs @@ -44,6 +44,7 @@ using MaaWpfGui.Services; using MaaWpfGui.Services.Notification; using MaaWpfGui.Services.Web; using MaaWpfGui.States; +using MaaWpfGui.Utilities; using MaaWpfGui.ViewModels.UI; using MaaWpfGui.ViewModels.UserControl.TaskQueue; using Newtonsoft.Json; @@ -463,7 +464,7 @@ public class AsstProxy /// 是否成功。 public bool LoadResource() { - _logger.Information("Load Resource"); + using var log = new LogScope(_logger); string clientType = SettingsViewModel.GameSettings.ClientType; diff --git a/src/MaaWpfGui/Utilities/LogScope.cs b/src/MaaWpfGui/Utilities/LogScope.cs new file mode 100644 index 0000000000..0635361a0b --- /dev/null +++ b/src/MaaWpfGui/Utilities/LogScope.cs @@ -0,0 +1,39 @@ +// +// Part of the MaaWpfGui project, maintained by the MaaAssistantArknights team (Maa Team) +// Copyright (C) 2021-2025 MaaAssistantArknights Contributors +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License v3.0 only as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY +// +#nullable enable +using System; +using System.Runtime.CompilerServices; +using Serilog; + +namespace MaaWpfGui.Utilities; + +public class LogScope : IDisposable +{ + private readonly ILogger _logger; + private readonly string _methodName; + private readonly System.Diagnostics.Stopwatch _stopwatch; + + public LogScope(ILogger logger, [CallerMemberName] string methodName = "") + { + _logger = logger; + _methodName = methodName; + _stopwatch = System.Diagnostics.Stopwatch.StartNew(); + _logger.Information("{MethodName} Enter", _methodName); + } + + void IDisposable.Dispose() + { + _stopwatch.Stop(); + _logger.Information("{MethodName} Exit, {Elapsed:#,0} ms", _methodName, _stopwatch.ElapsedMilliseconds); + } +} diff --git a/src/MaaWpfGui/ViewModels/UserControl/TaskQueue/FightSettingsUserControlModel.cs b/src/MaaWpfGui/ViewModels/UserControl/TaskQueue/FightSettingsUserControlModel.cs index 71d4220cbd..632c87d0bd 100644 --- a/src/MaaWpfGui/ViewModels/UserControl/TaskQueue/FightSettingsUserControlModel.cs +++ b/src/MaaWpfGui/ViewModels/UserControl/TaskQueue/FightSettingsUserControlModel.cs @@ -715,7 +715,7 @@ public class FightSettingsUserControlModel : TaskSettingsViewModel, FightSetting public void UpdateStageList() { Execute.PostToUIThreadAsync(() => { - _logger.Information("Updating stage list..."); + using var log = new LogScope(_logger); using var scope = _lock.EnterScope(); var stageList = Instances.StageManager.GetStageList(); RefreshStageList();