user3631413
user3631413

Reputation: 101

Unable to log in database using enterprise library

i have been trying to log exceptions in event viewer and database using enterprise library,For event viewer i have been successfully logging exception but i am unable to log exceptions in database.Major problem is that i am not getting any error either I have set the config files using enterprise console and have left all the fields as default My web config section looks like this

 <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
  <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    source="Enterprise Library Logging" formatter="Text Formatter"
    log="SampleLog" machineName="." traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack"
    filter="All" />
  <add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    databaseInstanceName="Sample ADO Connection String" writeLogStoredProcName="WriteLog"
    addCategoryStoredProcName="AddCategory" formatter="Text Formatter"
    traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" />
  </listeners>
    <formatters>
  <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.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" autoFlush="false" name="General">
    <listeners>
      <add name="Event Log Listener" />
      <add name="Database Trace Listener" />
    </listeners>
  </add>
  <add switchValue="All" name="EventLogSample">
    <listeners>
      <add name="Event Log Listener" />
    </listeners>
  </add>
  <add switchValue="All" name="DBExceptions">
    <listeners>
      <add name="Database Trace Listener" />
    </listeners>
  </add>
</categorySources>
<specialSources>
  <allEvents switchValue="All" name="All Events">
    <listeners>
      <add name="Database Trace Listener" />
      <add name="Event Log Listener" />
    </listeners>
  </allEvents>
  <notProcessed switchValue="All" name="Unprocessed Category">
    <listeners>
      <add name="Database Trace Listener" />
    </listeners>
  </notProcessed>
  <errors switchValue="All" autoFlush="false" name="Logging Errors &amp; Warnings">
    <listeners>
      <add name="Database Trace Listener" />
      <add name="Event Log Listener" />
    </listeners>
  </errors>
</specialSources>

and i am setting logger in C# code as

DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
Logger.SetLogWriter(new LogWriterFactory().Create());

and then logging exception as

LogEntry logEntry = new LogEntry();
logEntry.EventId = 100;
logEntry.Priority = 2;
logEntry.Message = "Informational message";
logEntry.Categories.Add("DBExceptions");
Logger.Write(logEntry);

And the default script in enterprise lib is

sqlcmd -S (localdb)\v11.0 -E -i CreateLoggingDatabase.sql
sqlcmd -S (localdb)\v11.0 -E -i CreateLoggingDatabaseObjects.sql -d Logging

This is the cmd file after running this error is being logged in event viewer but nothing is logged in database I tried checking the (localdb)\v11.0 database,I think these default scripts i.e. CreateLoggingDatabaseObjects.sql and CreateLoggingDatabase.sql are not being triggered.Kindly help me in this,i tried many approaches but nothing works and it is loggging perfectly in eventviewer.

Upvotes: 2

Views: 4116

Answers (1)

user3631413
user3631413

Reputation: 101

After,quiet alot surfing finally got the solution Firstly,you have to create all the structure of logging database (localdb)\v11.0 installed by enterprise library to your database i.e. each and every table and stored procedures. Otherwise you can acheive it by executing sql scripts in enterpriselibrary.logging.database folder in packages folder. Once you are done with this you are ready to go with logging exception in DB. Note;You need to add these scripts to the database you want the exceptions to be logged in

You,can also configure your listeners using Ent Lib console. regeretfully There,is no helpful blog for entlib i will go ahead with that soon and share the steps. Hopes,it helps others

Upvotes: 1

Related Questions