简体   繁体   English

从Windows服务发送电子邮件时检查事件日志文件是否已满

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

i am sending email from windows service. 我正在从Windows服务发送电子邮件。 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 您使用EventLog类的OverflowAction属性

More information : http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.overflowaction.aspx 更多信息: 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. 所以上面的代码使用1KB边距来决定日志文件是否已满。 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. 因此,我建议您始终将事件条目包装在异常块中包装,以避免当前日志超出事件日志大小的罕见情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM