Home
Manage Your Code
Snippet: event log catch block (C#)
Title: event log catch block Language: C#
Description: catch block that writes to the event log if the event log source exists. Views: 186
Author: Tim Mays Date Added: 6/16/2008
Copy Code  
1catch (Exception ex)
2        {
3            if (EventLog.SourceExists(ConfigurationManager.AppSettings["EventLogSource"]))
4            {
5                List<string> exVarList = new List<string>();
6                exVarList.Add(ex.Source + ":");
7                StringBuilder sb = new StringBuilder();
8                sb.AppendLine("Exception:==================");
9                sb.AppendLine(ex.ToString());
10                sb.AppendLine("Variable List:==============");
11                foreach (string s in exVarList)
12                {
13                    sb.AppendLine(s);
14                }
15                EventLog eventLog = new EventLog(ConfigurationManager.AppSettings["EventLogName"], 
16                                                         ConfigurationManager.AppSettings["EventLogMachine"], 
17                                                         ConfigurationManager.AppSettings["EventLogSource"]);
18
19                eventLog.WriteEntry(sb.ToString(), EventLogEntryType.Warning);
20            }
21        }
Notes
You need an event log source to use this...