Unity and Logging to Database

Topics: Logging Application Block, Policy Injection Application Block
Aug 31, 2008 at 4:28 PM
Edited Aug 31, 2008 at 7:53 PM
Hi all,

I have problem that likely underscores my incomplete understanding of using the Unity container.

In short, I have setup the Logging Application Block so that it works as I would like it - trace listeners listen, formatters format, and I am able to log to anywhere I wish: text file, Application Event Log, and database. Logging to all three destinations work fine when just calling the Logger.Write() method.

Now, I am trying to make use of the Unity container, so I have created what I think are the appropriate entries in my configuration file, along with the appropriate setup, culminating in BuildUp(). When instantiating the LogWriter class as a dependency like so...
[Dependency]
public LogWriter LogWriter { get; set; }
I am able to write to a text file and to the Application Event Log. However, any attempt to log to the database results in a huge exception (too long to post), with these main messages:
  • [InvalidOperationException: The type Database does not have an accessible constructor.]
  • [BuildFailedException: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Data.Database, SRDSystemConnectionString]) failed: The type Database does not have an accessible constructor. (Strategy type Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy, index 2)]
  • [InvalidOperationException: The parameter database could not be resolved when attempting to call constructor Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener(Microsoft.Practices.EnterpriseLibrary.Data.Database database, System.String writeLogStoredProcName, System.String addCategoryStoredProcName, Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.ILogFormatter formatter).]
  • [BuildFailedException: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Database Destination]) failed: The parameter database could not be resolved when attempting to call constructor Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener(Microsoft.Practices.EnterpriseLibrary.Data.Database database, System.String writeLogStoredProcName, System.String addCategoryStoredProcName, Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.ILogFormatter formatter). (Strategy type Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy, index 2)]
...And so on. My configuration section for Unity:

  <unity>
    <typeAliases>
      <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
    </typeAliases>
    <containers>
      <container>
        <extensions>
          <add type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common" />
          <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.Unity.LoggingBlockExtension, Microsoft.Practices.EnterpriseLibrary.Logging" />
        </extensions>
      </container>
    </containers>
  </unity>

Can anyone point me in the right direction please? Thanks much,

Allen
Sep 1, 2008 at 12:20 PM
Hi Allen,

You need to make sure you add the DataAccessBlockExtension to your container too; you can find more information about creating EntLib objects with Unity at http://msdn.microsoft.com/en-us/library/cc511545.aspx.

Now, there's an unfortunate issue when using Logging with Unity in release mode; this issue will be resolved in the next release, and you can follow the links in the work item to learn about some workarounds.

Hope this helps,
Fernando
Sep 1, 2008 at 9:53 PM
Fernando,

Wow, what a stupid thing to miss on my part! Thanks very much, now all logging appears to be occurring just fine. Regards,

Allen