创建事件源后立即设置事件日志属性

发布于 2024-10-25 11:50:28 字数 414 浏览 6 评论 0原文

我有一些创建新事件源的代码:

EventLog.CreateEventSource(Source, LogName);

我知道创建此事件源存在延迟。我想设置一些默认的 EventLog 属性。我正在思考以下问题:

EventLog log = new EventLog();
log.Source = Source;
log.MaximumKilobytes = 16384;
log.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0);

是否有一些创造性的方法可以同时做到这一点?

我想我可以定期检查 EventLog.Exists(...) 直到它返回 true,但似乎必须有一种更干净的方法。

I have some code that creates a new event source:

EventLog.CreateEventSource(Source, LogName);

I know that there's a latency to creating this. I would like to set some default EventLog properties. I'm thinking something along the lines of:

EventLog log = new EventLog();
log.Source = Source;
log.MaximumKilobytes = 16384;
log.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0);

Is there some creative way of doing this at the same time?

I suppose I could periodically check EventLog.Exists(...) until it returns true, but it seems like there must be a cleaner way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷遇一生 2024-11-01 11:50:28

这篇文章很旧,但我通过谷歌搜索来到这里,并认为这可能有用。

如果您正在创建事件日志源(而不是新的事件日志),则您使用 ModifyOverflowPolicy 应用的设置实际上适用于整个事件事件日志而不是您刚刚创建的源。

因此,您应该能够执行此操作:

string LogName = "Application";
EventLog.CreateEventSource(Source, LogName);

EventLog log = new EventLog(LogName);
log.MaximumKilobytes = 16384;
log.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0);

仅当您要写入日志时才使用属性 log.Sourcehttp://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.source.aspx

否则,您可以编写信息日志(例如创建的事件日志)来强制创建日志:

直到第一个条目写入日志后才会创建日志。
http://msdn.microsoft.com/en-us/library/2awhba7a.aspx

This post is old, but I came here by searching with google, and thought this might be useful.

If you are creating an event log source (instead of a new event log), the settings you are applying with ModifyOverflowPolicy are actually for the whole event log and not for the source you've just created.

Therefore you should be able to do this:

string LogName = "Application";
EventLog.CreateEventSource(Source, LogName);

EventLog log = new EventLog(LogName);
log.MaximumKilobytes = 16384;
log.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0);

The property log.Source is only used if you're going to write to the log: http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.source.aspx

Else, you can write an informational log (e.g. Event Log created) to force the log creation:

the log is not created until the first entry is written to it.
http://msdn.microsoft.com/en-us/library/2awhba7a.aspx

醉梦枕江山 2024-11-01 11:50:28

刚刚尝试过,据我所知,仅仅设置最大文件大小不需要 ModifyOverflowPolicy() 调用。此外,即使调用接受较低的值,最小大小似乎也是 1 MB。任何接受的值都存储在注册表中(以字节为单位),但 GUI 和测试显示 1028 kB 是最小值。值得注意的是,GetEventLogs() 调用返回从注册表派生的大小,而不是实际的 1 MB 限制。

if (!System.Diagnostics.EventLog.SourceExists(this.eventSourceName))
{
    System.Diagnostics.EventLog.CreateEventSource(this.eventSourceName, this.eventLogName);
    if (!string.IsNullOrEmpty(this.eventLogMaxSizeKB))
    {
        System.Diagnostics.EventLog myEventLog = new System.Diagnostics.EventLog(this.eventLogName);
        long RoundedToLowest64k = (long.Parse(this.eventLogMaxSizeKB) / 64) * 64;
        myEventLog.MaximumKilobytes = RoundedToLowest64k;
    }
}

在 Windows 7 64 位和 2008 R2 64 位上测试。
我还注意到实际文件大小比您设置的大 4 kB。在 GUI 中,最小大小 1028 kByte 可见,但所有较大值都是 64 的倍数:)

Just tried this and as far as I can tell, the ModifyOverflowPolicy() call is not needed just to set the maximum file size. Also, the minimum size seems to be 1 MB even though the call accepts lower values. Any accepted value is stored in the registry (in bytes) but GUI and tests shows that 1028 kB is the minimum. Worth noting is that the GetEventLogs() call returns the size derived from the registry, not the actual 1 MB limit.

if (!System.Diagnostics.EventLog.SourceExists(this.eventSourceName))
{
    System.Diagnostics.EventLog.CreateEventSource(this.eventSourceName, this.eventLogName);
    if (!string.IsNullOrEmpty(this.eventLogMaxSizeKB))
    {
        System.Diagnostics.EventLog myEventLog = new System.Diagnostics.EventLog(this.eventLogName);
        long RoundedToLowest64k = (long.Parse(this.eventLogMaxSizeKB) / 64) * 64;
        myEventLog.MaximumKilobytes = RoundedToLowest64k;
    }
}

Tested on Windows 7 64-bit and 2008 R2 64-bit.
I also noted that the actual file size is 4 kB larger than what you set. In the GUI this is visible for the minimum size, 1028 kByte, but all larger values are multiples of 64 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文