1using System;
2using System.Diagnostics;
3using System.Reflection;
4using System.Security.Principal;
5using System.Data;
6using System.Data.SqlClient;
7
8
9
10namespace Utilities
11{
12 /// <summary>
13 /// Provides a logging layer for applications and services.
14 /// Events can be written to the Application Event log. Failure notices
15 /// can be sent via SMTP email.
16 /// </summary>
17 public class Logging
18 {
19 #region " Private Declarations " ...
20
21
22 private const string EVENT_LOG_NEW_PROC_NAME = "proc_EVENT_LOG_NEW";
23
24 internal static ConfigSettings.ResourceConfig _Cfg;
25 #endregion
26 #region " Public Members " ...
27
28
29
30 /// <summary>
31 /// Log a message to the database, event log, and trace window in VS.NET. There is a seperate
32 /// try catch around each. Don't throw errors if any one of the writes fails.
33 /// </summary>
34 /// <param name="EventLogName"></param>
35 /// <param name="WorkstationName"></param>
36 /// <param name="EventLogSource"></param>
37 /// <param name="Location"></param>
38 /// <param name="Message"></param>
39 /// <param name="Type"></param>
40 /// <returns></returns>
41 public static bool LogMessage(
42 string EventLogName,
43 string WorkstationName,
44 string EventLogSource,
45 string Location,
46 string Message,
47 EventLogEntryType Type)
48 {
49 bool retVal = true;
50 SqlCommand Cmd = null;
51
52 try
53 {
54 // check to see if we have the config
55 if (_Cfg == null)
56 {
57 ConfigSettings Cs = new ConfigSettings();
58 _Cfg = Cs.GetResourceInformation(ConfigSettings.ResourceAlias.GPCS_REPORTING);
59 }
60
61 // Call sproc to write record directly (no DAL)
62 SqlParameter[] Params = null;
63 Params = CreateEventLogParameters(EventLogSource, Location, WorkstationName, Message, Type.ToString());
64 Cmd = CreateSqlCommand(_Cfg.SqlConnectionString, EVENT_LOG_NEW_PROC_NAME, Params);
65 Cmd.ExecuteNonQuery();
66
67 }
68 catch
69 {
70 retVal = false;
71 }
72 finally
73 {
74 if (Cmd != null)
75 {
76 Cmd.Connection.Close();
77 Cmd.Dispose();
78 }
79 }
80
81 // write the error into the event log
82 try
83 {
84 if (!EventLog.SourceExists(EventLogSource))
85 EventLog.CreateEventSource(EventLogSource, EventLogName);
86
87 EventLog.WriteEntry(EventLogSource,
88 "Location: " + Location + Environment.NewLine + Environment.NewLine +
89 "Message: " + Message,
90 Type);
91 }
92 catch
93 {
94 retVal = false;
95 }
96
97 // write the error into the VS.NET trace window
98 try
99 {
100 Trace.WriteLine(Environment.NewLine +
101 "START " + Type.ToString().ToUpper() + " =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- " + Environment.NewLine +
102 "Message: " + Message + Environment.NewLine +
103 "END " + Type.ToString().ToUpper() + " =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- " + Environment.NewLine + Environment.NewLine);
104 return true;
105 }
106 catch
107 {
108 retVal = false;
109 }
110
111 return retVal;
112 }
113
114
115 /// <summary>
116 /// Log an exception to the event log.
117 /// </summary>
118 /// <param name="mb"></param>
119 /// <param name="ex"></param>
120 /// <returns></returns>
121 public static bool LogException(MethodBase mb, ref Exception ex)
122 {
123 try
124 {
125 return LogMessage(
126 "Application",
127 Environment.MachineName,
128 mb.ReflectedType.Namespace,
129 mb.ReflectedType.Namespace + "." + mb.ReflectedType.Name + "." + mb.Name +
130 " [" + ex.Source + "." + ex.TargetSite.ReflectedType.Name + "." + ex.TargetSite.Name + "]" + Environment.NewLine +
131 System.Reflection.Assembly.GetCallingAssembly().CodeBase.ToString().Replace("file://", ""),
132 ex.Message + Environment.NewLine + Environment.NewLine + ex.ToString(),
133 EventLogEntryType.Error);
134 }
135 catch
136 {
137 return false;
138 }
139 }
140 #endregion
141 #region " Internal Static Members " ...
142
143
144 /// <summary>
145 /// Creates a SQL command object
146 /// </summary>
147 /// <param name="SqlConnectionString"></param>
148 /// <param name="StoredProcedureName"></param>
149 /// <param name="Parameters"></param>
150 /// <returns></returns>
151 internal static SqlCommand CreateSqlCommand(
152 string SqlConnectionString,
153 string StoredProcedureName,
154 SqlParameter[] Parameters)
155 {
156 try
157 {
158 SqlConnection Conn = new SqlConnection(SqlConnectionString);
159 SqlCommand Cmd = new SqlCommand(StoredProcedureName, Conn);
160 Cmd.CommandType = CommandType.StoredProcedure;
161 foreach (SqlParameter Prm in Parameters)
162 Cmd.Parameters.Add(Prm);
163 Conn.Open();
164 return Cmd;
165 }
166 catch (Exception)
167 {
168 return null;
169 }
170 }
171
172
173 /// <summary>
174 /// Creates the parameters needed to write the record into the EVENT_LOG table.
175 /// </summary>
176 /// <param name="EventLogSource"></param>
177 /// <param name="Location"></param>
178 /// <param name="WorkstationName"></param>
179 /// <param name="Message"></param>
180 /// <param name="Type"></param>
181 /// <returns></returns>
182 internal static SqlParameter[] CreateEventLogParameters(string EventLogSource,
183 string Location, string WorkstationName, string Message, string Type )
184 {
185 try
186 {
187 SqlParameter[] Params = new SqlParameter[6];
188
189 Params[0] = new SqlParameter("@event_log_source", SqlDbType.VarChar, 100);
190 Params[0].Value = EventLogSource;
191
192 Params[1] = new SqlParameter("@location", SqlDbType.VarChar, 255);
193 Params[1].Value = Location;
194
195 Params[2] = new SqlParameter("@workstation_name", SqlDbType.VarChar, 100);
196 Params[2].Value = WorkstationName;
197
198 Params[3] = new SqlParameter("@message", SqlDbType.VarChar, 1000);
199 // check the length of the message if it is greater than 1000 get the
200 // last 1000 characters
201 string TrimmedMessage = Message;
202 if (Message.Length > 1000)
203 {
204 TrimmedMessage = Message.Substring(Message.Length - 1000, 1000);
205 }
206 Params[3].Value = TrimmedMessage;
207
208 Params[4] = new SqlParameter("@type", SqlDbType.VarChar, 50);
209 Params[4].Value = Type;
210
211 WindowsIdentity CurrentIdentity = WindowsIdentity.GetCurrent();
212 Params[5] = new SqlParameter("@created_by", SqlDbType.VarChar, 50);
213 Params[5].Value = CurrentIdentity.Name;
214
215 return Params;
216 }
217 catch (Exception)
218 {
219 return null;
220 }
221 }
222 #endregion
223 }
224}
225