Calling ConsoleEventListener.EnableEvents causes error 'item with the same key has already been added'

Topics: Semantic Logging Application Block
Mar 5, 2013 at 4:46 AM
Edited Mar 5, 2013 at 5:03 AM
EDIT : I found the root cause. My ESAuditEvents class had two functions using the same eventID, ie [Event(1, ....)]

I get this error after first attempting to use SemanticLogging-svc.exe (out of proc event listener) for my particular Event Source, stopping it, and then using in-proc event listener.

The runtime error occurs on the second line, consoleListener.EnableEvents(...)
I also get this same exact error when trying to use a FlatFileListener or RollingFlatFileListener.

Code :
var consoleListener = new ConsoleEventListener();
consoleListener.EnableEvents(ESAuditEvents.Log, System.Diagnostics.Tracing.EventLevel.LogAlways, Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Keywords.All);
ESAuditEvents.Log.LogRequest("page_load called");
consoleListener.DisableEvents(ESAuditEvents.Log);
consoleListener.Dispose();
Error : "An item with the same key has already been added."

Stack Trace :
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Diagnostics.Tracing.ManifestBuilder.WriteMessageAttrib(StringBuilder stringBuilder, String elementName, String name, String value)
   at System.Diagnostics.Tracing.ManifestBuilder.StartEvent(String eventName, EventAttribute eventAttribute)
   at System.Diagnostics.Tracing.EventSource.CreateManifestAndDescriptors(Type eventSourceType, String eventSourceDllName, EventSource source)
   at System.Diagnostics.Tracing.EventSource.InsureInitialized()
   at System.Diagnostics.Tracing.EventSource.SendCommand(EventListener listener, EventCommand command, Boolean enable, EventLevel level, EventKeywords matchAnyKeyword, IDictionary`2 commandArguments)
   at System.Diagnostics.Tracing.EventListener.EnableEvents(EventSource eventSource, EventLevel level, EventKeywords matchAnyKeyword, IDictionary`2 arguments)
   at System.Diagnostics.Tracing.EventListener.EnableEvents(EventSource eventSource, EventLevel level, EventKeywords matchAnyKeyword)
   at TestApplication1.Phonebook1.Page_Load(Object sender, EventArgs e) in c:\Users\redacted\Documents\Visual Studio 2012\Projects\TestApplication1\TestApplication1\Phonebook.aspx.cs:line 23
   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
   at System.Web.UI.Control.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Editor
Mar 5, 2013 at 5:06 AM
Edited Mar 5, 2013 at 5:09 AM
I see you found the error. There are plans for tooling (perhaps FX Cop rules) to spot those types of issues before
hitting them at runtime.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Mar 12 at 4:53 AM
I'm hitting same issue. Is there a way to fix it? Un-register something?
Editor
Mar 12 at 5:01 AM
The EventSource performs some magic at runtime (e.g. manifest creation) so if there is an issue with the EventSource definition an exception can be raised at runtime (or events fail to be written).

You can now use the EventSourceAnalyzer class to spot these types of issues. Here's an example from Verifying your EventSource Class in the Developer's Guide:
EventSourceAnalyzer.InspectAll(SemanticLoggingEventSource.Log);
~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to