为什么 EventRecord.FormatDescription() 返回 null?

发布于 2024-12-06 07:46:05 字数 157 浏览 1 评论 0原文

使用 System.Diagnostics.Eventing.Reader.EventLogQuery 从 Windows 事件日志读取事件时,EventRecord.FormatDescription() 方法有时会返回 null。这是为什么呢?在事件查看器中,有有关返回 null 的事件的消息。

When using System.Diagnostics.Eventing.Reader.EventLogQuery to read events from the Windows Event Log, the EventRecord.FormatDescription() method sometimes returns null. Why is this? In the Event Viewer there are messages on the events which return null.

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

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

发布评论

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

评论(2

雨的味道风的声音 2024-12-13 07:46:05

这是由于 .NET 框架中的错误造成的。

基本上,要解决此错误,您需要做的就是将 CurrentCulture 设置为“en-US”。

示例:

var beforeCulture = Thread.CurrentThread.CurrentCulture;

try
{
  Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

  using (var session = new EventLogSession(ipOrAddress, userDomain, username, password, SessionAuthentication.Default))
  {
    var query = new EventLogQuery("System", PathType.LogName, queryString)
      {
        ReverseDirection = true,
        Session = session
      };

    using (var reader = new EventLogReader(query))
    {
      for (var record = reader.ReadEvent(); record != null; record = reader.ReadEvent())
      {
        // Read event records
        string message = record.FormatDescription();
      }
    }
  }
}
finally
{
  Thread.CurrentThread.CurrentCulture = beforeCulture;
}

这个解决方法is很难找到,所以我想我应该将其记录在一个将被Google索引的地方。我在旧的 MS Connect 中找到了它案例,但已关闭,状态为“无法修复”。

更新:
该错误已报告对于 .NET 4 也是如此,状态为“发送给工程团队考虑”,并评论暗示该错误可能会在下一个主要 .NET 框架版本 (v5) 中得到修复。

This is due to a bug in the .NET framework.

Basically what you need to do to work around this bug is to set the CurrentCulture to "en-US".

Example:

var beforeCulture = Thread.CurrentThread.CurrentCulture;

try
{
  Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

  using (var session = new EventLogSession(ipOrAddress, userDomain, username, password, SessionAuthentication.Default))
  {
    var query = new EventLogQuery("System", PathType.LogName, queryString)
      {
        ReverseDirection = true,
        Session = session
      };

    using (var reader = new EventLogReader(query))
    {
      for (var record = reader.ReadEvent(); record != null; record = reader.ReadEvent())
      {
        // Read event records
        string message = record.FormatDescription();
      }
    }
  }
}
finally
{
  Thread.CurrentThread.CurrentCulture = beforeCulture;
}

This workaround is was very hard to find, so I thought I would document it a place where it will be indexed by Google. I found it in an old MS Connect case, but it has been closed with a status of "wont fix".

UPDATE:
The bug has been reported for .NET 4 as well and the status is "Sent to Engineering Team for consideration" and comment alluding that the bug might be fixed in the next major .NET framework release (v5).

琉璃繁缕 2024-12-13 07:46:05

所以我也为此苦苦挣扎了几天。我无法通过改变文化来让它发挥作用。最后,我只是使用了事件记录的 Properties 属性中的原始数据。消息数据就在那里,只是不太漂亮。 (不过足以满足我的审计需求:-))

so i've been struggling with this for a few days too. I couldn't get it to work by changing the culture. In the end, i just used the raw data in the Properties property of the event record. The message data is in there, it's just not pretty. (just about good enough for my audit needs though :-))

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