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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace HH.WMS.CoreServer
{
    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 Info(string className, string content)
        {
            WriteLog("INFO", className, content);
        }
 
        /**
        * 向日志文件写入出错信息
        * @param className 类名
        * @param content 写入内容
        */
        public static void Error(string className, string content)
        {
            WriteLog("ERROR", className, content);
        }
 
        /**
        * 实际的写日志操作
        * @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
            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") + ".log";//用日期对日志文件命名
 
            lock (fileLock)
            {
                //创建或打开日志文件,向日志文件末尾追加记录
                StreamWriter mySw = File.AppendText(filename);
 
                //向日志文件写入内容
                string write_content = time + " " + type + " " + className + ": " + content;
                mySw.WriteLine(write_content);
 
                ////关闭日志文件
                mySw.Close();
            }
        }
 
    }
}