LogExecute.cs
2.75 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HHECS.Bll
{
/// <summary>
/// 日志帮助组件
/// </summary>
public class LogExecute
{
static readonly string LogPath = "D:\\";
public const string ExceptionTag = "ExceptionTag";
public static void WriteInfoLog(string Message, bool IsSucc)
{
string s = IsSucc ? "成功" : "失败";
WriteInfoLog(Message + ",操作结果[" + s + "]");
}
public static void WriteDBExceptionLog(Exception ex)
{
WriteExceptionLog("DBExecute", ex);
}
public static void WriteExceptionLog(string tile, Exception ex)
{
try
{
if (ex != null && ex.Message != ExceptionTag)
{
StringBuilder sb = new StringBuilder();
string NowDateTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
sb.AppendLine(string.Format("****************************{0},Exception[{1}]****************************", NowDateTime, tile));
sb.AppendLine(ex.ToString());
WriteLogExecute("Exception", sb.ToString());
}
}
catch
{
}
}
public static void WriteInfoLog(string Message)
{
WriteLogExecute("Info", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + " " + Message);
}
public static void WriteLineDataLog(string Message)
{
WriteLogExecute("Data", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + " " + Message);
}
static void WriteLogExecute(string FileName, string Message)
{
//如果日志文件目录不存在,则创建
if (!Directory.Exists(LogPath))
{
Directory.CreateDirectory(LogPath);
}
string filename = LogPath + "\\" + FileName + "_" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";
try
{
FileStream fs = new FileStream(filename, FileMode.Append);
StreamWriter strwriter = new StreamWriter(fs);
try
{
strwriter.WriteLine(Message);
strwriter.Flush();
}
catch
{
}
finally
{
strwriter.Close();
strwriter = null;
fs.Close();
fs = null;
}
}
catch
{
}
}
}
}