LogNetManagment.cs
3.99 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
using System;
using System.Text;
namespace HslCommunication.LogNet
{
/*************************************************************************************
*
* 目标:
* 1. 高性能的日志类
* 2. 灵活的配置
* 3. 日志分级
* 4. 控制输出
* 5. 方便筛选
* 6. 方便的配置按小时,天,月,年记录
*
*
*
*************************************************************************************/
/// <summary>
/// 日志类的管理器
/// </summary>
public class LogNetManagment
{
/// <summary>
/// 存储文件的时候指示单文件存储
/// </summary>
internal const int LogSaveModeBySingleFile = 1;
/// <summary>
/// 存储文件的时候指示根据文件大小存储
/// </summary>
internal const int LogSaveModeByFileSize = 2;
/// <summary>
/// 存储文件的时候指示根据日志时间来存储
/// </summary>
internal const int LogSaveModeByDateTime = 3;
/// <summary>
/// 日志文件的头标志
/// </summary>
internal const string LogFileHeadString = "Logs_";
internal static string GetDegreeDescription(HslMessageDegree degree)
{
switch (degree)
{
case HslMessageDegree.DEBUG: return StringResources.Language.LogNetDebug;
case HslMessageDegree.INFO: return StringResources.Language.LogNetInfo;
case HslMessageDegree.WARN: return StringResources.Language.LogNetWarn;
case HslMessageDegree.ERROR: return StringResources.Language.LogNetError;
case HslMessageDegree.FATAL: return StringResources.Language.LogNetFatal;
case HslMessageDegree.None: return StringResources.Language.LogNetAbandon;
default: return StringResources.Language.LogNetAbandon;
}
}
/// <summary>
/// 公开的一个静态变量,允许随意的设置
/// </summary>
public static ILogNet LogNet { get; set; }
/// <summary>
/// 通过异常文本格式化成字符串用于保存或发送
/// </summary>
/// <param name="text">文本消息</param>
/// <param name="ex">异常</param>
/// <returns>异常最终信息</returns>
public static string GetSaveStringFromException(string text, Exception ex)
{
StringBuilder builder = new StringBuilder(text);
if (ex != null)
{
if (!string.IsNullOrEmpty(text))
{
builder.Append(" : ");
}
try
{
builder.Append(StringResources.Language.ExceptionMessage);
builder.Append(ex.Message);
builder.Append(Environment.NewLine);
builder.Append(StringResources.Language.ExceptionSourse);
builder.Append(ex.Source);
builder.Append(Environment.NewLine);
builder.Append(StringResources.Language.ExceptionStackTrace);
builder.Append(ex.StackTrace);
builder.Append(Environment.NewLine);
builder.Append(StringResources.Language.ExceptionType);
builder.Append(ex.GetType().ToString());
builder.Append(Environment.NewLine);
builder.Append(StringResources.Language.ExceptopnTargetSite);
builder.Append(ex.TargetSite?.ToString());
}
catch
{
}
builder.Append(Environment.NewLine);
builder.Append("\u0002/=================================================[ Exception ]================================================/");
}
return builder.ToString();
}
}
}