lixiaojun
2023-03-20 14801a2e40bc79833c41151a37fe4cb0acbc5c7f
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation
{
    public class LogHelper 
    {
 
        #region Info
        public static string InfoName = "Info";
 
        //这里的 loginfo 和 log4net.config 里的名字要一样
        private static readonly log4net.ILog _loginfo = LogConfig.GetLogger(InfoName);
 
        /// <summary>
        /// 写入信息日志
        /// </summary>
        public static void Info(string info)
        {
            if (_loginfo.IsInfoEnabled)
            {
                _loginfo.Info(info);
            }
        }
 
        /// <summary>
        /// 写入信息日志
        /// </summary>
        public static void InfoFormat(string format, params object[] args)
        {
            if (_loginfo.IsInfoEnabled)
            {
                _loginfo.InfoFormat(format, args);
            }
        }
 
        #endregion
 
        #region Debug
        public static string DebugName = "Debug";
        //这里的 logdebug 和 log4net.config 里的名字要一样
        private static readonly log4net.ILog _logdebug = LogConfig.GetLogger(DebugName);
 
        /// <summary>
        /// 写入调试日志
        /// </summary>
        public static void Debug(string info)
        {
            if (_logdebug.IsInfoEnabled)
            {
                _logdebug.Debug(info);
            }
        }
 
        /// <summary>
        ///  写入调试日志
        /// </summary>
        public static void DebugFormat(string format, params object[] args)
        {
            if (_logdebug.IsInfoEnabled)
            {
                _logdebug.DebugFormat(format, args);
            }
        }
 
        #endregion
 
        #region Error 
 
        public static string ErrorName = "Error";
 
        private static readonly log4net.ILog _logerror = LogConfig.GetLogger(ErrorName);
 
        /// <summary>
        /// 写入错误日志
        /// </summary>
        public static void Error(string info, Exception ex = null)
        {
            if (_logerror.IsErrorEnabled)
            {
                if (ex == null)
                    _logerror.Error(info);
                else
                    _logerror.Error(info, ex);
            }
        }
 
        /// <summary>
        /// 写入错误日志
        /// </summary>
        public static void ErrorFormat(string format, params object[] args)
        {
            if (_logerror.IsErrorEnabled)
            {
                _logerror.ErrorFormat(format, args);
            }
        }
 
        /// <summary>
        /// 写入错误日志
        /// </summary>
        public static void Error(string info, int line, Exception ex = null)
        {
            if (_logerror.IsErrorEnabled)
            {
                if (ex == null)
                    _logerror.Error(info + " 在代码第" + line);
                else
                    _logerror.Error(info + " 在代码第" + line, ex);
            }
        }
        #endregion
 
        #region 自定义
 
        /// <summary>
        /// 自定义
        /// </summary>
        public static void Custom(string name, string msg)
        {
            var log = LogConfig.GetLogger(name);
            if (log.IsInfoEnabled)
            {
                log.Info(msg);
            }
        }
 
        /// <summary>
        /// 自定义
        /// </summary>
        public static void CustomFormat(string name, string format, params object[] args)
        {
            var log = LogConfig.GetLogger(name);
            if (log.IsInfoEnabled)
            {
                log.InfoFormat(format,args);
            }
        }
 
        
        #endregion
 
    }
}