Admin
4 天以前 bd6818fc9d40f343547bafca0743658f3c0379dc
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
 
namespace WIDESEA_Common.Tools
{
    /// <summary>
    /// 本地日志记录
    /// </summary>
    public class WriteLog
    {
        /// <summary>
        /// 文件大小
        /// </summary>
        private int fileSize;
        /// <summary>
        /// 日志文件名称
        /// </summary>
        private string logFileName;
        /// <summary>
        /// 日志内容
        /// </summary>
        public static WriteLog log;
        /// <summary>
        /// 设备名称
        /// </summary>
        static string EquipName;
        /// <summary>
        /// 获取日志
        /// </summary>
        /// <param name="equipName">设备名称</param>
        /// <returns></returns>
        public static WriteLog GetLog(string equipName)
        {
            EquipName = equipName;
            //if (log == null)
            log = new WriteLog(equipName);
            log.FileLogPath = AppContext.BaseDirectory + "\\log\\" + DateTime.Now.ToString("yyyyMMdd") /*+ "\\" + EquipName + "_" + DateTime.Now.ToString("yyyyMMdd")*/ + "\\";
            return log;
        }
 
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="equipName">设备名称</param>
        /// <returns></returns>
        public static WriteLog Info(string equipName)
        {
            EquipName = equipName;
            //if (log == null)
            log = new WriteLog("Info" + equipName);
            log.FileLogPath = AppContext.BaseDirectory + "\\log\\Info\\" + DateTime.Now.ToString("yyyyMMdd") /*+ "\\" + EquipName + "_" + DateTime.Now.ToString("yyyyMMdd")*/ + "\\";
            return log;
        }
 
        /// <summary>
        /// 错误
        /// </summary>
        /// <param name="equipName">设备名称</param>
        /// <returns></returns>
        public static WriteLog Error(string equipName)
        {
            EquipName = equipName;
            //if (log == null)
            log = new WriteLog("Error" + equipName);
            log.FileLogPath = AppContext.BaseDirectory + "\\log\\Error\\" + DateTime.Now.ToString("yyyyMMdd") /*+ "\\" + EquipName + "_" + DateTime.Now.ToString("yyyyMMdd")*/ + "\\";
            return log;
        }
 
        private WriteLog(string equipName)
        {
            //初始化大于399M日志文件将自动删除;
 
            this.fileSize = 2048 * 1024 * 200;//50M   2048 * 1024 * 200= 419430000字节(b)=399.9996185兆字节(mb)
 
            //默认路径
 
            //this.FileLogPath = AppContext.BaseDirectory + "\\log\\" + EquipName + "\\";
            EquipName = equipName;
            if (!string.IsNullOrEmpty(equipName))
                this.logFileName = equipName + "_log" + DateTime.Now.ToString("yyyyMMdd") + ".log";
            else
                this.logFileName = "log" + DateTime.Now.ToString("yyyyMMdd") + ".log";
            //this.logFileName = EquipName + "_log" + DateTime.Now.ToString("yyyyMMdd") + ".log";
        }
 
        public int FileSize
        {
            set
            {
                fileSize = value;
            }
            get
            {
                return fileSize;
            }
        }
 
        public string FileLogPath { set; get; } //= AppContext.BaseDirectory + "\\log\\" + EquipName + "\\";
 
        public string LogFileName
        {
            set
            {
                this.logFileName = value;
            }
            get
            {
                return this.logFileName;
            }
        }
        /// <summary>
        /// 方法锁
        /// </summary>
        private static readonly object flag = new object();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Message"></param>
        /// <param name="equipName"></param>
        public void Write(string Message, string equipName)
        {
            lock (flag)
            {
                if (!string.IsNullOrEmpty(equipName))
                    this.logFileName = equipName + "_log" + DateTime.Now.ToString("yyyyMMdd") + ".log";
                else
                    this.logFileName = "log" + DateTime.Now.ToString("yyyyMMdd") + ".log";
                this.Write(this.logFileName, Message, equipName);
            }
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="LogFileName"></param>
        /// <param name="Message"></param>
        /// <param name="equipName"></param>
        public void Write(string LogFileName, string Message, string equipName)
        {
 
            //DirectoryInfo path=new DirectoryInfo(LogFileName);
            //如果日志文件目录不存在,则创建
            if (!Directory.Exists(this.FileLogPath))
            {
                Directory.CreateDirectory(this.FileLogPath);
            }
 
            FileInfo finfo = new FileInfo(this.FileLogPath + LogFileName);
            if (finfo.Exists && finfo.Length > fileSize)
            {
                finfo.Delete();
            }
            try
            {
                using (FileStream fs = new FileStream(this.FileLogPath + LogFileName, FileMode.Append))
                {
                    using (StreamWriter strwriter = new StreamWriter(fs))
                    {
                        strwriter.WriteLine(Message);
                        strwriter.Flush();
                        strwriter.Close();
                    }
                }
            }
            catch (Exception ee)
            {
                Console.Out.WriteLine("日志文件写入失败信息:" + ee.ToString());
            }
        }
    }
}