Vs.net 2008, Ent Lib 3.1, Logging : The configuration section for logging

Topics: Logging Application Block
May 11, 2008 at 8:01 PM
Hi,

I am trying to use Ent lin 3.1 in Vs.net 2008; When I switch to design view "An unhandled exception has occured. The configuration section for logging cannot be found in the configuration source."  It is annoying that you cant view or edit the usercontrol contents in the desgin view.

Web.config has 

<

 

configuration>
<
configSections>
          <
section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /><section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /><section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /><section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /><section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /><section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

 

... [Removed other ...]

<

 

enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
<
sources><add name="File Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" filePath="EntLib.config" /></sources></enterpriseLibrary.ConfigurationSource>

 

and the Entlib.config file ...
<configuration><configSections><section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /><section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /><section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /><section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /><section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /></configSections>

<

 

loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
<
listeners><add source="Enterprise Library Logging" formatter="Text Formatter" log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="Formatted EventLog TraceListener" /></listeners>

 

<

 

formatters><add template="Timestamp: {timestamp(local)}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="Text Formatter" /></formatters>

 

<

 

logFilters><add enabled="true" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="LogEnabled Filter" /></logFilters>

 

<

 

categorySources>
<
add switchValue="All" name="DataLayer"><listeners><add name="Formatted EventLog TraceListener" /></listeners></add><add switchValue="All" name="General"><listeners><add name="Formatted EventLog TraceListener" /></listeners></add></categorySources>

 

<

 

specialSources><allEvents switchValue="All" name="All Events"><listeners><add name="Formatted EventLog TraceListener" /></listeners></allEvents><notProcessed switchValue="All" name="Unprocessed Category"><listeners><add name="Formatted EventLog TraceListener" /></listeners></notProcessed><errors switchValue="All" name="Logging Errors &amp; Warnings"><listeners><add name="Formatted EventLog TraceListener" /></listeners></errors></specialSources>

 

</

 

loggingConfiguration>

 

<

 

exceptionHandling><exceptionPolicies><add name="DataLayer"><exceptionTypes><add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="NotifyRethrow" name="Exception"><exceptionHandlers><add logCategory="DataLayer" eventId="200" severity="Error" title="Enterprise Library Exception Handling" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" priority="0" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="Logging Handler" /></exceptionHandlers></add></exceptionTypes></add><add name="Exception Policy"><exceptionTypes><add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="NotifyRethrow" name="Exception"><exceptionHandlers><add logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" priority="0" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="Logging Handler" /></exceptionHandlers></add></exceptionTypes></add></exceptionPolicies></exceptionHandling>

<
connectionStrings><add name="MyApp123Dev" connectionString="Data Source=(local);Initial Catalog=MyApp123Dev;database=MyApp123Dev;Integrated Security=True; Connect Timeout=200; pooling='true'; Max Pool Size=200;"providerName="System.Data.SqlClient" /></connectionStrings>

 

<

 

cachingConfiguration defaultCacheManager="Cache Manager"><cacheManagers><add expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000" numberToRemoveWhenScavenging="10" backingStoreName="Isolated Storage" name="Cache Manager" /></cacheManagers><backingStores><add partitionName="MyCachePartition" encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.IsolatedStorageBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="Isolated Storage" /></backingStores></cachingConfiguration></configuration>

Best Regards,

Sreedhar V

May 12, 2008 at 1:56 PM

Hi,

Can you clarify the problem? Just by having entlib's sections in your configuration file wiill not trigger that error; the logging block has to be actually used for that to happen. Since the integrated configuration tool doesn't use the logging block, there must be some custom code in your application that is invoking the logging block at design time. If that's the case, you should use the value in the Control.DesignMode property to avoid this logging at design time.

Hope this helps,
Fernando

May 12, 2008 at 5:31 PM
Edited May 12, 2008 at 9:05 PM

Hi,

Thanks for extending the help ... I am not sure whether I have to do some Initializers ... but here is typical piece of code I use ... What is the Control.DesignMode ???

using

System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Data.Common;
using MyProj.DataLayer.Utils;

 

 

 

....

try
{
         Database db = DatabaseFactory.CreateDatabase(Constants.DBName);
        
DbCommand dbCommand = db.GetStoredProcCommand("SP_Get_Table"); 
         DataTable dt = db.ExecuteDataSet(dbCommand).Tables[0] ??
null;
}
catch (Exception ex)
{
     
    Logger.Write(ex.ToString(), "My Project:: Data Layer ::");
        
bool rethrow = ExceptionPolicy.HandleException(ex, "DataLayer");
        
if (rethrow)
                
throw ex;
}

 

 

 

 

 

....     

Regards,

Sreedhar Vankayala

 

May 13, 2008 at 1:25 PM
I was referring to Control.DesignMode, because you mentioned a web.config file. Component.DesignMode would work if you're working with WinForms.

Your snippet doesn't show where this logging occurs, so I can tall if it's possible it's getting called at design time. But based on your description, it probably is.

Fernando



vansree wrote:

Hi,

Thanks for extending the help ... I am not sure whether I have to do some Initializers ... but here is typical piece of code I use ... What is the Control.DesignMode ???

using

System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Data.Common;
using MyProj.DataLayer.Utils;

 

 

 

 

....

try
{
         Database db = DatabaseFactory.CreateDatabase(Constants.DBName);
        
DbCommand dbCommand = db.GetStoredProcCommand("SP_Get_Table"); 
         DataTable dt = db.ExecuteDataSet(dbCommand).Tables[0] ??
null;
}
catch (Exception ex)
{
     
    Logger.Write(ex.ToString(), "My Project:: Data Layer ::");
        
bool rethrow = ExceptionPolicy.HandleException(ex, "DataLayer");
        
if (rethrow)
                
throw ex;
}

 

 

 

 

 

 

....     

Regards,

Sreedhar Vankayala

 




May 13, 2008 at 6:42 PM

Thanks for the reply again ... I am not doing anthing special. I dont have any initialization or any other code snippet other than the catch block for logging ...

 

catch (Exception ex)
{
    Logger.Write(ex.ToString(), "DataLayer");
    bool rethrow = ExceptionPolicy.HandleException(ex, "DataLayer");
    if (rethrow)
         throw ex;
}

I added the Entlib references and config to the project. Start using with the catch block.
Per your statement, am I missing any Init or code that need to be added for the web project.

- Sreedhar

 

May 13, 2008 at 7:07 PM
Hi,

That code cannot be isolated; it must be contained in a method that is being called for some reason. Try to track down why it could be called, or attach a debugger to the IDE itself and set a breakpoint on Logger.Write and look at the call stack. Once you know why it is being called, you can add a check for DesignMode to avoid the call.

Hope this helps,
Fernando


vansree wrote:

Thanks for the reply again ... I am not doing anthing special. I dont have any initialization or any other code snippet other than the catch block for logging ...

 

catch (Exception ex)
{
    Logger.Write(ex.ToString(), "DataLayer");
    bool rethrow = ExceptionPolicy.HandleException(ex, "DataLayer");
    if (rethrow)
         throw ex;
}

I added the Entlib references and config to the project. Start using with the catch block.
Per your statement, am I missing any Init or code that need to be added for the web project.

- Sreedhar

 

 




May 15, 2008 at 8:43 AM
Hi,

As per your advice, I tried to hook with the debugger ... let me explain one more time

In page, I use  <MyProj:rblSex ID="rblSex" runat="server" RepeatDirection="Horizontal" />

In the Control Library .... [Below I trimmed the spaces to more readibility]

using

 

System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using MyProjUtils = MyProj.DataLayer.Utils;
namespace MyProj.Engine.Tags {

 

[

ToolboxData("<{0}:Sex runat=server></{0}:Sex>")]
public class rblSex : RadioButtonList {
    protected override void OnInit(EventArgs e) {    base.OnInit(e);   this.DataBind();      }
    public override void DataBind() {   PopulateData(); base.DataBind(); }
   
protected override void CreateChildControls() { base.CreateChildControls(); }
   
protected override void Render(HtmlTextWriter output) { base.Render(output); }
    public override string DataTextField  {  get  return "sex_desc";  }  set {  /* Dont set anthing, as the control gets always from db */   } }
    public override string DataValueField{  get  return "sex_id";  }      set {  /* Dont set anthing, as the control gets always from db */   } }

 

 

 

    private

 

void PopulateData() {
           if (this.DataSource == null) {
                     DataTable dt = MyProjUtils.Common.Sex;
                    
this.DataSource = dt;
           }
    }
}
}

In the design mode after attaching the debugger, below is the stack trace ...

  MyProj.datalayer.dll!MyProj.DataLayer.Utils.Common.Sex.get() Line 73 + 0x17 bytes C#
  MyProj.Engine.DLL!MyProj.Engine.Tags.rblSex.PopulateData() Line 36 + 0x5 bytes C#
  MyProj.Engine.DLL!MyProj.Engine.Tags.rblSex.DataBind() Line 28 + 0x7 bytes C#
  MyProj.Engine.DLL!MyProj.Engine.Tags.rblSex.OnInit(System.EventArgs e = {System.EventArgs}) Line 22 + 0xa bytes C#


Question: 
         1) Is my code correct? Also, how to avoid it hitting the db in the design mode?
         2) Interestingly, the Logger. write is failing because the whole code base is in c# project and the configuration for the database connections and logging ... all those are in web project.
                 I look it as design flaw for my app.
                 Web project has Entlib.config and web.config, where the connection strings and logging configuration is available.
                 But the controls are in other project, which in design mode, not sure whether they search for web.config / Entlib.config / or app.config (which is not there)

Thanks

 

 

May 15, 2008 at 4:50 PM

Hi,

Here are your answers:

1. You should hit the database only if (!this.DesignMode). Look for references to this property in System.Web with Reflector and you will find plenty of examples...

2. The configuration belongs in web.config, both for technical and conceptual (since you have a project with reusable controls) reasons. The problem is not that the config file is not in the c# project; even if it was there, it wouldn't be used because at design time code is executed in an AppDomain owned by VS, which uses VS' configuration file. Just for kicks, try attaching a debugger like you did before and when you hit the breakpoint execute AppDomain.CurrentDomain.SetupInformation.ConfigurationFile in the immediate window; you should see the path to VS' configuration file.

Fernando

 

 


vansree wrote:
Hi,

As per your advice, I tried to hook with the debugger ... let me explain one more time

In page, I use  <MyProj:rblSex ID="rblSex" runat="server" RepeatDirection="Horizontal" />

In the Control Library .... [Below I trimmed the spaces to more readibility]

using

 

System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using MyProjUtils = MyProj.DataLayer.Utils;
namespace MyProj.Engine.Tags {

 

[

 

ToolboxData("<{0}:Sex runat=server></{0}:Sex>")]
public class rblSex : RadioButtonList {
    protected override void OnInit(EventArgs e) {    base.OnInit(e);   this.DataBind();      }
    public override void DataBind() {   PopulateData(); base.DataBind(); }
   
protected override void CreateChildControls() { base.CreateChildControls(); }
   
protected override void Render(HtmlTextWriter output) { base.Render(output); }
    public override string DataTextField  {  get  return "sex_desc";  }  set {  /* Dont set anthing, as the control gets always from db */   } }
    public override string DataValueField{  get  return "sex_id";  }      set {  /* Dont set anthing, as the control gets always from db */   } }

 

 

 

    private

 

 

void PopulateData() {
           if (this.DataSource == null) {
                     DataTable dt = MyProjUtils.Common.Sex;
                    
this.DataSource = dt;
           }
    }
}
}

In the design mode after attaching the debugger, below is the stack trace ...

  MyProj.datalayer.dll!MyProj.DataLayer.Utils.Common.Sex.get() Line 73 + 0x17 bytes C#
  MyProj.Engine.DLL!MyProj.Engine.Tags.rblSex.PopulateData() Line 36 + 0x5 bytes C#
  MyProj.Engine.DLL!MyProj.Engine.Tags.rblSex.DataBind() Line 28 + 0x7 bytes C#
  MyProj.Engine.DLL!MyProj.Engine.Tags.rblSex.OnInit(System.EventArgs e = {System.EventArgs}) Line 22 + 0xa bytes C#


Question: 
         1) Is my code correct? Also, how to avoid it hitting the db in the design mode?
         2) Interestingly, the Logger. write is failing because the whole code base is in c# project and the configuration for the database connections and logging ... all those are in web project.
                 I look it as design flaw for my app.
                 Web project has Entlib.config and web.config, where the connection strings and logging configuration is available.
                 But the controls are in other project, which in design mode, not sure whether they search for web.config / Entlib.config / or app.config (which is not there)

Thanks

 

 




May 15, 2008 at 7:52 PM

Hi fsimonazzi,

Great, it finally resolved with your help.

For those, who are interested, the logging error appears because
     - I have controls which fetch data oninit. The data is in DB and I use Integrated auth for my  project
     - It always fails in Vs.net (check for fsimonazzi explanation above)
     - I fxied it using if (! this.DesignMode) ... do databind in the oninit ...

PS: The bug is resolved even though it is partially related to Logging.

Thanks verymuch for fsimonazzi; the bug was killing me for a week ...

Regards,

Sreedhar