duheng
2024-04-22 a549072a040749f7c732c83d2d0f138614ac08e5
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
 
namespace IStation
{
    public  class LogHelper
    {
        public static void Initial()
        { 
            string  directory   ;
            if (IStation.DataFolderHelper.IsExeExcute)
            {
                directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).FullName;
            }
            else
            {
                directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            }
            var configFile = System.IO.Path.Combine(directory, "Config","log4net.config");
 
            if (System.IO.File.Exists(configFile))
            {
                log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(configFile));         
            }
            else
            {
                log4net.Config.XmlConfigurator.Configure();
            }
 
            logError = log4net.LogManager.GetLogger("IStation.Error");
            logInfo = log4net.LogManager.GetLogger("IStation.Info"); 
            logInfo.Info("初始化成功");
        }
 
        #region  错误日志
        private static log4net.ILog logError = null;
        public static void WriteError(string msg,Exception ex)
        {
            if (logError == null)
                logError = log4net.LogManager.GetLogger("IStation.Error");
            logError.Error(msg, ex);
        }
        public static void WriteError(string msg ,int line, Exception ex)
        {
            if (logError == null)
                logError = log4net.LogManager.GetLogger("IStation.Error");
            logError.Error(string.Format("msg:{0}, line:{1}",msg,line), ex);
        }
        public static void WriteError(string msg)
        {
            if (logError == null)
                logError = log4net.LogManager.GetLogger("IStation.Error");
            logError.Error(msg);
        }
        public static void Error(  Exception ex)
        {
            if (logError == null)
                logError = log4net.LogManager.GetLogger("IStation.Error");
            logError.Error(ex.Message, ex);
        }
        public static void Error(string msg, Exception ex)
        {
            if (logError == null)
                logError = log4net.LogManager.GetLogger("IStation.Error");
            logError.Error(msg, ex);
        }
        public static void Error(string msg, int line, Exception ex)
        {
            if (logError == null)
                logError = log4net.LogManager.GetLogger("IStation.Error");
            logError.Error(string.Format("msg:{0}, line:{1}", msg, line), ex);
        }
        public static void Error(string msg)
        {
            if (logError == null)
                logError = log4net.LogManager.GetLogger("IStation.Error");
            logError.Error(msg);
        }
        #endregion
 
        #region 正常日志
        private static log4net.ILog logInfo = null;
        public static void Info(string info)
        {
            if (logInfo == null)
                logInfo = log4net.LogManager.GetLogger("IStation.Info");
            if (logInfo != null)
            {
                logInfo.Info(info);
            }
        }
 
        #endregion
 
   
 
    }
}