| Author: shivkhare 24 Jan 2005 | Member 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 2005 | Member 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
|