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 » Articles » .NET Framework »

Event Log Creation


Posted Date: 29 Sep 2004    Resource Type: Articles    Category: .NET Framework
Author: Siddhartha HottaMember Level: Bronze    
Rating: 1 out of 5Points: 7



Event log Entry

This article explains how to create event logs.

Generally we create a event log entry in a situation where we cant use message box to show some desired information or error to the end user.this usually happens in the case of windows services which does not have a specfic UI tp interact with the users.It performs its tasks in the background and requires a place where it can log its entries.This is where our event log comes into the picture.

private void EventLogentry()
{
RegistryKey hkLM = null;
RegistryKey hkEventlog = null;
RegistryKey hkKey = null;
try
{
if(!System.Diagnostics.EventLog.Exists("DSP"))
{

if(System.Diagnostics.EventLog.SourceExists("DSP_Source"))
{

System.Diagnostics.EventLog.DeleteEventSource("DSP_Source");
}
System.Diagnostics.EventLog.CreateEventSource("DSP_Source","DSP");
}
else
{
if(System.Diagnostics.EventLog.SourceExists("DSP_Source"))
{
System.Diagnostics.EventLog.DeleteEventSource("DSP_Source");
}
System.Diagnostics.EventLog.CreateEventSource("DSP_Source","DSP");
}
hkLM = Registry.LocalMachine;
hkEventlog = hkLM.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\EventLog\\DSP", true);
hkEventlog.SetValue("Retention",0);
}
catch (Exception ex)
{
XOCErrorHandler.HandleError(ex);
}
finally
{
// Close the registry keys.
if (hkEventlog != null) hkEventlog.Close();
if (hkKey != null) hkKey.Close();
if (hkLM !=null) hkLM.Close();
}

}



Responses

Author: shivkhare    24 Jan 2005Member Level: Bronze   Points : 0
It would be of great help if u can add a bit of explaination with the code.
shiv


Author: Lance W. Larsen    31 Jan 2005Member Level: Bronze   Points : 0
After playing with several code examples - of which I found yours very helpful ( much appreated ) -- I've made the two following methods. Now my only question ( and as you can see I did leave your registry modifciations in ) -- but what specifically does the following line of code accomplish:

registrykeyEventlog.SetValue("Retention",0);

I suspected that it would change the Event Log's "maximum log size" or somthing -- but I didn't see anything different than creating the Log without doing so. Please elaborate. Hope my revisions are of value to you.

Regards...

...Lance W. Larsen (www.lancelarsen.com)

=========================

#region | Method: EnterEventLog() |
///--------------------------------------------------------------------------------
///
/// Write a message to an event log
///

///
/// Example: EnterEventLog("Error Message", System.Diagnostics.EventLogEntryType.Error, "CustEventLog", "AppName");
///

/// Message to be entered into the Event Log
/// System.Diagnostics.EventLogEntryType
/// Name of the Event Log
/// Name of the Source that appears in the Event Log
///
public static bool EnterEventLog(string strEventMessage, System.Diagnostics.EventLogEntryType eventlogType, string strEventLogName, string strEventLogSource)
{
try
{
if ( CreateEventLog(strEventLogName, strEventLogSource) )
{
System.Diagnostics.EventLog eventlogObject = new System.Diagnostics.EventLog();
eventlogObject.Source = strEventLogSource;

eventlogObject.WriteEntry("Test Message",eventlogType);
}
}
catch (Exception exceptionGeneral)
{
throw new ArgumentException("Exception(EnterEventLog): ", exceptionGeneral.Message);
}

return true; //--- Successful
}
///--------------------------------------------------------------------------------
#endregion

#region | Method: CreateEventLog() |
///--------------------------------------------------------------------------------
///
/// Create the event log and source
///

/// Name of the Event Log
/// Name of the Source that appears in the Event Log
private static bool CreateEventLog(string strEventLogName, string strEventLogSource)
{
Microsoft.Win32.RegistryKey registrykeyLM = null;
Microsoft.Win32.RegistryKey registrykeyEventlog = null;
Microsoft.Win32.RegistryKey registrykeyKey = null;

try
{
//--- Check to see that event log name exists, if so...
if (System.Diagnostics.EventLog.Exists(strEventLogName))
{
//--- Check to see if the source exists, if not...
if(!System.Diagnostics.EventLog.SourceExists(strEventLogSource))
{
//--- Create new source
System.Diagnostics.EventLog.CreateEventSource(strEventLogSource,strEventLogName);
}
}
else //--- Log name does not exist
{
//--- Check to see if the source exists, if it exists but the log does not, delete source
if (System.Diagnostics.EventLog.SourceExists(strEventLogSource))
{
//--- Delete source
System.Diagnostics.EventLog.DeleteEventSource(strEventLogSource);
}

//--- Create new log and source
System.Diagnostics.EventLog.CreateEventSource(strEventLogSource,strEventLogName);

//--- Modify registry settings on local machine
registrykeyLM = Microsoft.Win32.Registry.LocalMachine;
registrykeyEventlog = registrykeyLM.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\EventLog\\"+strEventLogName, true);
registrykeyEventlog.SetValue("Retention",0);
}

}
catch (Exception exceptionGeneral)
{
throw new ArgumentException("Exception(CreateEventLog): ", exceptionGeneral.Message);
}
finally
{
// Close the registry keys.
if (registrykeyEventlog != null) registrykeyEventlog.Close();
if (registrykeyKey != null) registrykeyKey.Close();
if (registrykeyLM != null) registrykeyLM.Close();
}

return true; //--- Successful
}
///--------------------------------------------------------------------------------
#endregion


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: Calling Com Components from .Net Application
Previous Resource: OverView of Assemblies
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use