Reg using EHAB and LAB to log to a custom table

Topics: Building and extending application blocks, Exception Handling Application Block, Logging Application Block
Jun 2, 2009 at 10:09 PM

Hi Support,
I have the following requirement:
When exception is thrown, need to log in the following details to custom database "XYZ" and table "App_Error"
1. User_ID
2. Session_ID
3. Page_Name
4. Error Code
5. Error_desc
6. Exception_Text
7. Severity

Im using Enterprise Library 4.0's Excetion Handling Application Block and Logging Application Block to accomplish the above.
1. I have extended the Customtracelistener
2. I have extended the LogEntry class to include the above mentioned 7 fields of App_Error table as get set property like below:
public string Excepion_Text { get; set; } 
and nothing else apart from these 7 public properties
Now i hav the following question:
1. In the UI layer when i catch the exception, i ll have the following piece of code in UI catch block
catch(Exception ex)
{
 ExceptionPolicy.HandleException(ex, " UI Policy");
}
which should log the exceptions in App_Err table.
Please let me know what exactly have to be written in Write(object o) of my extended CustomTraceListener class?
Please let me know what exactly have to be written in CustomLogEntry class apart from 7 public properties.

Im confused how exactly is the exception object being passed to Logger class.?


I would greatly appreciate if you could help me in getting out of this.
Thanks in advance.

Regards,
Arun

Jun 3, 2009 at 3:24 AM
Edited Jun 3, 2009 at 3:38 AM

"how exactly is the exception object being passed to Logger class.?:

- As part of the exception handling process, the handlers you define in your config gets executed by calling each of their HandleException method.  The LoggingExceptionHandler creates a LogEntry based on this exception and writes this using an instance of a LogWriter.  EntLib takes care of passing an instance of LogWriter during the creation of an LoggingExceptionHandler object.

So based on this, I'd say you also have to create your custom IExceptionHandler in addition to creating a custom trace listener.  You can just pattern it after the LoggingExceptionHandler but instead of creating a LogEntry class out of the exception, create your custom logentry object.

In your custom tracelistener Write(object o) method, you need to add a DbCommand object and add parameters to it based on the object(o) parameter which you can check if it is just a string, an instance of your custom logentry object or a simple LogEntry object.

  

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 3, 2009 at 11:47 PM
Hi,
Im confused here.
Let me tell you what i have done
1. I have a class called DatabasePublisher which extends from IExceptionHandler
 In this class i have the following two methods:
    1.

public Exception HandleException(Exception exception, Guid handlingInstanceId)

{

try

{

this.LogInDatabase(exception);

}

catch (Exception ex)

{

throw ex;

}

}

2.

private

void LogInDatabase(System.Exception exception)

{

CustomLogEntry logEntry = new CustomLogEntry();

logEntry.Activity_Log_ID = 1;

logEntry.PCR_No =

"101";

logEntry.Module_Page_Name =

"Component";

logEntry.Method =

"CreatePCR";

logEntry.Err_Code =

"1009";

logEntry.Err_Desc = exception.Message;

logEntry.Exception_Text = exception.StackTrace;

logEntry.SeverityLevel = System.Diagnostics.

TraceEventType.Critical;

FormatStackTraceMessage(System.

Environment.StackTrace);

try

{

if (Logger.IsLoggingEnabled())

{

Logger.Write(logEntry);

}

}

catch (Exception ex)

{

string str = ex.Message;

}

}

I have the CustomLogEntry entry class which inherits from LogEntry class having the following properties only and no methods:

public

int Activity_Log_ID { get; set; }

public

string PCR_No { get; set; }

public

string Module_Page_Name { get; set; }

public

string Method { get; set; }

public

string Err_Code { get; set; }

public

string Err_Desc { get; set; }

public

string Exception_Text { get; set; }

public

TraceEventType SeverityLevel { get; set; }

Using the Enterprise Library Configuration tool I have done the following:

1. Created a new Exception policy "UI Policy"

2. Added an Exception "DivideByZeroException" under "UI Policy"

3. Added a Logging Handler for DivideByZeroException(Do i need to add CustomHandler instead of Logging Handler to log the exceptions in to my custom database?)

Now please guide me exactly what i have to configure for Logging Application block with the below sections:

1. Category Sources

      Category?

2. Special Sources

     All Events

           What Event?

3. Trace Listeners

       What Trace Listener i have to add here?

4. Formatters

      What formatters i have to add here?

 

In the UI Class i have the following to test it out:

protected void Page_Load(object sender, EventArgs e)

{

try

{

int i = 0;

double d1 = 1 / i;

}

catch (DivideByZeroException ex)

{

ExceptionPolicy.HandleException(ex, CommonConstants.UIEXCEPTIONPOLICY);

}

}

 

I would greatly appreciate if you could help me in this regard.

Thanks & Regards,

Arun

 

On Tue, Jun 2, 2009 at 9:24 PM, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

"how exactly is the exception object being passed to Logger class.?:

- As part of the exception handling process, the handlers you define in your config gets executed by calling each of their HandleException method.  The LoggingExceptionHandler creates a LogEntry based on this exception and writes this using an instance of a LogWriter.  EntLib takes care of passing an instance of LogWriter during the creation of an LoggingExceptionHandler object.

So based on this, I'd say you have to go for creating your custom IExceptionHandler rather than a custom trace listener.  You can just pattern it after the LoggingExceptionHandler but instead of creating a LogEntry class out of the exception, create your custom logentry object. 

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Jun 4, 2009 at 5:29 AM

Hi,

- Do i need to add CustomHandler instead of Logging Handler to log the exceptions in to my custom database?

A: Yes, you would have to add you custom handler instead of the LoggingHandler.

1. Category Sources

      You can add a category which has you CustomTraceListener.

      Ex:

              Category Sources

                   - LogToDatabaseCategory

                                - Here goes your CustomTraceListener

be aware of your category that will be used to log to database. later you might change your call to Logger.Write(o) to Logger.Write(o, "LogToDatabaseCategory"), which takes the category to log to.

2. Special Sources

     All Events

           Please see this : http://www.davidhayden.com/blog/dave/archive/2006/02/18/2805.aspx

3. Trace Listeners

       Add your CustomTraceListener here.

4. Formatters

      I just leave this as default.

 

If you are interested for a sample solution, just post your mailing address or email us privately.

 

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 5, 2009 at 4:54 AM

Hi, I've already sent to you the sample solution.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com