Wednesday, July 28, 2010

Sitecore Logging Part 2. Dealing with the Exceptions

As as follow up to the initial post about SQL based logging in Sitecore, here is another quick tip about how to include exception stack trace into your log database.

It is quite simple actually:
1. Make sure to add the Exception column to your table.
CREATE TABLE [dbo].[Log](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Date] [datetime] NOT NULL,
    [Thread] [varchar](255) NOT NULL,
    [Level] [varchar](20) NOT NULL,
    [Logger] [varchar](255) NOT NULL,
    [Message] [varchar](4000) NOT NULL,
    [Exception] [varchar](2000) NULL

2. Extend the CommandText value to include the Exception parameter:
<param name="CommandText" 
value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception])
       VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
3. Finally, add the following parameter to the log4net configuration. Note that special “Exception” layout is used.
The value of the sizeparameter should match the size of the SQL column on #1.
<param name="Parameter">
   <param name="ParameterName" value="@exception" />
   <dbType value="String" />
   <size value="2000" />
   <layout type="log4net.Layout.ExceptionLayout" />
Now you are all set, and should be able to see exceptions in the database:


Nico Lubbers said...

Great stuff,
One question: currently the exception-field is containing a text that contains 3 parts: "exception", "message" and "source"

Is it possible to change this to be logged in seperate database columns?

Regards, Nico Lubbers