jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Runtime.CompilerServices;
using HH.WMS.Common.External;
 
 
namespace HH.WMS.Common
{
    public class Log
    {
        //在网站根目录下创建日志目录
        /**
         * 向日志文件写入调试信息
         * @param className 类名
         * @param content 写入内容
         */
        public static void Debug(string className, string content)
        {
            WriteLog("DEBUG", className, content);
        }
        /**
      * 向日志文件写入调试信息
      * @param className 类名
      * @param content 写入内容
      */
        public static void AlgorDebug(string className, string content)
        {
 
            WriteAlgorLog("DEBUG", className, content);
        }
 
        /**
        * 向日志文件写入运行时信息
        * @param className 类名
        * @param content 写入内容
        */
        public static void Info(string className, string content)
        {
            WriteLog("INFO", className, content);
        }
        /**
   * 向日志文件写入运行时信息
   * @param className 类名
   * @param content 写入内容
   */
        public static void AlgorInfo(string className, string content)
        {
            WriteAlgorLog("INFO", className, content);
        }
 
        /// <summary>
        /// 在global里面调用的方法里使用
        /// </summary>
        /// <param name="className"></param>
        /// <param name="content"></param>
        public static void DomainInfo(string className, string content)
        {
            WriteDomainLog("INFO", className, content);
        }
 
        /**
        * 向日志文件写入出错信息
        * @param className 类名
        * @param content 写入内容
        */
        public static void Error(string className, string content)
        {
            WriteLog("ERROR", className, content);
        }
 
 
        protected static void WriteDomainLog(string type, string className, string content)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "/logs"; // string.Empty;
            if (!Directory.Exists(path))//如果日志目录不存在就创建
            {
                Directory.CreateDirectory(path);
            }
 
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间
            string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名
 
            lock (fileLock)
            {
                //创建或打开日志文件,向日志文件末尾追加记录
                StreamWriter mySw = File.AppendText(filename);
 
                //向日志文件写入内容
                string write_content = time + " " + type + " " + className + ": " + content;
                mySw.WriteLine(write_content);
 
                ////关闭日志文件
                mySw.Close();
            }
        }
 
        /**
        * 实际的写日志操作
        * @param type 日志记录类型
        * @param className 类名
        * @param content 写入内容
        */
        private static object fileLock = new object();
        protected static void WriteLog(string type, string className, string content)
        {
            string path = string.Empty;
            //HttpContext context = HttpContext.Current;
            //if (context != null)
            //{
            //    //Web
            //    path = HttpContext.Current.Request.PhysicalApplicationPath + "/logs";
            //}
            //else
            //{
            //    //Win
            //    try
            //    {
            //        path = System.Windows.Forms.Application.StartupPath + "/logs";
            //    }
            //    catch (Exception ex)
            //    {
            //        path = "";
            //    }
 
            //}
 
            path = AppDomain.CurrentDomain.BaseDirectory + "/logs";
 
            if (string.IsNullOrEmpty(path))
            {
                string logUrl = System.Configuration.ConfigurationManager.AppSettings["PrintLogUrl"].ToString();
                if (!string.IsNullOrEmpty(logUrl))
                    path = logUrl + "/logs";
                else
                    path = "C:/logs";
            }
 
            if (!Directory.Exists(path))//如果日志目录不存在就创建
            {
                Directory.CreateDirectory(path);
            }
 
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间
            string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + type + ".log";//用日期对日志文件命名
 
            lock (fileLock)
            {
                //创建或打开日志文件,向日志文件末尾追加记录
                StreamWriter mySw = File.AppendText(filename);
 
                //向日志文件写入内容
                string write_content = time + " " + type + " " + className + ": " + content;
                mySw.WriteLine(write_content);
 
                ////关闭日志文件
                mySw.Close();
            }
        }
        protected static void WriteAlgorLog(string type, string className, string content)
        {
            try
            {
                string printLog = "";
                string path = string.Empty;
                HttpContext context = HttpContext.Current;
                if (context != null)
                {
                    //Web
                    path = HttpContext.Current.Request.PhysicalApplicationPath + "/Algorlogs";
                    printLog = System.Configuration.ConfigurationManager.AppSettings["PrintAlgorLog"].ToString();
                }
                else
                {
                    //Win
                    //path = System.Windows.Forms.Application.StartupPath + "/Algorlogs";
                    printLog = System.Configuration.ConfigurationManager.AppSettings["PrintAlgorLog"].ToString();
                }
                if (printLog == "N")
                {
                    return;
                }
                if (!Directory.Exists(path))//如果日志目录不存在就创建
                {
                    Directory.CreateDirectory(path);
                }
 
 
                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间
                string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名
 
                lock (fileLock)
                {
                    //创建或打开日志文件,向日志文件末尾追加记录
                    StreamWriter mySw = File.AppendText(filename);
 
                    //向日志文件写入内容
                    string write_content = time + " " + type + " " + className + ": " + content;
                    mySw.WriteLine(write_content);
 
                    ////关闭日志文件
                    mySw.Close();
                }
            }
            catch (Exception ex)
            {
                //SysLog.Log(ex, SysLog.LogType.CodeException);
            }
        }
 
        public static void Detail(string title, string content)
        {
            WriteLog("DETAIL", title, content);
        }
 
        public static void Detail(LogPara logPara, string content)
        {
            logPara.logPara.Push(content);
            logPara.LogIndex++;
            WriteLog("DETAIL", logPara.LogType + logPara.LogIndex, content);
        }
 
 
        public static int LogIndex = 0;
 
        public static string LogTitle = "";
        public static void InfoNo(string content)
        {
            LogIndex++;
            Info(LogTitle + LogIndex, content);
        }
 
        public static void InfoNo(string title, string content)
        {
            LogIndex = 0;
            LogTitle = title;
            InfoNo(content);
        }
 
        public static void InfoNo(int index, string title, string content)
        {
            LogIndex = index;
            LogTitle = title;
            InfoNo(content);
        }
 
    }
}