Plunging into .NET Development

Weblog Pieter Gheysens
Microsoft .NET Development - C# - Enterprise Library - Visual Studio 2005 Team System - Compuware DevPartner - ...
 


Tuesday, August 15

Enterprise Library v2.0 : log to database

Last week-end I've been playing with the Logging Application Block of the Enterprise Library v2.0 to setup logging to a database ...

How to setup logging to a database with EntLib v2.0?
  1. Create logging database with tables and stored procedures
    Script can be found in [Install Folder]\Microsoft Enterprise Library January 2006\src\Logging\TraceListeners\Database\Scripts.
  2. Create new configuration file with the Enterprise Library Configuration Tool.
  3. Add Data Access Application Block and set connectionString to logging database.
  4. Add Logging Application Block and add a Database Trace Listener to the Trace Listeners.
  5. Set DatabaseInstanceName and formatter on created Database Trace Listener.
  6. Add reference to the created Database Trace Listener in the general category.
  7. Add references Microsoft.Practices.EnterpriseLibrary.Logging and Microsoft.Practices.EnterpriseLibrary.Logging.Database to your project.
  8. Write some code to log to the database.


            try

            {

                throw new Exception("My Special Exception");

            }

            catch (Exception ex)

            {

                //Logging

                LogEntry logEntry = new LogEntry();

                logEntry.Message = ex.ToString();

                logEntry.Title = ex.Message;

                logEntry.Priority = 1;

                logEntry.Severity = System.Diagnostics.TraceEventType.Error;

 

                Logger.Write(logEntry);

            }



Don't forget the reference in your project to Microsoft.Practices.EnterpriseLibrary.Logging.Database. Otherwise you will end up with the Invalid TraceListenerData type in configuration error.

16 Comments:

  • At 11:00 AM, Anonymous Anonymous said…

    This article is fine to understand, I tried like this but am getting exception like "The value can not be null or string or empty. Parameter name: name". Where is the mistake? Please help me.

     
  • At 11:12 AM, Blogger Pieter said…

    Be sure to set the formatter in the database trace listener. I just noticed that it's not set in the screenshot I published ...

     
  • At 6:47 AM, Blogger Michael Freidgeim said…

    To log exception you can use Exception handling block http://www.devx.com/dotnet/Article/31463

     
  • At 4:50 PM, Anonymous Anonymous said…

    To the first commentor:

    You haven't selected a formatter in the Database Trace Listener. Once you set that it should work just fine.

    Newms

     
  • At 12:25 PM, Anonymous Anonymous said…

    Thank you. no other article mentioned Add reference to the created Database Trace Listener in the general category. its easy to miss out.

     
  • At 2:58 PM, Anonymous Anonymous said…

    Can I use it in order to log into Oracle db?

     
  • At 3:17 PM, Blogger Pieter said…

    I haven't tried it, but it certainly should be possible. That's the whole idea of the Enterprise Library!

     
  • At 1:04 PM, Blogger sa said…

    it is working with Sql server but i cant log into oracle db..plz solve my problems.. i create scripts and tables as said sqlserver...

     
  • At 1:16 PM, Blogger sa said…

    i tried with oracle db but i cant log..i have created scripts and tables for oracle
    plz help me to sovle the problem
    no error..

     
  • At 2:16 PM, Blogger dharmendra said…

    Hi, I am using Enterprise library . I am using Logging Application Blog with DataBaseTraceListener. I have configure connection string, listener etc. But when I tried to Write LogEntry using Logger it gives error as "Message = {The value can not be null or string or empty.
    Parameter name: name} Source = { Microsoft.Practices.EnterpriseLibrary.Common } " I tried this with FlatFileTraceListener, it is working. Also I have given all values to LogEntry object. But it is giving me same error for database.

     
  • At 1:19 AM, Anonymous Anonymous said…

    Thanks for you post and that little comment at the end, i.e. Don't forget the reference in your project to Microsoft.Practices.EnterpriseLibrary.Logging.Database. Otherwise you will end up with the Invalid TraceListenerData type in configuration error.
    You saved me hours of time in tring to figure out that error

     
  • At 12:44 PM, Blogger Jon said…

    What formatter should I use (text, binary or custom)? I've a problem that no log record were inserted.

     
  • At 12:46 PM, Blogger Jon said…

    What formatter should I use (text, binary or custom)? I've a problem of no log record were inserted.

     
  • At 2:58 AM, Blogger Nehal said…

    I just want to use one DB table and my own stored proc to enter information in the table to store exception information. I am not receiving any error but there is no entry in the table also. Any ideas?

     
  • At 10:25 PM, Anonymous Anonymous said…

    Even though there is no error in your application, there could be error in Enterprise Library database logging process. Check Windows Event log (or whatever is your default error listener) for error related to Data logging.

     
  • At 1:17 AM, Blogger Irf said…

    If logging to the database fails, how do you ensure the error gets written to a flatfile?

     

Post a Comment

Links to this post:

Create a Link

<< Home