Results 1 to 10 of 10

Thread: Determining if a log file is filled

  1. #1

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Determining if a log file is filled

    Hi. I know how to write to a log file. I know how to clear a log file. The thing I do not know is how to determine when the log file is filled.

    Usually when i write a log I would check for an exception and this will work e.g.

    Code:
    try
    			{
    				MyLog.WriteEntry(logentry, logtype); // Write a log file.
    
    			}
    			catch(System.ComponentModel.Win32Exception)
    			{
    				// clear the log file
    MyLog.Clear();
    				
    			}



    However, apparently this is not working since I'm still getting a dialog box when the log file is filled on this machine. I do not want that dialog box, I want the log file to clear automatically. Anyone know a concrete way to find out if the log file is filled?

    Jennifer

  2. #2
    Hyperactive Member BrandonTurner's Avatar
    Join Date
    Sep 2001
    Location
    East Lansing, Michiagn
    Posts
    268

    Re: Determining if a log file is filled

    If you never write an empty log... then just check file size.

  3. #3
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Determining if a log file is filled

    A custom event log can contain maximum of 512KB of data. So you can check the myLog.Log.Length property to find out the length of the log before clearing it.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  4. #4

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: Determining if a log file is filled

    Hey i'm getting a small problem here. What I use MyLog.Log.Length, I'm getting 14 but it's supposed to be 512. What am i Doing wrong? I want to find the size of the entire log but it's only outputting 14
    Jennifer

  5. #5
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Determining if a log file is filled

    Use [code] source code here[/code] tags when you post source code.

    My Articles

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Determining if a log file is filled

    Are you using .NET 2.0? I don't know because you haven't bothered to specify. If you are then you should look at the EventLog.OverflowAction property. Please use the radio buttons provided to specify your IDE/Framework version when creating a thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: Determining if a log file is filled

    I'm using .net 2003.

  8. #8

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: Determining if a log file is filled

    Ok. I've tried everything. I've come to the conclusion that the size of a log file just cannot be retrieved.

  9. #9

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: Determining if a log file is filled

    Hey, does anyone knows how to find the path of the log file? If I find the path, I could easily find the size of the .evt file.

  10. #10

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: Determining if a log file is filled

    Hey. I was doing some research and the Event.Overflow action is only for version 2.0 but I'm using 1.1 in which that is not available. Now I researched some code to find the names and size of all the log files. The following is the code:

    Code:
    // Create a EventLog array of all the log files.
    			EventLog[] AllEventLogs = EventLog.GetEventLogs(); 
    
    			string OutputMess = ""; // Message to output to the messagebox to display.
    
    			foreach(EventLog e in AllEventLogs)
    			{
    				Int64 SizeKB = 0; // Size of the file.
    
    				OutputMess += e.LogDisplayName.ToString()+ "  "; // Add the Log name to the output message.
    				
    				// Determine if there is an event log file for this event log.
    				RegistryKey RegEventLog = Registry.LocalMachine.OpenSubKey(
    					"System\\CurrentControlSet\\Services\\EventLog\\" + e.Log);
    
    				if (RegEventLog != null)
    				{
    					Object temp = RegEventLog.GetValue("File");
    					
    					if (temp != null)
    					{
    						// Add the path to the output message.
    						OutputMess += "Path is: " +temp.ToString()+ "  ";
    
    						FileInfo file = new FileInfo(temp.ToString());
    
    						// Get the current size of the event log file.
    						if (file.Exists)
    						{
    							SizeKB = file.Length / 1024;
    							if ((file.Length % 1024) != 0)
    							{
    								SizeKB++;
    							}/* 'if ((file.Length % 1024) != 0)' delimiter */
    
    							// Add the size in KB to the output message.
    							OutputMess += "Current size in KiloBytes: " +SizeKB.ToString()+ "  ";
    
    						}
    					}
    					else
    					{
    						OutputMess += "Log file path = <not set>";
    
    					}
    
    				}
    
    				OutputMess += "\r\n\r\n";
    
    			}
    
    			
    			MessageBox.Show(OutputMess); // Output the concatenated output message.


    Now this is working for just a few log files. It is working for the application, security log files and also for the system log files but not for custom log files. What is being outputted for the custom log file is the name and "Log path is not set" which means that RegistryKey RegEventLog returned null for every custom log file I created. So I'm getting the path and sizes of the application, system and security log files but not my custom log files. But there is a path, I just do not know how to access it relatively. I do not want to hard code the path. One more observation, when I used window Server 2000, the above code worked fine. I got the sizes of all the log files including my custom logs. However when I used window XP, its not displaying the path and thus the size of the log file. It's not reading the path. Anyone know how I could get the path to my custom made log files so I could find the size of the .evt file?

    Darn I'm so close, I found out how to calculate the size of the log file, just missing how to get the relative path for my custom logs!

    Jennifer
    Last edited by drattansingh; Apr 20th, 2006 at 10:24 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width