Logging XML to SQL2005 database

Topics: Data Access Application Block, Exception Handling Application Block, Logging Application Block
Jun 23, 2008 at 12:16 PM
I am quite new to the Enterprise Library (using 4.0) and have got a little lost.

We are creating a SQL2005 database for logging and my boss wants to use the XML datatype.  I would like to know if is was possible to log exceptions in pure xml so they can be written to the xml datatype field.  I have tried using XmlExceptionFormatter but the result produced is not pure xml as the HandlingInstanceID is inserted at the beginning.

I hope someone can help.

Thanks
Jun 23, 2008 at 2:38 PM
Hi,

The generation of ill-formed XML is a known issue that needs fixing. And since the instance ID is added by the logging exception handler itself, using a new formatter won't help. You will need to use a different logging exception handler, probably mostly cloned from the existing one. You can either create a handler with design-time support like the existing one, which requires creating a couple of assemblies and some extra code, or just create a custom exception handler with a constructor that receives a NameValueCollection with the configuration items; you will not get design-time validation or guidance on what the configuration should be, but it's much simpler. You can see an example of a custom handler in the EHAB quick-start at "QuickStarts\ExceptionHandling\CS\ExceptionHandlingBasicQuickStart\AppMessageExceptionHandler.cs".

Hope this helps,
Fernando



SkelNonch wrote:
I am quite new to the Enterprise Library (using 4.0) and have got a little lost.

We are creating a SQL2005 database for logging and my boss wants to use the XML datatype.  I would like to know if is was possible to log exceptions in pure xml so they can be written to the xml datatype field.  I have tried using XmlExceptionFormatter but the result produced is not pure xml as the HandlingInstanceID is inserted at the beginning.

I hope someone can help.

Thanks



Jun 24, 2008 at 4:00 PM
Thanks for the reply Fernando. 
Ideally I would like to have full design time support.  I need to either extend of modify the logging application block because the default does not suit my needs.  I may have to create a new LogEntry object and I would like to create a custom formatted database trace listener that provides the ability to write xml to a sql2005 database xml datatype.  Unfortunatly I am a little in the dark as to how to provide design time support.  I am also considereing altering the logging exception handler so it does not write the instance Id at all.  Can you point me in the right direction ?

Cheers

fsimonazzi wrote:
Hi,

The generation of ill-formed XML is a known issue that needs fixing. And since the instance ID is added by the logging exception handler itself, using a new formatter won't help. You will need to use a different logging exception handler, probably mostly cloned from the existing one. You can either create a handler with design-time support like the existing one, which requires creating a couple of assemblies and some extra code, or just create a custom exception handler with a constructor that receives a NameValueCollection with the configuration items; you will not get design-time validation or guidance on what the configuration should be, but it's much simpler. You can see an example of a custom handler in the EHAB quick-start at "QuickStarts\ExceptionHandling\CS\ExceptionHandlingBasicQuickStart\AppMessageExceptionHandler.cs".

Hope this helps,
Fernando



SkelNonch wrote:
I am quite new to the Enterprise Library (using 4.0) and have got a little lost.

We are creating a SQL2005 database for logging and my boss wants to use the XML datatype.  I would like to know if is was possible to log exceptions in pure xml so they can be written to the xml datatype field.  I have tried using XmlExceptionFormatter but the result produced is not pure xml as the HandlingInstanceID is inserted at the beginning.

I hope someone can help.

Thanks






Jun 24, 2008 at 5:46 PM
Hi,

There are two old by still great webcasts from Brian Button about how to deal with configuration and design-time support. EntLib v3.x included a software factory that helped build your own providers; it's no longer available out-of-the-box with v4 but you can get it from the entlibcontrib project if you want to give it a try.

The logging exception handler itself follows the plugin model, so you could use it as a guidance. There's a runtime project and a .design project; you will likely have to clone both and make the appropriate changes (namespaces and the actual handler implementation for sure).

Hope this helps,
Fernando



SkelNonch wrote:
Thanks for the reply Fernando. 
Ideally I would like to have full design time support.  I need to either extend of modify the logging application block because the default does not suit my needs.  I may have to create a new LogEntry object and I would like to create a custom formatted database trace listener that provides the ability to write xml to a sql2005 database xml datatype.  Unfortunatly I am a little in the dark as to how to provide design time support.  I am also considereing altering the logging exception handler so it does not write the instance Id at all.  Can you point me in the right direction ?

Cheers

fsimonazzi wrote:
Hi,

The generation of ill-formed XML is a known issue that needs fixing. And since the instance ID is added by the logging exception handler itself, using a new formatter won't help. You will need to use a different logging exception handler, probably mostly cloned from the existing one. You can either create a handler with design-time support like the existing one, which requires creating a couple of assemblies and some extra code, or just create a custom exception handler with a constructor that receives a NameValueCollection with the configuration items; you will not get design-time validation or guidance on what the configuration should be, but it's much simpler. You can see an example of a custom handler in the EHAB quick-start at "QuickStarts\ExceptionHandling\CS\ExceptionHandlingBasicQuickStart\AppMessageExceptionHandler.cs".

Hope this helps,
Fernando



SkelNonch wrote:
I am quite new to the Enterprise Library (using 4.0) and have got a little lost.

We are creating a SQL2005 database for logging and my boss wants to use the XML datatype.  I would like to know if is was possible to log exceptions in pure xml so they can be written to the xml datatype field.  I have tried using XmlExceptionFormatter but the result produced is not pure xml as the HandlingInstanceID is inserted at the beginning.

I hope someone can help.

Thanks









Jun 25, 2008 at 3:30 PM
Thanks very much Fernando :)

I managed to fix the ill-formed xml issue by cloning the application blocks and changing the CreateMessage method in the LoggingExceptionHandler to the following

private string CreateMessage(Exception exception, Guid handlingInstanceID)
        {
            StringWriter writer = null;
            StringBuilder stringBuilder = null;
            try
            {
                writer = CreateStringWriter();
                ExceptionFormatter formatter = CreateFormatter(writer, exception);
                formatter.AdditionalInfo.Add("HandlingInstanceID", handlingInstanceID.ToString());
                formatter.Format();

                stringBuilder = writer.GetStringBuilder();
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }

            return stringBuilder.ToString();
        }


Jan 11, 2009 at 4:56 PM
Hi SkelNonch,
Could you let me know how did you implement Logging XML to SQL2005 database using the Enterprise library.

Could you list out  the steps to implement the same exception handling and logging with Exception as XML.

Jan 12, 2009 at 6:20 AM
Answered in: http://www.codeplex.com/entlib/Thread/View.aspx?ThreadId=43935