C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Adding Exception Messages to Event Viewer


Posted Date: 16 Apr 2007    Resource Type: Code Snippets    Category: C# Syntax
Author: SuneethaMember Level: Bronze    
Rating: 1 out of 5Points: 10



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
}
}
}







Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Adding Vertex points to Control
Previous Resource: Overloading Main method in C#
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use