1
duyongjia
2024-11-20 81f3d2205ff6ee8d173f6d30f57ad510ad86e0bf
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace WIDESEAWCS_Common.ServiceLog
{
    public class ServiceLogger
    {
        private static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim();
 
        private static string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log\\" + DateTime.Now.ToString("yyyy-MM-dd"));
 
        private static int size = 10485760;
 
        private static string ext = ".log";
 
        public static void WriteLogToFile(string fileName, string log)
        {
            try
            {
                LogWriteLock.EnterWriteLock();
                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }
 
                string path = Path.Combine(folderPath, GetLastAccessFileName(fileName));
                DateTime now = DateTime.Now;
                string contents = $"【{now}】{Environment.NewLine}{log}";
                File.AppendAllText(path, contents);
            }
            catch
            {
            }
            finally
            {
                LogWriteLock.ExitWriteLock();
            }
        }
 
        private static string GetLogFilePath(string folderPath, string fileName)
        {
            string fileName2 = fileName;
            List<FileInfo> list = (from fi in new DirectoryInfo(folderPath).GetFiles()
                                   where fi.Name.ToLower().Contains(fileName2.ToLower()) && fi.Extension.ToLower() == ext.ToLower() && fi.Length < size
                                   select fi into d
                                   orderby d.Name descending
                                   select d).ToList();
            if (list.Count > 0)
            {
                return list.FirstOrDefault().FullName;
            }
 
            return Path.Combine(folderPath, fileName2 + "_" + DateTime.Now.ToString("HH-mm-ss") + ".log");
        }
 
        private static string GetLastAccessFileName(string fileName)
        {
            string[] existLogFileNames = GetExistLogFileNames(fileName);
            foreach (string text in existLogFileNames)
            {
                if (new FileInfo(text).Length < size)
                {
                    return text;
                }
            }
 
            return fileName + "_" + DateTime.Now.ToString("HH-mm-ss") + ".log";
        }
 
        public static string[] GetExistLogFileNames(string fileName)
        {
            return Directory.GetFiles(folderPath, fileName + "*.log");
        }
 
 
        public static void WriteDebug(string fileName, string msg)
        {
 
            StringBuilder stringBuilder = new StringBuilder(msg);
            stringBuilder.Append(Environment.NewLine);
            stringBuilder.Append(Environment.NewLine);
            WriteLogToFile("APIDebug_" + fileName, stringBuilder.ToString());
 
        }
 
        public static void WriteInfo(string fileName, string msg)
        {
            StringBuilder stringBuilder = new StringBuilder(msg);
            stringBuilder.Append(Environment.NewLine);
            stringBuilder.Append(Environment.NewLine);
            WriteLogToFile("APIInfo_" + fileName, stringBuilder.ToString());
        }
 
     
    }
}