-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cs
More file actions
125 lines (122 loc) · 5.58 KB
/
Logger.cs
File metadata and controls
125 lines (122 loc) · 5.58 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
125
using System;
using System.IO;
using System.Web;
using System.Xml;
using System.Diagnostics;
namespace LothianProductions.VoIP
{
/// <summary>
/// Class to log error/applications messages
/// </summary>
public class Logger
{
public Logger() { }
public string EventLogName;
protected readonly static Logger mInstance = new Logger();
public enum LoggerLevel { Error, Warning, Information };
public static Logger Instance() {
return mInstance;
}
public void Log(string strLogMessage) {
Log(strLogMessage, LoggerLevel.Error);
}
public void Log(string strLogMessage, LoggerLevel level) {
lock (this) {
string strType = System.Configuration.ConfigurationManager.AppSettings["logType"];
switch (strType) {
case "eventlog":
EventLogEntryType et;
switch (level) {
case LoggerLevel.Error:
et = EventLogEntryType.Error;
break;
case LoggerLevel.Information:
et = EventLogEntryType.Information;
break;
case LoggerLevel.Warning:
et = EventLogEntryType.Warning;
break;
default:
et = EventLogEntryType.Information;
break;
}
StampEventLogEntry(strLogMessage, et);
break;
case "xml":
StampXmlLogEntry(strLogMessage, level);
break;
default:
string strFile = "";
try {
strFile = System.Configuration.ConfigurationManager.AppSettings["logFile"];
} catch (System.Configuration.SettingsPropertyNotFoundException) {
strFile = System.Environment.CurrentDirectory + "\\debug.log";
}
if (strFile == "") {
strFile = System.Environment.CurrentDirectory + "\\debug.log";
} else {
strFile = System.Environment.CurrentDirectory + "\\" + strFile;
}
StreamWriter objLogFile = File.AppendText(strFile);
objLogFile.WriteLine(DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss.ffffff") + "|" + level.ToString() + "|" + strLogMessage);
objLogFile.Close();
break;
}
}
}
public void Log(string strLogMessage, Exception ex) {
Log(strLogMessage + ": " + ex.ToString());
}
public void Log(string strLogMessage, Exception ex, LoggerLevel level) {
Log(strLogMessage + ": " + ex.ToString(), level);
}
private void StampEventLogEntry(string strMessage, EventLogEntryType evtType) {
if (EventLogName == "") EventLogName = "LovettsUtilities";
if (!EventLog.SourceExists(EventLogName)) {
EventLog.CreateEventSource(EventLogName, EventLogName);
}
EventLog objLog = new EventLog();
objLog.Source = EventLogName;
try {
objLog.WriteEntry(strMessage, evtType);
} catch (Exception ex) {
throw ex;
}
}
private void StampXmlLogEntry(string strMessage, LoggerLevel level) {
XmlDocument doc = new XmlDocument();
string strFile = "";
try {
strFile = System.Configuration.ConfigurationManager.AppSettings["logFile"];
} catch (System.Configuration.SettingsPropertyNotFoundException) {
strFile = System.Environment.CurrentDirectory + "\\debug.log";
}
if (strFile == "") {
strFile = System.Environment.CurrentDirectory + "\\debug.log";
} else {
strFile = System.Environment.CurrentDirectory + "\\" + strFile;
}
if (File.Exists(strFile)) {
doc.Load(strFile);
XmlNode elMessages = doc.LastChild;
XmlElement eleMessage = doc.CreateElement("message");
eleMessage.SetAttribute("timestamp", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss.ffffff"));
XmlText messageText = doc.CreateTextNode(strMessage);
eleMessage.AppendChild(messageText);
elMessages.AppendChild(eleMessage);
doc.Save(strFile);
} else {
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.InsertBefore(dec, doc.DocumentElement);
XmlElement el = doc.CreateElement("logMessages");
XmlElement eleMessage = doc.CreateElement("message");
eleMessage.SetAttribute("timestamp", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss.ffffff"));
XmlText messageText = doc.CreateTextNode(strMessage);
eleMessage.AppendChild(messageText);
el.AppendChild(eleMessage);
doc.AppendChild(el);
doc.Save(strFile);
}
}
}
}