Does the System.Diagnostics.EventLog class participate in transactions when calling the WriteEntry method?
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.
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
.
精彩评论