分支自 SuZhouGuanHong/TaiYuanTaiZhong

WMS
dengjunjie
2023-12-13 d772a6cddc8ed656a2cb5604fc02f6e891db546e
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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
 
namespace WIDESEA_Comm.LogInfo
{
    /// <summary>
    /// 日志写入
    /// </summary>
    public partial class WriteLog
    {
        /// <summary>
        /// 写入日志(不要多线程操作同一文件,一个设备一个文件夹)
        /// </summary>
        public static void Write_Log(string groupName, string logName, string content, object data = null)
        {
            string basePath = System.Environment.CurrentDirectory + "/Log/" + $"/{groupName}/{DateTime.Now.ToString("yyyy_MM_dd")}";
            //如果日志文件目录不存在,则创建
            if (!Directory.Exists(basePath))
            {
                Directory.CreateDirectory(basePath);
            }
            try
            {
                FileStream fs = new FileStream(basePath + "/" + logName + $"{DateTime.Now.ToString("yyyy_MM_dd")}_Log.txt", FileMode.Append);
                StreamWriter strwriter = new StreamWriter(fs);
                try
                {
                    DateTime d = DateTime.Now;
                    strwriter.WriteLine(d.ToString());
                    strwriter.WriteLine(content);
                    if (data != null)
                    {
                        strwriter.WriteLine(JsonConvert.SerializeObject(data));
                    }
                    strwriter.WriteLine("-------------------------------");
                    strwriter.WriteLine();
                    strwriter.Flush();
                }
                catch { }
                finally
                {
                    strwriter.Close();
                    fs.Close();
                }
            }
            catch { }
        }
    }
}