HDLogUtil.cs
2.9 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
using log4net;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace CodeLibrary
{
public class HDLogUtil
{
private static string logName = "";
public static string LogName
{
get { return logName; }
set
{
logName = value;
LOGGER = LogManager.GetLogger(LogName);
}
}
private static ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static Dictionary<int, DateTime> lastErrorLogTime = new Dictionary<int, DateTime>();
public static System.Windows.Forms.RichTextBox logBox = null;
public static int showCount = 50;
public static bool debug_opened = false;
static HDLogUtil()
{
XmlConfigurator.Configure();
}
public static void info(string msg)
{
LOGGER.Info("HDLogUtil" + " - " + msg);
OutputDebugString("HDLogUtil" + " - " + msg);
AddToBox(msg);
}
public static void debug(string msg)
{
LOGGER.Debug("HDLogUtil" + " - " + msg);
OutputDebugString("HDLogUtil" + " - " + msg);
if (debug_opened)
{
AddToBox(msg);
}
}
public static void error(string errorMsg)
{
LOGGER.Error("HDLogUtil" + " - " + errorMsg);
OutputDebugString("HDLogUtil" + " - " + errorMsg);
AddToBox(errorMsg);
}
private static void AddToBox(string msg)
{
try
{
if (logBox == null)
{
return;
}
ShowLogPro(msg);
}
catch (Exception ex)
{
LOGGER.Error("出错:" + ex.StackTrace);
}
}
private static int count = 0;
private static void ShowLogPro(string msg)
{
try
{
if (count > showCount)
{
count = 0;
logBox.Clear();
}
System.DateTime now = System.DateTime.Now;
logBox.AppendText(now.ToLongTimeString() + " " + msg + Environment.NewLine);
count++;
}
catch (Exception ex)
{
LOGGER.Error("出错:" + ex.ToString());
}
}
public static void ClearLog()
{
if (logBox != null)
{
logBox.Text = "";
count = 0;
}
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern void OutputDebugString(string message);
}
}