Enterprise Library 6.0: create ExceptionManager by ExceptionPolicyFactory then it throw LogWirte instance null exception

Topics: Exception Handling Application Block
Sep 4, 2013 at 7:22 AM
Edited Sep 4, 2013 at 7:24 AM
Hi All guys,

____The LogWriter has not been set for the Logger static class. Set it invoking the Logger.SetLogWriter method.
__
How I could create the ExceptionManager??
__
I'm sorry to trouble you that I'm haunting by a question. I'm using the Enterprise library 6.0
I had set the Exception block and Log block configuration informations in my app file.
The exceptions will be catched and logged.
so I'm try to initiate the ExceptionManager before I use it.

IConfigurationSource configurationSource=ConfigurationSourceFactory.Create();
ExceptionPolicyFactory exceptionFactory=new ExceptionPolicyFactory(configurationSource);
ExceptionManager manager=exceptionFactory.CreateManager();    

While I first run this code snippet it will throw out a exception that:
The LogWriter has not been set for the Logger static class. Set it invoking the Logger.SetLogWriter method.

I mean I didn't call the Logger yet. I have bind the LogBlock with the Exception Block.
Does the library will save the logged inforamtions automatically?

Below is my configuration:
<exceptionPolicies>
        <add name="DataAccess">
            <exceptionTypes>
                <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                    postHandlingAction="NotifyRethrow">
                    <exceptionHandlers>
                        <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                            logCategory="DataAccess" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                            formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                            priority="0" />
                    </exceptionHandlers>
                </add>
            </exceptionTypes>
        </add>
        <add name="BLL">
            <exceptionTypes>
                <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                    postHandlingAction="ThrowNewException">
                    <exceptionHandlers>
                        <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                            logCategory="BLL" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                            formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                            priority="0" />
                    </exceptionHandlers>
                </add>
            </exceptionTypes>
        </add>
    </exceptionPolicies>


Appreciate for your help.
Sep 4, 2013 at 9:35 AM
Since the Exception Handling Block is using the Logging Application Block (via LoggingExceptionHandler) then the Logger must be explicitly initialized:
IConfigurationSource configurationSource=ConfigurationSourceFactory.Create();

LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create());

ExceptionPolicyFactory exceptionFactory=new ExceptionPolicyFactory(configurationSource);
ExceptionManager manager=exceptionFactory.CreateManager();    

If you aren't sure whether or not your application will use Logging or not (e.g. you may remove use of LoggingExceptionHandler and LoggingSettings) then you could verify the existence of the section first:
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();

if (configurationSource.GetSection(LoggingSettings.SectionName) != null)
{
    LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
    Logger.SetLogWriter(logWriterFactory.Create());
}

ExceptionPolicyFactory exceptionFactory=new ExceptionPolicyFactory(configurationSource);
ExceptionManager manager=exceptionFactory.CreateManager();    

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by randylevy on 10/7/2013 at 11:01 PM
Sep 4, 2013 at 10:22 AM
Edited Sep 4, 2013 at 10:26 AM
Thank you for your reply and It works.
Because the Logger is a global static class so it will be fine to initiate before we create the ExceptionManager.

But I'm still curious why it's designed to load the Logger instance by manual??

I mean now we load the whole configuration first. It may be nicer to load the Logger when we create the ExceptionManager.
The Exception policy has been matched with the loggingconfiguration.

Thank you.