forked from swhitley/TwitterStreamClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.cs
165 lines (156 loc) · 3.91 KB
/
logger.cs
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.IO;
using System.Net.Mail;
using System.Configuration;
using System.Resources;
using System.Reflection;
using System.Diagnostics;
namespace TwitterStreamClient
{
public class Logger
{
private static string _baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
private string _subDirectory = null;
private string _logDirectory = null;
private string _logFile = null;
private DateTime dt;
public enum LogLevel { OFF, ERROR, WARNING, INFO, ALL };
public enum FileTypes {Log, ReadLog, MailLog};
private FileTypes _fileType = FileTypes.Log;
public FileTypes FileType
{
get { return _fileType;}
set
{
_fileType = value;
switch(value)
{
case FileTypes.Log:
this.SubDirectory = "logs";
break;
case FileTypes.ReadLog:
this.SubDirectory = "readlog";
break;
case FileTypes.MailLog:
this.SubDirectory = "maillog";
break;
}
}
}
public string SubDirectory
{
get
{
if(_subDirectory == null)
{
_subDirectory = "\\logs\\";
}
return _subDirectory;
}
set
{
_subDirectory = "\\" + value + "\\";
_logDirectory = null;
_logFile = null;
}
}
public string LogFile
{
get
{
if(_logFile == null)
{
_logFile = this.LogDirectory + dt.ToString("yyyyMMdd") + ".log";
}
return _logFile;
}
}
public string LogDirectory
{
get
{
if(_logDirectory == null)
{
dt = DateTime.Now;
_logDirectory = _baseDirectory + this.SubDirectory + dt.ToString("yyyyMMdd") + "\\";
if (!Directory.Exists(_logDirectory))
{
Directory.CreateDirectory(_logDirectory);
}
}
return _logDirectory;
}
}
public Logger()
{
}
public void emailLog()
{
MailMessage objMailMessage = new MailMessage();
objMailMessage.Body = "Please review the attached log file.";
objMailMessage.From = new MailAddress(ConfigurationManager.AppSettings["EmailFrom"]);
objMailMessage.To.Add(new MailAddress(ConfigurationManager.AppSettings["EmailTo"]));
objMailMessage.Subject = "-- Log Message --";
if (File.Exists(this.LogFile))
{
objMailMessage.Attachments.Add(new Attachment(this.LogFile));
}
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTPServer"];
smtp.Send( objMailMessage );
}
public void backup()
{
int intCtr = 1;
string filePath1 = this.LogFile;
string filePath2 = this.LogDirectory + dt.ToString("yyyyMMdd") + intCtr.ToString("0000") + ".log";
while(File.Exists(filePath2) && intCtr < 99999)
{
intCtr++;
filePath2 = this.LogDirectory + dt.ToString("yyyyMMdd") + intCtr.ToString("0000") + ".log";
}
if (File.Exists(filePath1))
{
File.Move(filePath1,filePath2);
}
}
public static string MethodName()
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
MethodBase methodBase = stackFrame.GetMethod();
return methodBase.ReflectedType.Name + "." + methodBase.Name;
}
public void append(String message, LogLevel level)
{
int logLevel = (int) level;
int configLogLevel = 0;
string strLogLevel = ConfigurationManager.AppSettings["loglevel"];
configLogLevel = (int)LogLevel.OFF;
if (!String.IsNullOrEmpty(strLogLevel))
{
configLogLevel = (int) Enum.Parse(typeof(LogLevel), strLogLevel);
}
if (configLogLevel >= (int) level)
{
if (!File.Exists(this.LogFile))
{
FileStream fs = File.Create(this.LogFile);
fs.Close();
}
try
{
DateTime dtLog = DateTime.Now;
StreamWriter sw = File.AppendText(this.LogFile);
sw.WriteLine(dtLog.ToString("s") + "\t" + message);
sw.Flush();
sw.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
}
}
}