调用 WriteEntry 方法时 System.Diagnostics.EventLog 类是否参与事务?

发布于 2024-09-13 17:32:09 字数 434 浏览 6 评论 0原文

我在事务中进行调用,当抛出异常时(从而阻止我的scope.complete()),我看不到它们,即使我知道它们被调用了。

[编辑:为了澄清 - 这是在 Server 2008 R2、.Net 3.5 上运行]

[编辑:添加示例 - 基本上回答了问题,但如果有人可以在某处引用文档]

EventLog.WriteEntry("Start.");

using(TransactionScope scope = new TransactionScope()) {

EventLog.WriteEntry("Middle.");

throw new applicationexception("Whatever");

scope.Complete();

}

EventLog.WriteEntry("End.");

我的事件日志仅显示开始和结束。

I'm making calls in a transaction, and when an exception is thrown (thus preventing my scope.complete()) I don't see them, even though I know they were called.

[Edit: For clarification - this is running on Server 2008 R2, .Net 3.5]

[Edit: added example - basically answers the question, but if someone could cite documentation somewhere]

EventLog.WriteEntry("Start.");

using(TransactionScope scope = new TransactionScope()) {

EventLog.WriteEntry("Middle.");

throw new applicationexception("Whatever");

scope.Complete();

}

EventLog.WriteEntry("End.");

My event log shows only Start and End.

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

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

发布评论

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

评论(1

幽蝶幻影 2024-09-20 17:32:09

EventLog 不支持事务。我认为没有任何关于此的具体文件。这是有道理的,因为文档很少提及支持的内容,除非有明确的理由这样做(澄清、常见误解等)。

从实际的角度来看,一个简单的测试表明,即使事务回滚,也会记录消息:

class Program
{
    static void Main(string[] args)
    {
        EventLog.WriteEntry("Application", "Start");

        using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))
        {
            EventLog.WriteEntry("Application", "123");
        }

        EventLog.WriteEntry("Application", "End");
    }
}

在事件查看器中,我看到 3 个事件(“开始”、“123”、“结束”)。

从理论角度来看,EventLog 需要有一个 资源管理器参与事务EventLog 需要实现 < code>IEnlistmentNotification 或包含实现 IEnlistmentNotification。检查反射器中的 EventLog 表明它没有实现 IEnlistmentNotification

EventLog does not support transactions. I don't think there is any specific documentation on this. Which makes sense since the documentation rarely mentions things that are not supported unless there is some explicit reason to do so (clarifications, common misconception, etc.).

From a practical point of view a simple test shows that messages are logged even if the transaction is rolled back:

class Program
{
    static void Main(string[] args)
    {
        EventLog.WriteEntry("Application", "Start");

        using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))
        {
            EventLog.WriteEntry("Application", "123");
        }

        EventLog.WriteEntry("Application", "End");
    }
}

In the event viewer I see 3 events ("Start", "123", "End").

From a theoretical point of view the EventLog would need to have a resource manager to participate in the transaction. Either EventLog would need to implement IEnlistmentNotification or contain a class that implements IEnlistmentNotification. An inspection of EventLog in reflector shows that it doesn't implement IEnlistmentNotification.

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