HDLogUtil.cs
3.5 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
using IDHIKCamera;
using log4net;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
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();
//LibLogUtil.LogEvent += LibLogUtil_LogEvent;
}
/*
private static void LibLogUtil_LogEvent(LibLogEventArg libLogEventArg)
{
if (libLogEventArg.Level.Equals(LibLogLevel.Info))
{
HDLogUtil.info(libLogEventArg.Msg + " " + libLogEventArg.Exception?.ToString());
}
else if (libLogEventArg.Level.Equals(LibLogLevel.Warning))
{
HDLogUtil.info(libLogEventArg.Msg + " " + libLogEventArg.Exception?.ToString());
}
else if (libLogEventArg.Level.Equals(LibLogLevel.Debug))
{
HDLogUtil.debug(libLogEventArg.Msg + " " + libLogEventArg.Exception?.ToString());
}
else if (libLogEventArg.Level.Equals(LibLogLevel.Error))
{
HDLogUtil.error(libLogEventArg.Msg + " " + libLogEventArg.Exception?.ToString());
}
}
*/
public static void info(string msg)
{
LOGGER.Info("HDLogUtil" + " - " + msg);
AddToBox(msg);
}
public static void debug(string msg)
{
LOGGER.Debug("HDLogUtil" + " - " + msg);
if (debug_opened)
{
AddToBox(msg);
}
}
public static void error(string errorMsg)
{
LOGGER.Error("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;
}
}
}
}