Creating LogWriter with Unity - problem if run withouth debugging

Topics: Logging Application Block
Jul 22, 2008 at 8:07 AM

Hello,

I have the following code (I've made only two classes, in order to simplify the case):

class
Program
{
    static void Main(string[] args)
    {
        LogWrapper log = new LogWrapper();
    }
}

public class LogWrapper
{
    
private LogWriter _logWriter;

    
public LogWrapper()
    {
        
IUnityContainer container = new UnityContainer()
            .AddNewExtension<
EnterpriseLibraryCoreExtension>()
            .AddNewExtension<
LoggingBlockExtension>();
        _logWriter = container.Resolve<
LogWriter>();
    
}
}


If I build and run this code with debugging (F5), then everything works fine. If I run it without debugging (Ctrl+F5) then on the container.Resolve invokation I get the following Exception:

Resolution of the dependency failed, type = "LogWriter", name = "". Exception message is: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: The parameter traceSources could not be resolved when attempting to call constructor Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter(System.Collections.Generic.ICollection`1[[Microsoft.Practices.EnterpriseLibrary.Logging.Filters.ILogFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] filters, System.Collections.Generic.ICollection`1[[Microsoft.Practices.EnterpriseLibrary.Logging.LogSource, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] traceSources, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource allEventsTraceSource, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource notProcessedTraceSource, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource errorsTraceSource, System.String defaultCategory, System.Boolean tracingEnabled, System.Boolean logWarningsWhenNoCategoriesMatch). (Strategy type Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy, index 2)

Let's add to the constructor of LogWrapper a couple of lines which create a LogEntry:

    public LogWrapper()
    {
        IUnityContainer container = new UnityContainer()
            .AddNewExtension<EnterpriseLibraryCoreExtension>()
            .AddNewExtension<LoggingBlockExtension>();
        _logWriter = container.Resolve<LogWriter>();

        LogEntry logEntry = new LogEntry();
        logEntry.Message =
"Hello World";
        _logWriter.Write(logEntry);
    
}

And now, if I run it without debugging - it works fine!

It seems that the LogSource (this is a type of elements of the collection traceSources, which is the parameter that cannot be resolved) is in the same assembly as LogEntry. And there is a problem with loading this assembly when the LogEntry lines are not there.

Is it a bug or I am doing something wrong?

Jul 22, 2008 at 12:12 PM
Hi,

You seem to have hit the issue described here http://www.codeplex.com/entlib/Thread/View.aspx?ThreadId=29005. There's a workaround but it requires updating the code and rebuilding the logging block.

Fernando
Jul 24, 2008 at 11:24 AM
Oh, yes. That's true. It's working after applying this workaround. Thanks.