I found a nice way for logging errors from exceptions to the event log. This is for when your website is in production and you want to keep track of exceptional errors.
using System.Diagnostics;
public static void WriteErrorToEventLog(string applicationName, string msg)
{
StackFrame CallStack = new StackFrame(1, true);
int lineNumber = CallStack.GetFileLineNumber();
string msg2 = CallStack.GetFileName() + "\r\n"; // This get calling module's file name and adds to event log.
msg2 += "Line: " + lineNumber.ToString() + "\r\n"; // This get calling module's line number of call to this function.
msg2 += msg;
if (!EventLog.SourceExists(applicationName))
{
EventLog.CreateEventSource(applicationName, "Application");
}
EventLog.WriteEntry(applicationName, msg2, EventLogEntryType.Error, lineNumber);
}
public static void WriteWarningToEventLog(string applicationName, string msg)
{
StackFrame CallStack = new StackFrame(1, true);
int lineNumber = CallStack.GetFileLineNumber();
string msg2 = CallStack.GetFileName() + "\r\n"; // This get calling module's file name and adds to event log.
msg2 += "Line: " + lineNumber.ToString() + "\r\n"; // This get calling module's line number of call to this function.
msg2 += msg;
if (!EventLog.SourceExists(applicationName))
{
EventLog.CreateEventSource(applicationName, "Application");
}
EventLog.WriteEntry(applicationName, msg2, EventLogEntryType.Warning, lineNumber);
}
public static void WriteInfoToEventLog(string applicationName, string msg)
{
StackFrame CallStack = new StackFrame(1, true);
int lineNumber = CallStack.GetFileLineNumber();
string msg2 = CallStack.GetFileName() + "\r\n"; // This get calling module's file name and adds to event log.
msg2 += "Line: " + lineNumber.ToString() + "\r\n"; // This get calling module's line number of call to this function.
msg2 += msg;
if (!EventLog.SourceExists(applicationName))
{
EventLog.CreateEventSource(applicationName, "Application");
}
EventLog.WriteEntry(applicationName, msg2);
}
I like be able to include the source module name and line number in the event log for reverse engineering.
Member
18 Points
47 Posts
Writing error messages to event log, __LINE__ and __FILE__ in C#
Jul 22, 2007 08:49 PM|DaveFromNJ|LINK
I found a nice way for logging errors from exceptions to the event log. This is for when your website is in production and you want to keep track of exceptional errors.
using System.Diagnostics;
public static void WriteErrorToEventLog(string applicationName, string msg)
{
StackFrame CallStack = new StackFrame(1, true);
int lineNumber = CallStack.GetFileLineNumber();
string msg2 = CallStack.GetFileName() + "\r\n"; // This get calling module's file name and adds to event log.
msg2 += "Line: " + lineNumber.ToString() + "\r\n"; // This get calling module's line number of call to this function.
msg2 += msg;
if (!EventLog.SourceExists(applicationName))
{
EventLog.CreateEventSource(applicationName, "Application");
}
EventLog.WriteEntry(applicationName, msg2, EventLogEntryType.Error, lineNumber);
}
public static void WriteWarningToEventLog(string applicationName, string msg)
{
StackFrame CallStack = new StackFrame(1, true);
int lineNumber = CallStack.GetFileLineNumber();
string msg2 = CallStack.GetFileName() + "\r\n"; // This get calling module's file name and adds to event log.
msg2 += "Line: " + lineNumber.ToString() + "\r\n"; // This get calling module's line number of call to this function.
msg2 += msg;
if (!EventLog.SourceExists(applicationName))
{
EventLog.CreateEventSource(applicationName, "Application");
}
EventLog.WriteEntry(applicationName, msg2, EventLogEntryType.Warning, lineNumber);
}
public static void WriteInfoToEventLog(string applicationName, string msg)
{
StackFrame CallStack = new StackFrame(1, true);
int lineNumber = CallStack.GetFileLineNumber();
string msg2 = CallStack.GetFileName() + "\r\n"; // This get calling module's file name and adds to event log.
msg2 += "Line: " + lineNumber.ToString() + "\r\n"; // This get calling module's line number of call to this function.
msg2 += msg;
if (!EventLog.SourceExists(applicationName))
{
EventLog.CreateEventSource(applicationName, "Application");
}
EventLog.WriteEntry(applicationName, msg2);
}
I like be able to include the source module name and line number in the event log for reverse engineering.