-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.cs
198 lines (168 loc) · 6.87 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System.Reflection;
using System.Text;
namespace WorkerService1
{
public class Logger
{
public static void WriteToFile(string Message)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
if (!File.Exists(filepath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(filepath))
{
sw.WriteLine(Message);
}
}
else
{
using (StreamWriter sw = File.AppendText(filepath))
{
sw.WriteLine(Message);
}
}
}
public static void Log(string Message, Exception exception, string classname, LoggerMsgType loggerMsgType = (LoggerMsgType)1)
{
try
{
if (Convert.ToInt32(1) == (int)LoggerMode.File)
{
Logtofile(Message, exception, classname, loggerMsgType);
}
else
{
string CurrentTimeDate = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");
Hl7LogInfo hl7LogInfo = new Hl7LogInfo();
hl7LogInfo.CreatedBy = 99999997;
hl7LogInfo.CreatedOn = DateTime.Now;
hl7LogInfo.MsgType = (int)loggerMsgType;
hl7LogInfo.Message = Message.ToString().Replace("'", "").Trim();
hl7LogInfo.IpAddress = "";
hl7LogInfo.HostName = "";
hl7LogInfo.AddInfo = "";
if (exception != null)
{
hl7LogInfo.Source = exception.Source;
hl7LogInfo.StackTrace = exception.StackTrace + "-" + exception.InnerException;// exception.StackTrace;
hl7LogInfo.TargetSite = exception.TargetSite.ToString();
}
hl7LogInfo.AppName = Assembly.GetCallingAssembly().GetName().Name;
Int64 logid = 0;
try
{
WareedBL wareedBL = new WareedBL();
//SenderType 1:Referral ,2:Health Status
logid = wareedBL.InsertSenderHl7LogInfo(hl7LogInfo, 2);
}
catch (Exception ex)
{
throw ex;
}
}
}
catch (Exception ex)
{
string messgae = ex.Message;
Logtofile(messgae, ex, classname, loggerMsgType);
}
}
private static void Logtofile(string Message, Exception ex, string classname, LoggerMsgType loggerMsgType = (LoggerMsgType)1)
{
try
{
string ErrorOrInfo = "Information";
if (ex != null)
{
ErrorOrInfo = "Error";
}
//IsInformatioLogRequired is required & logger Msg is of type information or
//Error Message then log the information to DB or File
if (((int)loggerMsgType == 1 && IsInformatioLogRequired == 1) || ((int)loggerMsgType == 2))
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
if (!File.Exists(filepath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(filepath))
{
WriteLogToFile(Message, ex, ErrorOrInfo, sw);
}
}
else
{
using (StreamWriter sw = File.AppendText(filepath))
{
WriteLogToFile(Message, ex, ErrorOrInfo, sw);
}
}
}
}
catch
{
}
}
private static void WriteLogToFile(string Message, Exception ex, string ErrorOrInfo, StreamWriter sw)
{
string logdate = DateTime.Now.ToString();
//sw.WriteLine(Message);
sw.WriteLine();
sw.WriteLine($"============={ErrorOrInfo} Logging ===========");
sw.WriteLine($"======================Start============= {logdate}");
sw.WriteLine($"Message:{Message}");
//messgae += exception.StackTrace + "-" + exception.InnerException;// exception.StackTrace;
while (ex != null)
{
sw.WriteLine(ex.GetType().FullName);
if (ex.Message != null)
sw.WriteLine($"Error Message: { ex.Message}");
if (ex.Source != null)
sw.WriteLine($"Error Source: { ex.Source}");
if (ex.TargetSite != null)
sw.WriteLine($"Error TargetSite: { ex.TargetSite.ToString()}");
if (ex.StackTrace != null)
sw.WriteLine($"Stack Trace: {ex.StackTrace}");
ex = ex.InnerException;
}
sw.WriteLine($"======================END============= {logdate}");
}
}
public enum LoggerMode
{
File = 1,
DB = 2,
}
public enum LoggerMsgType
{
Information = 1,
Error = 2,
Warning,
}
public class Hl7LogInfo
{
public long Id { get; set; }
public string Message { get; set; }
public string Source { get; set; }
public string StackTrace { get; set; }
public string TargetSite { get; set; }
public string AddInfo { get; set; }
public string AppName { get; set; }
public string HostName { get; set; }
public string IpAddress { get; set; }
public long CreatedBy { get; set; }
public DateTime CreatedOn { get; set; }
public int? MsgType { get; set; }
public int? DataFlag { get; set; }
}
}