LogNetDateTime.cs
4.36 KB
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
using System;
using System.Globalization;
using System.IO;
namespace HslCommunication.LogNet
{
/// <summary>
/// 一个日志组件,可以根据时间来区分不同的文件存储
/// </summary>
/// <remarks>
/// 此日志实例将根据日期时间来进行分类,支持的时间分类如下:
/// <list type="number">
/// <item>小时</item>
/// <item>天</item>
/// <item>周</item>
/// <item>月份</item>
/// <item>季度</item>
/// <item>年份</item>
/// </list>
/// </remarks>
public class LogNetDateTime : LogNetBase, ILogNet
{
#region Contructor
/// <summary>
/// 实例化一个根据时间存储的日志组件
/// </summary>
/// <param name="filePath">文件存储的路径</param>
/// <param name="generateMode">存储文件的间隔</param>
public LogNetDateTime(string filePath, GenerateMode generateMode = GenerateMode.ByEveryYear)
{
m_filePath = filePath;
this.generateMode = generateMode;
LogSaveMode = LogNetManagment.LogSaveModeByDateTime;
m_filePath = CheckPathEndWithSprit(m_filePath);
}
#endregion
#region LogNetBase Override
/// <summary>
/// 获取需要保存的日志文件
/// </summary>
/// <returns>完整的文件路径,含文件名</returns>
protected override string GetFileSaveName()
{
if (string.IsNullOrEmpty(m_filePath)) return string.Empty;
switch (generateMode)
{
case GenerateMode.ByEveryHour:
{
return m_filePath + LogNetManagment.LogFileHeadString + DateTime.Now.ToString("yyyyMMdd_HH") + ".txt";
}
case GenerateMode.ByEveryDay:
{
return m_filePath + LogNetManagment.LogFileHeadString + DateTime.Now.ToString("yyyyMMdd") + ".txt";
}
case GenerateMode.ByEveryWeek:
{
GregorianCalendar gc = new GregorianCalendar();
int weekOfYear = gc.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
return m_filePath + LogNetManagment.LogFileHeadString + DateTime.Now.Year + "_W" + weekOfYear + ".txt";
}
case GenerateMode.ByEveryMonth:
{
return m_filePath + LogNetManagment.LogFileHeadString + DateTime.Now.ToString("yyyy_MM") + ".txt";
}
case GenerateMode.ByEverySeason:
{
return m_filePath + LogNetManagment.LogFileHeadString + DateTime.Now.Year + "_Q" + (DateTime.Now.Month / 3 + 1) + ".txt";
}
case GenerateMode.ByEveryYear:
{
return m_filePath + LogNetManagment.LogFileHeadString + DateTime.Now.Year + ".txt";
}
default: return string.Empty;
}
}
#endregion
#region Public Method
/// <summary>
/// 获取所有的文件夹中的日志文件
/// </summary>
/// <returns>所有的文件路径集合</returns>
public string[] GetExistLogFileNames()
{
if (!string.IsNullOrEmpty(m_filePath))
{
return Directory.GetFiles(m_filePath, LogNetManagment.LogFileHeadString + "*.txt");
}
else
{
return new string[] { };
}
}
#endregion
#region Private Member
private string m_fileName = string.Empty; // 当前正在存储的文件名称
private string m_filePath = string.Empty; // 文件的路径
private GenerateMode generateMode = GenerateMode.ByEveryYear; // 文件的存储模式,默认按照年份来存储
#endregion
#region Object Override
/// <summary>
/// 返回表示当前对象的字符串
/// </summary>
/// <returns>字符串</returns>
public override string ToString()
{
return $"LogNetDateTime[{generateMode}]";
}
#endregion
}
}