-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.cs
More file actions
92 lines (82 loc) · 2.47 KB
/
Log.cs
File metadata and controls
92 lines (82 loc) · 2.47 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.Text;
using System.IO;
namespace UHFReader288MPDemo
{
/// <summary>
/// 日志操作对象,提供将软件运行过程中一些状况记录入日志的操作接口;
/// </summary>
public class Log
{
public static string PATH;
public static bool WriteLog(string log)
{
return write(log, "Log");
}
public static bool WriteError(string error)
{
return write(error, "Error");
}
public static bool WriteException(Exception exception)
{
if (exception.InnerException != null)
{
write("InnerException: " + exception.InnerException.ToString(), "Error");
}
if (exception.Message != null)
{
write("Message: " + exception.Message.ToString(), "Error");
}
if (exception.Source != null)
{
write("Source: " + exception.Source.ToString(), "Error");
}
if (exception.StackTrace != null)
{
write("StackTrace :" + exception.StackTrace.ToString(), "Error");
}
if (exception.TargetSite != null)
{
write("TargetSite :" + exception.TargetSite.ToString(), "Error");
}
write("-------------------------------------------------------------------------", "Error");
return true;
}
private static bool write(string text, string writeType)
{
StreamWriter f = null;
try
{
string path = PATH + @"\Log";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path += @"\" + DateTime.Now.ToString("yyyyMMdd") + writeType + ".txt";
if (File.Exists(path))
{
f = File.AppendText(path);
}
else
{
f = File.CreateText(path);
}
f.WriteLine(text);
return true;
}
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
return false;
}
finally
{
if (f != null)
{
f.Close();
}
}
}
}
}