Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semantic Logging: An item with the same key has already been added

Im trying to use new semantic application block for logging. As per MSDN i have test method which inspects the EventSource using

EventSourceAnalyzer.InspectAll(MyEventSource.Log);

But when i run this test i'm getting error

An item with the same key has already been added 

Usually we get this error if we use same event id, but im using different EventID for each method. Below is my event source class

EventSource(Name = "SLAB_1.1.1403.1")]
public class MyEventSource : EventSource
{
    public class Keywords
    {
        public const EventKeywords Page = (EventKeywords)1;
        public const EventKeywords DataBase = (EventKeywords)2;
        public const EventKeywords Diagnostic = (EventKeywords)4;
        public const EventKeywords Perf = (EventKeywords)8;
    }

    public class Tasks
    {
        public const EventTask Page = (EventTask)1;
        public const EventTask DBQuery = (EventTask)2;
    }

    private static MyEventSource _log = new MyEventSource();
    private MyEventSource() { }
    public static MyEventSource Log { get { return _log; } }

    [Event(1, Message = "Application Failure: {0}",
    Level = EventLevel.Critical, Keywords = Keywords.Diagnostic)]
    internal void Failure(string message)
    {
        if (this.IsEnabled(EventLevel.Critical, Keywords.Diagnostic))
        {
            this.WriteEvent(1, message);
        }
    }

    [Event(2, Message = "Application Failure1: {0}",
    Level = EventLevel.Critical, Keywords = Keywords.Diagnostic)]
    internal void Failure(string message, string exception)
    {
        if (this.IsEnabled(EventLevel.Critical, Keywords.Diagnostic))
        {
            this.WriteEvent(2, message, exception);
        }
    }

    [NonEvent]
    internal void Failure(string message, Exception ex)
    {
        if (this.IsEnabled(EventLevel.Critical, Keywords.Diagnostic))
        {
            Failure(message, ex.ToString());
        }
    }
}
like image 393
Laksh Avatar asked Oct 01 '22 03:10

Laksh


1 Answers

The method name is the key, so since you have two methods named Failure, even though you have different parameter lists, the SLAB infrastructure is adding the method name as a key to a dictionary. Rename one of your Failure methods to Failure2 or something like that to fix this problem.

like image 109
randall Avatar answered Oct 12 '22 10:10

randall