Entlib 6.0 Custom Configuration Source is not working

Topics: Logging Application Block
Jul 23, 2013 at 6:39 PM
Please see below issue detail-

I downloaded Entlib 6.0 from codeplex.com and created custom configuration source as Entlib.config and referred as a source in App.Config. And called the C# code to write logging. I got the below error message.

Error Message: "The configuration section for Logging cannot be found in the configuration source."

I. App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<enterpriseLibrary.ConfigurationSource selectedSource="fileSource"
parentSource="fileSource">
<sources>
  <add name="fileSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    filePath="C:\App_Data\Entlib.config" />
</sources>
<redirectSections>
  <add sourceName="fileSource" name="loggingConfiguration" />
</redirectSections>
</enterpriseLibrary.ConfigurationSource>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

II. Entlib.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
  <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    source="Enterprise Library Logging" formatter="Text Formatter"
    log="Client" machineName="." traceOutputOptions="None" />
  <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    fileName="rolling.log" formatter="Text Formatter"
    rollInterval="Day" />
</listeners>
<formatters>
  <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
    name="Text Formatter" />
</formatters>
<categorySources>
  <add switchValue="All" name="General">
    <listeners>
      <add name="Event Log Listener" />
    </listeners>
  </add>
</categorySources>
<specialSources>
  <allEvents switchValue="All" name="All Events">
    <listeners>
      <add name="Event Log Listener" />
      <add name="Rolling Flat File Trace Listener" />
    </listeners>
  </allEvents>
  <notProcessed switchValue="All" name="Unprocessed Category" />
  <errors switchValue="All" name="Logging Errors &amp; Warnings">
    <listeners>
      <add name="Event Log Listener" />
    </listeners>
  </errors>
</specialSources>
</loggingConfiguration>
</configuration>

III. C# code on button event.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnLogging_Click(object sender, EventArgs e)
    {
        WriteLogEvent("Important", 1, "btnLogging_Click",TraceEventType.Information, 23);
    }

    public static string WriteLogEvent(string Category, int EventID, string Message, TraceEventType Severity, int Priority)
    {
        LogEntry LogEntry = null;
        string StatusMessage = string.Empty;
        try
        {
            IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
            LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
            LogEntry = new LogEntry();
            LogEntry.Categories.Add(Category);
            LogEntry.EventId = EventID;
            LogEntry.Message = Message;
            LogEntry.TimeStamp = DateTime.Now;
            LogEntry.Severity = Severity;
            LogEntry.Priority = Priority;
            LogEntry.GetProcessName();
            LogEntry.Title = "SmartClient Test Log";


           Logger.SetLogWriter(new LogWriterFactory().Create());

                Logger.Write(LogEntry);
                StatusMessage = "Successful";


        }
        catch (Exception ex)
        {

        }
        return null;
    }
}
Could you please provide help to resolve the issue.
Jul 23, 2013 at 7:16 PM
You need to use the LogWriterFactory that you initialized with the Enterprise Library configuration information and not a new default instance:
            IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
            LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
            Logger.SetLogWriter(logWriterFactory.Create()); // not new LogWriterFactory().Create();

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by randylevy on 10/7/2013 at 10:39 PM
Jul 23, 2013 at 7:47 PM
Edited Jul 24, 2013 at 12:18 PM
Randy,

Thank you so much and following your code change is perfect solution.
        IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
        LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
        Logger.SetLogWriter(logWriterFactory.Create()); // not new LogWriterFactory().Create();
Thanks
Oct 5, 2013 at 2:09 PM
hi,

I did the chage to config file as below
     IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
    LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
    Logger.SetLogWriter(logWriterFactory.Create()); // not new LogWriterFactory().Create();

but still i'm facing same problem "The configuration section for Logging cannot be found in the configuration source." can any one help me to solve this prob with nice example.
please



thanks,
sampath
Oct 5, 2013 at 3:30 PM
@sampath13, I would recommend posting a new discussion thread with the specifics of your issue (@atpstblue at the top of this thread had a very complete description of their situation).

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Oct 5, 2013 at 3:46 PM
Ok sure.
I've posted a new discussion with How to log NHibernate SQL Statements using Microsoft Enterprise Library?
item number is 33783.


please help me ASAP.

Thanks,
sampath