LogUtil.cs
3.2 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
using log4net;
using System;
using System.Reflection;
using System.Drawing;
using log4net.Repository;
using log4net.Config;
using System.IO;
using System.Text;
using System.Collections.Generic;
using log4net.Util;
namespace Common
{
public class LogUtil
{
static Dictionary<string, LogBean> LogMap = new Dictionary<string, LogBean>();
public delegate void ShowMsg(string msg);
public static event ShowMsg ShowDebug;
public static event ShowMsg ShowInfo;
public static event ShowMsg ShowWarn;
public static event ShowMsg ShowError;
public static readonly LogBean defaultLog = new LogBean(Setting_Str.AppLog);
static LogBean getLogBean(string logname)
{
if (LogMap.ContainsKey(logname))
return LogMap[logname];
return null;
}
public static void Init()
{
XmlConfigurator.Configure(new FileInfo("log4net.config"));
}
public static void AddLogMap(string key, LogBean log)
{
if (!LogMap.ContainsKey(key))
LogMap.Add(key, log);
}
#region Main log
public static void Debug(string msg, LogBean log = null)
{
ShowDebug?.Invoke(msg);
if (log == null)
defaultLog.Debug(msg);
else
log.Debug(msg);
}
public static void Info(string msg, LogBean log = null)
{
ShowInfo?.Invoke(msg);
if (log == null)
defaultLog.Info(msg);
else
log.Info(msg);
}
public static void Info(string msg, string logName)
{
LogBean log = getLogBean(logName);
ShowInfo?.Invoke(msg);
if (log == null)
defaultLog.Info(msg);
else
log.Info(msg);
}
//public static void Info(string msg, string logName)
//{
// ShowInfo?.Invoke(msg);
// if (string.IsNullOrEmpty(logName))
// defaultLog.Info(msg);
// else if (LogMap.ContainsKey(logName) && LogMap[logName] != null)
// LogMap[logName].Info(msg);
//}
public static void Warn(string msg, LogBean log = null)
{
ShowWarn?.Invoke(msg);
if (log == null)
defaultLog.Warn(msg);
else
log.Warn(msg);
}
public static void Error(string msg, Exception exception = null, LogBean log = null)
{
StringBuilder sb = new StringBuilder(msg + "\r\n");
if (log == null)
{
if (exception == null)
defaultLog.Error(msg);
else
{
defaultLog.Error(msg, exception);
sb.Append(exception.StackTrace);
}
}
else
{
if (exception == null)
log.Error(msg);
else
{
log.Error(msg, exception);
sb.Append(exception.StackTrace);
}
}
ShowError?.Invoke(sb.ToString());
}
#endregion
}
}