C3204
2026-01-13 510dc693e91b291f36667088f47923591d25c98f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace LB_SmartVisionCommon
{
    /// <summary>
    /// 同步记录日志类
    /// </summary>
    public static class LogHelper
    {
        static string LineT = "记录信息:";
        static readonly ILog _logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
 
        /// <summary>
        /// Info类型 - 异步记录
        /// </summary>
        /// <param name="message">记录信息</param>
        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);
        }
 
        /// <summary>
        /// Debug类型 - 异步记录
        /// </summary>
        /// <param name="message">记录信息</param>
        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);
        }
 
        /// <summary>
        /// Warn类型 - 异步记录
        /// </summary>
        /// <param name="message">记录信息</param>
        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);
        }
 
        /// <summary>
        /// Error类型 - 异步记录
        /// </summary>
        /// <param name="message">记录信息</param>
        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);
        }
 
        /// <summary>
        /// Error类型 - 异步记录异常
        /// </summary>
        /// <param name="message">记录信息</param>
        /// <param name="exception">异常对象</param>
        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);
        }
    }
}