diff --git a/STranslate/App.xaml.cs b/STranslate/App.xaml.cs index f19844ac..986c1cbe 100644 --- a/STranslate/App.xaml.cs +++ b/STranslate/App.xaml.cs @@ -20,7 +20,14 @@ protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); - // 1. 检查是否已经具有管理员权限 + // 1. 开启日志服务 +#if DEBUG + LogService.Register(); +#elif !DEBUG + LogService.Register(minLevel: LogLevel.Info); +#endif + + // 2. 检查是否已经具有管理员权限 if (NeedAdministrator()) { // 如果没有管理员权限,可以提示用户提升权限 @@ -32,7 +39,7 @@ protected override void OnStartup(StartupEventArgs e) } } - // 2. 多开检测 + // 3. 多开检测 if (IsAnotherInstanceRunning()) { MessageBox_S.Show($"{Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly()!.Location)} 应用程序已经在运行中。", "多开检测"); @@ -40,19 +47,13 @@ protected override void OnStartup(StartupEventArgs e) return; } - // 3. 启动应用程序 -#if DEBUG - LogService.Register(); -#elif !DEBUG - LogService.Register(minLevel: LogLevel.Info); -#endif // 4. 开启监听系统代理 ProxyUtil.LoadDynamicProxy(); // 5. 软件配置涉及初始化操作 Singleton.Instance.InitialOperate(); - // 6. Open View + // 6. 启动应用程序 StartProgram(); // 7. 全局异常处理 diff --git a/STranslate/Helper/ConfigHelper.cs b/STranslate/Helper/ConfigHelper.cs index 6d764322..418da213 100644 --- a/STranslate/Helper/ConfigHelper.cs +++ b/STranslate/Helper/ConfigHelper.cs @@ -165,9 +165,9 @@ internal ConfigModel ReadConfig() var content = File.ReadAllText(CnfName); return JsonConvert.DeserializeObject(content, settings) ?? throw new Exception("反序列化失败..."); } - catch (Exception) + catch (Exception ex) { - LogService.Logger.Error("[READCONFIG] 读取配置错误,本次运行加载初始化配置..."); + LogService.Logger.Error($"[READCONFIG] 读取配置错误,本次运行加载初始化配置...", ex); return InitialConfig(); } } diff --git a/STranslate/ViewModels/InputViewModel.cs b/STranslate/ViewModels/InputViewModel.cs index 768b80ca..b55ce54f 100644 --- a/STranslate/ViewModels/InputViewModel.cs +++ b/STranslate/ViewModels/InputViewModel.cs @@ -1,4 +1,12 @@ -using CommunityToolkit.Mvvm.ComponentModel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -8,14 +16,6 @@ using STranslate.Model; using STranslate.Util; using STranslate.ViewModels.Preference; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Controls; namespace STranslate.ViewModels { @@ -170,7 +170,8 @@ await Parallel.ForEachAsync( if (translatorList != null) { IdentifyLanguage = "缓存"; - service.Data = translatorList?.FirstOrDefault(x => x.Identify == service.Identify)?.Data ?? "该服务未获取到缓存Ctrl+Enter更新"; + service.Data = + translatorList?.FirstOrDefault(x => x.Identify == service.Identify)?.Data ?? "该服务未获取到缓存Ctrl+Enter更新"; return; } @@ -186,59 +187,64 @@ await Parallel.ForEachAsync( switch (service.Type) { case ServiceType.ApiService: - { - response = - (Task) - await service.TranslateAsync( - new RequestApi() - { - Text = InputContent, - SourceLang = LangDict[source].ToString(), - TargetLang = LangDict[target].ToString() - }, - token - ); - service.Data = (response.Result as ResponseApi)!.Data; - break; - } + { + response = + (Task) + await service.TranslateAsync( + new RequestApi() + { + Text = InputContent, + SourceLang = LangDict[source].ToString(), + TargetLang = LangDict[target].ToString() + }, + token + ); + service.Data = (response.Result as ResponseApi)!.Data; + break; + } case ServiceType.BaiduService: - { - string salt = new Random().Next(100000).ToString(); - string sign = StringUtil.EncryptString(service.AppID + InputContent + salt + service.AppKey); - response = - (Task) - await service.TranslateAsync( - new RequestBaidu() - { - Text = InputContent, - From = LangDict[source].ToString(), - TO = LangDict[target].ToString(), - AppId = service.AppID, - Salt = salt, - Sign = sign - }, - token - ); - var ret = (response.Result as ResponseBaidu)?.TransResult ?? []; - service.Data = ret.Length == 0 ? string.Empty : - string.Join(Environment.NewLine, ret.Where(trans => !string.IsNullOrEmpty(trans.Dst)).Select(trans => trans.Dst)); - break; - } + { + string salt = new Random().Next(100000).ToString(); + string sign = StringUtil.EncryptString(service.AppID + InputContent + salt + service.AppKey); + response = + (Task) + await service.TranslateAsync( + new RequestBaidu() + { + Text = InputContent, + From = LangDict[source].ToString(), + TO = LangDict[target].ToString(), + AppId = service.AppID, + Salt = salt, + Sign = sign + }, + token + ); + var ret = (response.Result as ResponseBaidu)?.TransResult ?? []; + service.Data = + ret.Length == 0 + ? string.Empty + : string.Join( + Environment.NewLine, + ret.Where(trans => !string.IsNullOrEmpty(trans.Dst)).Select(trans => trans.Dst) + ); + break; + } case ServiceType.BingService: + { + var req = new RequestBing { - var req = new RequestBing - { - From = LangDict[source].ToString(), - To = LangDict[target].ToString(), - Req = [new TextData { Text = InputContent }], - }; - response = (Task)await service.TranslateAsync(req, token); - var ret = (response.Result as ResponseBing[])!.FirstOrDefault()?.Translations?.FirstOrDefault()?.Text; - service.Data = string.IsNullOrEmpty(ret) ? string.Empty : ret; - break; - } + From = LangDict[source].ToString(), + To = LangDict[target].ToString(), + Req = [new TextData { Text = InputContent }], + }; + response = (Task)await service.TranslateAsync(req, token); + var ret = (response.Result as ResponseBing[])!.FirstOrDefault()?.Translations?.FirstOrDefault()?.Text; + service.Data = string.IsNullOrEmpty(ret) ? string.Empty : ret; + break; + } default: break; @@ -278,15 +284,18 @@ private void HandleTranslationException(ITranslator service, string errorMessage service.Data = errorMessage; if (isDebug) - LogService.Logger.Debug($"[{service.Name}({service.Identify})] {errorMessage}, 请求API: {service.Url}, 异常信息: {exception?.Message}"); + LogService.Logger.Debug( + $"[{service.Name}({service.Identify})] {errorMessage}, 请求API: {service.Url}, 异常信息: {exception?.Message}" + ); else - LogService.Logger.Error($"[{service.Name}({service.Identify})] {errorMessage}, 请求API: {service.Url}, 异常信息: {exception?.Message}"); + LogService.Logger.Error( + $"[{service.Name}({service.Identify})] {errorMessage}, 请求API: {service.Url}, 异常信息: {exception?.Message}" + ); } #endregion Translatehandle public void Clear() - { InputContent = string.Empty; } @@ -412,7 +421,7 @@ protected override IList CreateProperties(Type type, MemberSeriali list! .ToList() - ?.ForEach(x => + .ForEach(x => { if (x.Ignored && x.PropertyName == "Data") x.Ignored = false; //不忽略 @@ -465,4 +474,4 @@ public override void WriteJson(JsonWriter writer, ITranslator? value, JsonSerial } #endregion JsonConvert -} \ No newline at end of file +} diff --git a/STranslate/Views/MainView.xaml.cs b/STranslate/Views/MainView.xaml.cs index fcefc943..ee91eaeb 100644 --- a/STranslate/Views/MainView.xaml.cs +++ b/STranslate/Views/MainView.xaml.cs @@ -57,11 +57,11 @@ private void LoadPosition() Left = left; Top = top; } - catch (Exception ex) + catch (Exception) { WindowStartupLocation = WindowStartupLocation.CenterOwner; - LogService.Logger.Warn($"加载上次窗口位置({position})失败,启用默认位置 {ex.Message}"); + LogService.Logger.Warn($"加载上次窗口位置({position})失败,启用默认位置"); } }