-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.cs
More file actions
125 lines (113 loc) · 3.64 KB
/
Copy pathLog.cs
File metadata and controls
125 lines (113 loc) · 3.64 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
using System;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Threading;
namespace BaseCommon
{
/// <summary>
/// 日志类
/// </summary>
public class Log
{
#region 日志记录
private static Log _logger;
private static readonly string LogFilePath = string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["LogPath"]) ? System.Environment.CurrentDirectory + "\\log\\" : System.Configuration.ConfigurationManager.AppSettings["LogPath"];
private static readonly Mutex Mutex = new Mutex(false);
public static Log log
{
get
{
if (_logger == null) _logger = GetInstance();
return _logger;
}
}
/// <summary>
/// 构造
/// </summary>
private Log()
{
}
/// <summary>
/// 单键
/// </summary>
/// <returns></returns>
public static Log GetInstance()
{
if (_logger == null)
{
_logger = new Log();
if (!Directory.Exists(LogFilePath))
{
Directory.CreateDirectory(LogFilePath);
}
}
return _logger;
}
/// <summary>
/// 错误日志暴露接口
/// </summary>
/// <param name="message">The message.</param>
public void WriteDebugLog(string message)
{
WriteLogFile(message);
}
/// <summary>
/// 错误日志记录
/// </summary>
/// <param name="message">The message.</param>
private static void WriteLogFile(string message)
{
if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["LogEnable"])) return;
try
{
Mutex.WaitOne();
string strSysTime = DateTime.Now.ToString("yyyyMMddHH:mm:ss.FFF");
string logFullPath = LogFilePath + strSysTime.Substring(0, 10) + "_" + ".log";
string logstr = message;
if (!File.Exists(logFullPath))
{
using (StreamWriter sw = File.CreateText(logFullPath))
{
sw.Write(strSysTime + " " + logstr + "\r\n");
sw.Close();
}
}
else
{
using (StreamWriter sw = File.AppendText(logFullPath))
{
sw.Write(strSysTime + " " + logstr + "\r\n");
sw.Close();
}
}
}
finally
{
Mutex.ReleaseMutex();
}
}
#endregion
public void Error(string message, Exception ex)
{
if (ex != null)
WriteLogFile(string.Format("{0}.{1} Exception:{2},{3},Detail:{4}", "", "", message, ex.Message, ex.StackTrace));
else
{
Error(message);
}
}
public void WriteErrLog(string message, Exception ex, string p_5)
{
WriteLogFile(string.Format("{0}.{1} Exception:{2},{3},Detail:{4} \n Addtion:{5}", "", "", message, ex.Message, ex.StackTrace, p_5));
}
internal void Error(string p)
{
WriteLogFile(p);
}
public void Error(string message, string classname, string methodname)
{
WriteLogFile(string.Format("{0},{1},{2}", classname, methodname, message));
}
}
}