2

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.

user3631413
  • 101
  • 2
  • 10
  • Where's your connection string? Is anything logged in the Event Log? – Panagiotis Kanavos Jun 25 '14 at 09:53
  • @ Panagiotis Kanavos yeah i am ihaving connection strings in config this part is just part of snippet of my config file and nothing is recorded regarding this in event viewer – user3631413 Jun 25 '14 at 09:55
  • So what have you put into your connection string? Are you trying to connect to the correct database? Did you try debugging your code to see whether an exception is thrown by Logger.Write? Did you check the output window for any debug messages? – Panagiotis Kanavos Jun 25 '14 at 09:57
  • @Panagiotis Kanavos I have tried debugging but it doesnot throw any exception in logger and in connection string i have included some other database rather than (localdb)\v11.0 ,Do,i need to include this for triggering those sql scripts in packages? – user3631413 Jun 25 '14 at 09:59
  • @Panagiotis Kanavos Yeah an exception is logged in for DBExceptions category in eventviewer also but information message is empty. – user3631413 Jun 25 '14 at 10:02

1 Answers1

1

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

user3631413
  • 101
  • 2
  • 10