开发者

check for event log file full while sending email from windows service

i am sending email from windows service. It threw an error of "event log file is full" sometimes when i am writing the event entry into event viewer.开发者_开发问答

How to check whether it is full or not?

thanks


You use OverflowAction property of EventLog class

More information : http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.overflowaction.aspx


Event Log size is based on storage size and not by number of entries so it would be little bit difficult to figure out if event log is full or not. For example,

bool logFull = false;
EventLog e = ... // get the needed event log
var sizeKB = e.MaximumKilobytes; // event log size
// Check current event log size
var regEntry = Rgistry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Services\\EventLog\\" + e.Log);
if (regEntry != null)
{
  var filePath = regEntry.GetValue("File");
  if (filePath != null)
  {
      var file = new FileInfo(filePath.ToString());
      if (file.Exists)
      {
         var fileSize = (file.Length + 1023) / 1024;
         logFull = (fileSize >= sizeKB); // a 1K margin
      }
   }
}

So above code is using 1KB margin to decide if log file is full or not. As such, I would suggest that you always wrap your event entry writing code inside exception block to avoid rare scenario where current log may take you beyond event log size.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜