Enterprise Library Not Logging to Database

Topics: Logging Application Block
Jan 23, 2014 at 4:55 AM
Want to start off by saying I am new to logging using Enterprise Library. I have an application that logs to a SQL Server database. I see some events are being logged, but not all. Specifically, the actual exceptions are not being logged. The application is calling Logger.Write(object, category). Is there a way to debug that will help determine why the line that I call to write the exception is not working, but other Logger.Write() calls are working?
Jan 23, 2014 at 6:48 AM
There could be a variety of issues. One thing to do is ensure there is an errors specialSources defined in the configuration and that it does not point to the Database trace listener.

The errors specialSources tells Enterprise Library where to log any exception information that occur while logging (along with the original LogEntry). This can provide the information about what is going wrong.

If you are using XML configuration it would look something like this:
<specialSources>
    <allEvents switchValue="All" name="All Events" />
    <notProcessed switchValue="All" name="Unprocessed Category" />
    <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
            <add name="Flat File Trace Listener" />
        </listeners>
    </errors>
</specialSources>

The above assumes there is a trace listener called "Flat File Trace Listener" defined. Then in the log file you should see any errors that are occurring (just make sure the process has permissions to write a file).

If you are still stuck then post the configuration and the code demonstrating how the message is logged.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Jan 23, 2014 at 7:02 AM
Edited Jan 23, 2014 at 7:04 AM
The solution is using 3 config files:

dataconfiguration.config:
<?xml version="1.0" encoding="utf-8"?>
<dataConfiguration>
  <xmlSerializerSection type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null">
    <enterpriseLibrary.databaseSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" defaultInstance="TEST" xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data">
      <databaseTypes>
        <databaseType name="Sql Server" type="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase, Microsoft.Practices.EnterpriseLibrary.Data, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null" />
    <databaseType name="Oracle" type="Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase, Microsoft.Practices.EnterpriseLibrary.Data, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null" />           
      </databaseTypes>
      <instances>
        <instance name="LogDB" type="Sql Server" connectionString="LogDB" />
        <instance name="TEST" type="Oracle" connectionString="TEST" />
      </instances>
      <connectionStrings>
        <connectionString name="LogDB">
          <parameters>
            <parameter name="database" value="LoggingQA" isSensitive="false" />
            <parameter name="server" value="test7\dev" isSensitive="false" />
            <parameter name="Password" value="ball" isSensitive="false" />
            <parameter name="User" value="logging" isSensitive="false" />
          </parameters>
        </connectionString>
        <connectionString xsi:type="OracleConnectionStringData" name="TEST">
          <parameters>
            <parameter name="password" value="testpwd" isSensitive="true" />
            <parameter name="user" value="testuser" isSensitive="false" />
            <parameter name="server" value="testserver" isSensitive="false" />
          </parameters>
        </connectionString>
      </connectionStrings>
    </enterpriseLibrary.databaseSettings>
  </xmlSerializerSection>
</dataConfiguration>
loggingconfiguration.config:
<?xml version="1.0" encoding="utf-8"?>
<loggingConfiguration>
  <xmlSerializerSection type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null">
    <enterpriseLibrary.loggingSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" loggingEnabled="true" tracingEnabled="false" categoryFilterMode="AllowAllExceptDenied" distributionStrategy="In Process" minimumPriority="0" name="Client Settings" xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/logging">
      <categoryFilters />
      <distributionStrategies>
        <distributionStrategy xsi:type="InProcDistributionStrategyData" name="In Process" />
      </distributionStrategies>
    </enterpriseLibrary.loggingSettings>
  </xmlSerializerSection>
</loggingConfiguration>
loggingdistributorconfiguration.config
<?xml version="1.0" encoding="utf-8"?>
<loggingDistributorConfiguration>
  <xmlSerializerSection type="Microsoft.Practices.EnterpriseLibrary.Logging.Distributor.Configuration.DistributorSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null">
    <enterpriseLibrary.loggingDistributorSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" defaultCategory="General" defaultFormatter="Text Formatter" xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/loggingdistributor">
      <sinks>
        <sink xsi:type="FlatFileSinkData" name="Flat File" fileName="Auditor.log" header="" footer="" />
      </sinks>
      <categories>
        <category name="General">
          <destinations>
            <destination name="To Flat File" sink="Flat File" format="Text Formatter" />
          </destinations>
        </category>
      </categories>
      <formatters>
        <formatter xsi:type="TextFormatterData" name="Text Formatter">
          <template><![CDATA[Timestamp: {timestamp}  Message: {message}]]></template>
        </formatter>
      </formatters>
    </enterpriseLibrary.loggingDistributorSettings>
  </xmlSerializerSection>
</loggingDistributorConfiguration>
Then I am calling like: Logger.Write(object, "category")

Where would I insert the suggested xml?
Jan 23, 2014 at 8:13 AM
Edited Jan 23, 2014 at 8:13 AM
So it looks like there is only one category defined ("General") and it is only logging to a flat file. You will need to configure a DatabaseSink. For example:
<sink xsi:type="DatabaseSinkData" name="Database Sink" databaseInstanceName="LogDB" storedProcName="WriteLog" /> 
and then add it to the category.

I'm just curious if this an old .NET 1.1 application? The reason I ask is that you are using Enterprise Library 1 which is 10 years old and has been retired so if possible it would be good to move forward to a newer version. If you are sticking with Enterprise Library 1 and want to learn about it you can look at the Enterprise Library 1.0 Hands On Labs.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by obautista on 1/27/2014 at 6:11 AM
Jan 23, 2014 at 2:21 PM
Where does the flat file typically get saved?
Jan 27, 2014 at 6:34 AM
The location of the file is entirely up to you. In the configuration you posted the file is Auditor.log and would be saved in the working directory of the process.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by obautista on 1/27/2014 at 6:12 AM