bklLiudl
2024-07-23 277bbae216debe7e6c04e8cc6ee6e1ba9763e14b
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
 
using System;
using System.IO;
 
namespace Common
{
    public class LogHelper : IDisposable
    {
        private string LogFile;
        private static StreamWriter sw;
        private string logIsWrite = ConfigHelper.GetAppSettings("LogIsWrite");
 
        public LogHelper()
        {
            string fileName = DateTime.Now.ToDateString();
            this.CreateLoggerFile(fileName);
        }
 
        public LogHelper(string _log)
        {
            this.CreateLoggerFile(_log);
        }
 
        private void CreateLoggerFile(string fileName)
        {
            if (this.logIsWrite == "true")
            {
                if (string.IsNullOrEmpty(fileName))
                {
                    fileName = DateTimeHelper.GetToday();
                }
                object _myLogPath = null;
                if (_myLogPath == null)
                {
                    this.LogFile = ConfigHelper.GetAppSettings("LogFilePath");
                    if (!Directory.Exists(this.LogFile))
                    {
                        Directory.CreateDirectory(this.LogFile);
                    }
                }
                else
                {
                    this.LogFile = _myLogPath.ToString();
                }
                if (1 > this.LogFile.Length)
                {
                    Console.WriteLine("配置文件中没有指定日志文件目录!");
                }
                else
                {
                    if (!Directory.Exists(this.LogFile))
                    {
                        Console.WriteLine("配置文件中指定日志文件目录不存在!");
                    }
                    else
                    {
                        if (this.LogFile.Substring(this.LogFile.Length - 1, 1).Equals("/") || this.LogFile.Substring(this.LogFile.Length - 1, 1).Equals("\\"))
                        {
                            this.LogFile = this.LogFile + fileName + ".log";
                        }
                        else
                        {
                            this.LogFile = this.LogFile + "\\" + fileName + ".log";
                        }
                        try
                        {
                            FileStream fs = new FileStream(this.LogFile, FileMode.OpenOrCreate);
                            fs.Close();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                    }
                }
            }
        }
 
        private void writeInfos(string messagestr)
        {
            //DateTime DateNow = default(DateTime);
            try
            {
                this.FileOpen();
                string DateStr = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                string sWrite = DateStr + "\n" + messagestr;
                LogHelper.sw.WriteLine(sWrite);
                LogHelper.sw.Flush();
                LogHelper.sw.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
 
        private void FileOpen()
        {
            LogHelper.sw = new StreamWriter(this.LogFile, true);
        }
 
        private void FileClose()
        {
            if (LogHelper.sw != null)
            {
                LogHelper.sw.Flush();
                LogHelper.sw.Close();
                LogHelper.sw.Dispose();
                LogHelper.sw = null;
            }
        }
 
        public void WriteLog(string msg)
        {
            if (msg != null)
            {
                this.writeInfos(msg.ToString());
            }
        }
 
        public void Dispose()
        {
        }
    }
}