using log4net; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LB_SmartVisionCommon { /// /// 同步记录日志类 /// public static class LogHelper { static string LineT = "记录信息:"; static readonly ILog _logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); /// /// Info类型 - 异步记录 /// /// 记录信息 public static void Info(string message) { Task.Factory.StartNew(() => { try { _logger.Info(LineT + message); } catch (Exception ex) { // 异步日志记录失败时的处理 System.Diagnostics.Debug.WriteLine($"日志记录失败: {ex.Message}"); } }, TaskCreationOptions.LongRunning); } /// /// Debug类型 - 异步记录 /// /// 记录信息 public static void Debug(string message) { Task.Factory.StartNew(() => { try { _logger.Debug(LineT + message); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"日志记录失败: {ex.Message}"); } }, TaskCreationOptions.LongRunning); } /// /// Warn类型 - 异步记录 /// /// 记录信息 public static void Warn(string message) { Task.Factory.StartNew(() => { try { _logger.Warn(LineT + message); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"日志记录失败: {ex.Message}"); } }, TaskCreationOptions.LongRunning); } /// /// Error类型 - 异步记录 /// /// 记录信息 public static void Error(string message) { Task.Factory.StartNew(() => { try { _logger.Error(LineT + message); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"日志记录失败: {ex.Message}"); } }, TaskCreationOptions.LongRunning); } /// /// Error类型 - 异步记录异常 /// /// 记录信息 /// 异常对象 public static void Error(string message, Exception exception) { Task.Factory.StartNew(() => { try { _logger.Error(LineT + message, exception); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"日志记录失败: {ex.Message}"); } }, TaskCreationOptions.LongRunning); } } }