logging application block, not storing the log in the database

Topics: Logging Application Block
Mar 22, 2009 at 8:27 AM
I am using Enterprise library version 3.1. I am trying to write the logs in to database, and it is not working. It didn't give me any error, but logs are not there in the database. I have created the database from the .cmd file but nothing seems to be working.

My web.config file is as follows.

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add databaseInstanceName="LocalSqlServer" writeLogStoredProcName="WriteLog"
        addCategoryStoredProcName="AddCategory" formatter="Text Formatter"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        name="Database Trace Listener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#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>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

Please help
Mar 22, 2009 at 10:21 AM

Hi,

Would you mind posting your full config file including the dataconfiguration section, i suspect that it may have cause by improper connection string.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 22, 2009 at 10:56 AM
please find the copy of web.config..

<?xml version="1.0"?>
<configuration>
  <configSections>
    <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="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false" />
    <section name="securityCryptographyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration.CryptographySettings, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add databaseInstanceName="PGConnection" writeLogStoredProcName="WriteLog"
        addCategoryStoredProcName="AddCategory" formatter="Text Formatter"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        name="Database Trace Listener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#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>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <dataConfiguration defaultDatabase="PGConnection" />
  <connectionStrings>
    <add name="PGConnection" connectionString="Data Source=sreeju;Initial Catalog=PaymentGatewayDB;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <securityCryptographyConfiguration>
    <hashProviders>
      <add algorithmType="System.Security.Cryptography.SHA1Managed, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        saltEnabled="true" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.HashAlgorithmProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        name="AlnadeemDefault" />
    </hashProviders>
  </securityCryptographyConfiguration>
 
  <system.web>
    <!--
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
        -->
    <compilation debug="true" />
    <!--
            The <authentication> section enables configuration
            of the security authentication mode used by
            ASP.NET to identify an incoming user.
        -->
    <authentication mode="Windows" />
    <!--
            The <customErrors> section enables configuration
            of what to do if/when an unhandled error occurs
            during the execution of a request. Specifically,
            it enables developers to configure html error pages
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    <customErrors mode="Off" />
  </system.web>
</configuration>
Mar 22, 2009 at 7:31 PM
Things to check:
  • The account running (Network Service?) is able to connect to the database
  • The account running is able to execute stored procedures AddCategory and WriteLog - verify the tables and stored procedure were not created in alternate schema if using SQL 2005.
Mar 23, 2009 at 4:50 AM
The account running (Network Service?) is able to connect to the database- Since this is a web application and I am able to write data to the database using the same connection string.
Stored procedures are created already... but unfortunately it is not working
Mar 23, 2009 at 5:47 AM
Hi,

What do you mean by "it is not working?" is the stored procedure not working? Anyway, as i've noticed in your config, the Database instance of your database trace listener is "PGConnection" which points to a database "PaymentGatewayDB", I expect that if you run the "CreateLoggingDb.cmd" of the Logging App Block, it will create a database named "Logging" by default on a SQLEXPRESS instance, not unless you run the .sql file on your sql server. Would you mind checking if your config has a connection string that's for the Logging?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 23, 2009 at 6:55 AM
What i mean "not working" is I have the following code, that doesn't throw any error but unfortunately no data is writing in the DB. After i have created the "logging" database from .cmd file, i have exported the database to my db.
Mar 23, 2009 at 6:58 AM
So that means, that all the tables and stored procedure that are created by the .cmd are now in your database "PaymentGatewayDB", right?
Mar 24, 2009 at 8:47 AM
Yes, everything exists... 3 tables and 4 sps are there...
Mar 24, 2009 at 9:12 AM

Can you post your code here on how are you performing the logging?  Also, try running the sql profiler, see if the WriteLog sp is actually being called.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 26, 2009 at 5:16 AM
Thank you for your support.

The code for logging is as follows
LogEntry logEntry = new LogEntry();
 logEntry.Severity = System.Diagnostics.TraceEventType.Information;
 logEntry.ProcessName = "Section XYZ from your project";
logEntry.Title = "Title";
logEntry.Categories.Add("General");
logEntry.Message = string.Format("Your Message");
logEntry.TimeStamp = DateTime.Now;
Logger.Write(logEntry);

I made a trace with sql server profiler and got the below result when calling the stored procedure writelog

RPC:Completed    declare @p14 int
set @p14=NULL
exec WriteLog @eventID=6352,@priority=-1,@severity=N'Error',@title=N'',@timestamp=''2009-03-26 05:10:20:203'',@machineName=N'SREEJU',@AppDomainName=N'3de20650-74-128825160150751052',@ProcessID=N'5048',@ProcessName=N'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.exe',@ThreadName=NULL,@Win32ThreadId=N'7276',@message=N'Tracing to LogSource ''General'' failed. Processing for other sources will continue. See summary information below for more information. Should this problem persist, stop the service and check the configuration file(s) for possible error(s) in the configuration of the categories and sinks.


Summary for Enterprise Library Distributor Service:
======================================
-->
Message:
Timestamp: 3/26/2009 8:10:19 AM
Message: Your Message
Category: General
Priority: -1
EventId: 0
Severity: Information
Title:Title
Machine: SREEJU
App Domain: 3de20650-74-128825160150751052
ProcessId: 5048
Process Name: Section XYZ from your project
Thread Name:
Win32 ThreadId:7276
Extended Properties:
--> MachineName: SREEJU
--> TimeStamp: 3/26/2009 5:10:20 AM
--> FullName: Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
--> AppDomainName: 3de20650-74-128825160150751052
--> WindowsIdentity: NIT\sreeju

Exception Information Details:
======================================
Exception Type: System.Data.SqlClient.SqlException
Errors: System.Data.SqlClient.SqlErrorCollection
Class: 16
LineNumber: 26
Number: 515
Procedure: WriteLog
Server: sreeju
State: 2
Source: .Net SqlClient Data Provider
ErrorCode: -2146232060
Message: Cannot insert the value NULL into column ''LogID'', table ''PaymentGatewayDB.dbo.Log''; column does not allow nulls. INSERT fails.
The statement has been terminated.
The ''WriteLog'' procedure atte',@formattedmessage=N'Timestamp: 3/26/2009 5:10:20 AM
Message: Tracing to LogSource ''General'' failed. Processing for other sources will continue. See summary information below for more information. Should this problem persist, stop the service and check the configuration file(s) for possible error(s) in the configuration of the categories and sinks.


Summary for Enterprise Library Distributor Service:
======================================
-->
Message:
Timestamp: 3/26/2009 8:10:19 AM
Message: Your Message
Category: General
Priority: -1
EventId: 0
Severity: Information
Title:Title
Machine: SREEJU
App Domain: 3de20650-74-128825160150751052
ProcessId: 5048
Process Name: Section XYZ from your project
Thread Name:
Win32 ThreadId:7276
Extended Properties:
--> MachineName: SREEJU
--> TimeStamp: 3/26/2009 5:10:20 AM
--> FullName: Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
--> AppDomainName: 3de20650-74-128825160150751052
--> WindowsIdentity: NIT\sreeju

Exception Information Details:
======================================
Exception Type: System.Data.SqlClient.SqlException
Errors: System.Data.SqlClient.SqlErrorCollection
Class: 16
LineNumber: 26
Number: 515
Procedure: WriteLog
Server: sreeju
State: 2
Source: .Net SqlClient Data Provider
ErrorCode: -2146232060
Message: Cannot insert the value NULL into column ''LogID'', table ''PaymentGatewayDB.dbo.Log''; column does not allow nulls. INSERT fails.
The statement has been terminated.
The ''WriteLog'' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.
Data: System.Collections.ListDictionaryInternal
TargetSite: Void OnError(System.Data.SqlClient.SqlException, Boolean)
HelpLink: NULL

StackTrace Information Details:
======================================
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Microsoft.Practices.EnterpriseLibrary.Data.Database.DoExecuteNonQuery(DbCommand command)
   at Microsoft.Practices.EnterpriseLibrary.Data.Database.ExecuteNonQuery(DbCommand command, DbTransaction transaction)
   at Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener.ExecuteWriteLogStoredProcedure(LogEntry logEntry, Database db, DbTransaction transaction)
   at Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener.ExecuteStoredProcedure(LogEntry logEntry)
   at Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener.TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, Object data)
   at Microsoft.Practices.EnterpriseLibrary.Logging.LogSource.TraceData(TraceEventType eventType, Int32 id, LogEntry logEntry, TraceListenerFilter traceListenerFilter)
   at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter.ProcessLog(LogEntry log)
Category:
Priority: -1
EventId: 6352
Severity: Error
Title:
Machine: SREEJU
Application Domain: 3de20650-74-128825160150751052
Process Id: 5048
Process Name: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.exe
Win32 Thread Id: 7276
Thread Name:
Extended Properties: ',@LogId=@p14 output
select @p14    .Net SqlClient Data Provider    sreeju    NIT\sreeju    0    0    0    0    5048    57    2009-03-26 08:10:20.200    2009-03-26 08:10:20.200    0X000000000F0000001000570072006900740065004C006F0067002600000003000600381069006E007400100040006500760065006E00740049004400D01800002800000003000600381069006E007400120040007000720069006F007200690074007900FFFFFFFF4400000082001800E7306E007600610072006300680061007200280033003200290012004000730065007600650072006900740079000A0000004500720072006F0072003600000082001A00E7306E007600610072006300680061007200280032003500360029000C0040007400690074006C0065000000000040000000870010003D106400610074006500740069006D00650014004000   

Mar 26, 2009 at 5:27 AM
Thank you very much for your support. The problem lies on the table structure. it dosn't create identity or primary key during export process. Now I generate the script and ran in my database, which perfectly create the log. Thank you very much, the issue is resolved now
Mar 26, 2009 at 5:34 AM

Yeah, it might have been modified.  Glad it's working now.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com