0

I have just recently added Microsoft.Practices.EnterpriseLibrary.SemanticLogging (v6) with SqlServer silk in my MVC 4 application.

var sqlListener = SqlDatabaseLog.CreateListener("SampleEventLogger",strConnection);
sqlListener.EnableEvents(BasicLogger.Log, EventLevel.LogAlways);

this is the code in my controller

BasicLogger.Log.Critical("ERROR CRITICAL");

It works well!

...and now the impediment :) In my MVC application I use Unity for DI and I inject into constructor ILogger interface. Because I need to use the same reference that I have registered into unity container (otherwise doesn't work) ,so I have thought that taking the RegisteredType could be the solution.

 var myRegistereType = container.Resolve<ILogger>();
 sqlListener.EnableEvents(myRegistereType, EventLevel.LogAlways);

but I received an error

*There is already an instance of EventSource with Guid XXX XXX XXX XXX *

Does anyone have any idea? thanks

MetalMad
  • 416
  • 5
  • 19

1 Answers1

0

There should only be one instance of an EventSource. This is why you will usually see an EventSource exposing a singleton instance (e.g. BasicLogger.Log).

If you are using Unity you will have to register the EventSource as a singleton or use another approach that results in one instance being returned. Here is one way to do it using an InjectionFactory and the Log property:

IUnityContainer container = new UnityContainer();

container.RegisterType<ILogger>(
    new InjectionFactory(c => BasicLogger.Log));

var logger = container.Resolve<ILogger>();
Randy Levy
  • 22,566
  • 4
  • 68
  • 94