This sample C# code will add The Exceptionmessages to the event viewer.
#region Namespaces using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; #endregion
namespace BRDExceptions { /// /// This is a base class for creating all the Custom Exceptions for XBPD project. /// public class BRDBaseException : ApplicationException {
// Created by Koti R on 05-DEC-2006. // This object will be useful when we try to log all exceptions to the // Windows EventViewer private static EventLog LogObject;
/// /// This is Custom Exception Number assinged for Event Viewer /// private static int CUSTOMEXCEPTIONNUMBER = 9;
/// /// This is System Exception Number assigned for Event Viewer /// private static int SYSTEMEXCEPTIONNUMBER = 18;
const string EventLogSource = "BRD"; const string EventLogLogName = "Application";
// Created by Koti R on 05-DEC-2006 // Will be useful to create an instance of EventLog object. // This constructor executes only for the very first //time of BPDBaseException class usage. static BRDBaseException() { // Creating instance of the EventLog this single instance will // be used through out the application to log all the exceptions // to the EventViewer. LogObject = new EventLog(EventLogLogName);
// Setting the source to the BPD. BRDBaseException.LogObject.Source = EventLogSource;
}// End of static BPDBaseException();
/// /// Programmer has to include the custom message - Reason or Description for the Exception /// Custom Message is read from the Resources file - Culture independant message /// Inner exception is optional but it should be passes wherever applicable /// /// /// public BRDBaseException(string customMessage, Exception Innerexception) : base(customMessage, Innerexception) { try { if (Innerexception != null) {
LogObject.WriteEntry(this.Message + " [Inner Exception:" + Innerexception.ToString() + " ]", EventLogEntryType.Error, CUSTOMEXCEPTIONNUMBER); }// End of if else { LogObject.WriteEntry(this.ToString(), EventLogEntryType.Error, SYSTEMEXCEPTIONNUMBER); }// End of else
}// End of try catch (Exception) { // This catch will eat-up the exception if any occured // during the time of logging the event to the windows event viewer. }// End of catch } } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|