Administrator
2021-03-16 c4fa5bc34b30434b9f39f2670c30d10cf9f1cad4
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
using log4net;
using System.IO;
using System.Reflection;
 
namespace Bro.Common.Log
{
    public class LogHelper
    {
        public const string         DFT_CONFIG_FILE = "BroC.Comn.Log.BroClog4net.xml";
 
        private static ILog         log         = null;
 
        private static LogHelper inst;
 
        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public static LogHelper GetInstance()
        {
            if (inst == null)
                inst = new LogHelper(null);
            return inst;
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="configPath"></param>
        /// <returns></returns>
        public static LogHelper GetInstance(string configPath)
        {
            if (inst == null)
                inst = new LogHelper(configPath);
            return inst;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configPath"></param>
        private LogHelper(string configPath)
        {
            var type = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
            log = log4net.LogManager.GetLogger(type);
            
            // 1. 优先使用手动指定的log4net配置文件
            // 2. 不存在手动指定配置文件,则使用默认配置
            if (!string.IsNullOrEmpty(configPath) && File.Exists(configPath))
            {
                try
                {
                    log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(configPath));
                }
                catch
                {
                    // 默认的log4net配置文件作为内嵌式文件编译进应用集合内
                    var sm = Assembly.GetExecutingAssembly().GetManifestResourceStream(DFT_CONFIG_FILE); 
                    log4net.Config.XmlConfigurator.Configure(sm);
                }
            }
            else
            {
                // 默认的log4net配置文件作为内嵌式文件编译进应用集合内
                var sm = Assembly.GetExecutingAssembly().GetManifestResourceStream(DFT_CONFIG_FILE); 
                log4net.Config.XmlConfigurator.Configure(sm);
            }
        }
 
        #region 各级别日志方法
 
 
        public void Debug(string msg)
        {
            
            log.Debug(msg);
        }
 
        public void DebugFormat(string format, params object[] args)
        {
            log.DebugFormat(format, args);
        }
 
        public void Info(string msg)
        {
            log.Info(msg);
        }
 
        public void InfoFormat(string format, params object[] args)
        {
            log.InfoFormat(format, args);
        }
 
        public void Warn(string msg)
        {
            log.Warn(msg);
        }
 
        public void WarnFormat(string format, params object[] args)
        {
            log.WarnFormat(format, args);
        }
 
        public void Error(string msg)
        {
            log.Error(msg);
        }
 
        public void ErrorFormat(string format, params object[] args)
        {
            log.ErrorFormat(format, args);
        }
 
        public void Fatal(string msg)
        {
            log.Fatal(msg);
        }
 
        public void FatalFormat(string format, params object[] args)
        {
            log.FatalFormat(format, args);
        }
 
        #endregion
    }
}