tags:

views:

31

answers:

2

Title says it all. 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.

A: 

Can you be more detailed about the question? Also paste code snippet that causes problem. What do u mean by making calls in a transaction? Is is that your are calling EventLog class functions in applications?

Lakshmi
Done. Hope that makes it clear.
Peter LaComb Jr.
@Lakshmi - FYI, your answer should really be a comment since it is just asking for more information.
Tuzo
+1  A: 

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.

Tuzo