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 » Operating System »

Write to Event Log


Posted Date: 27 Mar 2007    Resource Type: Code Snippets    Category: Operating System
Author: Manikandan BalakrishnanMember Level: Gold    
Rating: 1 out of 5Points: 10



System.Diagnostics.EventLog class lets write our application logs to the windows event log, rather than writing logs into the specific file, its easy, and also good in terms of performance, and multiple users environment.

Opening the windows event log:

Go to Run command and type “eventvwr”, then press the OK button, this will open the windows event log, [or] you can also use the control panel to open it. You can see number of event log already exists in the tree view, like “Application”, “security”, “System” etc. in the same manner we can also add our logs from our application.

The following class is used to communicating with windows event log.

public class MyProjectHelper
{
static EventLog EL;
static MyProjectHelper()
{
if (!EventLog.SourceExists("TestApplication"))
{
//Creating new Log, (it will appear as a tree node in windows event log)
EventLog.CreateEventSource("TestApplication", "NewLog");
EL = new EventLog();
EL.Source = "TestApplication";
}

}

public static void AddEvent(string Message)
{
//Log a information to the eventLog
EL.WriteEntry(Message, EventLogEntryType.Information);
}

public static void AddException(string Message)
{
//Log a exception to the eventLog
EL.WriteEntry(Message, EventLogEntryType.Error);
}
public static void AddWarning(string Message)
{
//Log a warning to the eventLog
EL.WriteEntry(Message, EventLogEntryType.Warning);
}
}



Using the MyProjectHelper class to write the event logs

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
//Logging Event
MyProjectHelper.AddEvent("Project Statrted");

//Logging Warning
MyProjectHelper.AddWarning("Too Many users connected with database..");

//Logging Exception
MyProjectHelper.AddException("Exception while accessing DB");
}
}




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: Get Computer Name using Windows API and C# Pointers
Previous Resource: Shutting or Loggin off
Return to Discussion Resource Index
Post New Resource
Category: Operating System


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use