public void InstallHook(Form window) { if (this.State == HookState.Uninstalled) { if (window != null) { NativeExports.SetLastError(0); IntPtr nextWindow = NativeExports.SetClipboardViewer(window.Handle); if (nextWindow == IntPtr.Zero) { var errorCode = NativeExports.GetLastError(); if (errorCode == 0) { OnSuccessfullHook(window.Handle, nextWindow); } else { var message = ErrorCodeHelper.GetMessage(errorCode); throw new Exception(message); } } else { OnSuccessfullHook(window.Handle, nextWindow); } } } }
/// <summary> /// 使用 <see cref="LogLevel.Warning"/> 级别打印错误日志,并记录异常堆栈。 /// </summary> /// <param name="logger">日志记录器的实例。</param> /// <param name="exception">错误码异常实例。</param> public static void LogWarningInfo(this ILogger logger, ErrorCodeException exception) { if (exception.ErrorCode < 50000) { throw exception; } var sb = new StringBuilder(); sb.Append($"错误代码: {exception.ErrorCode},信息: {ErrorCodeHelper.GetMessage(exception.ErrorCode)}"); sb.Append($"\n附加信息:\n {JsonConvert.SerializeObject(exception.AttachObject)}"); logger.LogWarning(sb.ToString()); }
public void RemoveHook() { if (this.State == HookState.Installed) { if (NativeExports.UnhookWindowsHookEx(this.handle)) { this.OnSuccessfullUnhook(); } else { var errorCode = NativeExports.GetLastError(); throw new Exception(ErrorCodeHelper.GetMessage(errorCode)); } } }
public void InstallHook(Form window) { if (this.State == HookState.Uninstalled) { this.keyboardProcessor = new KeyboardMessageEventHandler(KeyboardProc); this.handle = NativeExports.SetWindowsHookEx(NativeConstants.WH_KEYBOARD_LL, this.keyboardProcessor, IntPtr.Zero, 0); if (this.handle == IntPtr.Zero) { this.keyboardProcessor = null; var errorCode = NativeExports.GetLastError(); throw new Exception(ErrorCodeHelper.GetMessage(errorCode)); } else { this.OnStateChanged(new StateChangedEventArgs(this.State)); } } }
public void RemoveHook() { if (this.State == HookState.Installed) { NativeExports.SetLastError(0); NativeExports.ChangeClipboardChain(this.clipboard.Handle, this.clipboard.NextWindow); var errorCode = NativeExports.GetLastError(); if (errorCode == 0) { this.OnSuccessfullUnhook(); } else { var message = ErrorCodeHelper.GetMessage(errorCode); throw new Exception(message); } } }
private static int HandleException(Exception ex) { switch (ex) { case ErrorCodeException exception: Log.Logger.Error($"出现了未处理的异常,错误代码: {exception.ErrorCode},错误信息: {ErrorCodeHelper.GetMessage(exception.ErrorCode)}\n调用栈:\n{exception.StackTrace}"); Environment.Exit(exception.ErrorCode); return(exception.ErrorCode); case { } unknownException: Log.Logger.Error($"出现了未处理的异常: {unknownException.Message}\n调用栈:\n{unknownException.StackTrace}"); Environment.Exit(-1); return(1); default: return(1); } }
public void GetMessage_Test() { ErrorCodeHelper.LoadErrorMessage(); ErrorCodeHelper.GetMessage(ErrorCodes.DirectoryNotExist).ShouldBe("需要扫描的目录不存在,请确认路径是否正确。"); }