Exception and Event logging to DB

Topics: Exception Handling Application Block, Logging Application Block
Dec 13, 2010 at 3:50 PM

 

Could you please provide custom database tracer to log user activities and error raised in code.

 

Thanks,

Prakash

Dec 14, 2010 at 12:20 AM

I have a sample custom database trace listener.  Send me an email so I could sent it to you.

 

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

Dec 14, 2010 at 1:42 PM

My email id is prakash.sajjanar@gmail.com

 

 

Thanks for your help,

--Prakash Sajjanar

Dec 15, 2010 at 2:24 AM

We have sent you the sample. Hope that helps.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Dec 15, 2010 at 7:37 AM

My requirement is simple - If any error in page, need log exception in DB with around 5 fields(user id, method, severity, error, event)

For event logging - need to log user actions with fields( user id, user action, description, datetime)

For this am using normal database trace listiner. But in this how to get extra fileds like user id , decriptions etc.

Could you please help me out in this.

 

 

Thanks,

Prakash Sajjanar

Dec 15, 2010 at 8:50 AM

Then I believe you're on the right track of using the Custom Database Trace Listener if what you want with the enumerated fields ( user id, user action, description, datetime) is to be a column of your logging database. You can utilize the LogEntry's ExtendedProperties collection property (logEntry.ExtendedProperties.Add("key", "value");) for any additional details you may want to include in your log.

Here are some pointers I hope may help;

  1. Create/Design you logging database, stored procedure and etc..
  2. Create Custom Database TraceListener with reference to your logging database
  3. Configure Exception Handling App Block to use Logging Handler that will also use your Custom Database Trace Listener

So far those are the items I can think of that you'll need. HTH.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

 

 

Dec 15, 2010 at 3:55 PM
Edited Dec 15, 2010 at 4:13 PM

Thanks for information, Works fine for event logging, But In exception handling my concern is here,

try

{

error

}

catch(exception ex)

{

bool rethrow = exceptionpolicy.HandleException(ex,"policy")

if(rethrouw)

throw;

}

How to set my extra fields[db fileds: user id, method name etc..] in catch block.

Could you please help me out.

Thanks.

 

Dec 15, 2010 at 10:01 PM

You can add those fields in the .Data property of the exception.  Those that are in the  .Data property will then be mapped to the .ExtendedProperties of the LogEntry that will be created by the Logging Exception Handler out of the exception.

catch(Exception ex)
{
      ex.Data.Add("userId", userId);
      ex.Data.Add("method", methodName);
      bool rethrow = exceptionPolicy.HandleException(ex, "policy");
      if(rethrow)
           throw; 
}

The sample custom database trace listener we sent contains codes which logs those items from the ExtendedProperties.

 

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

Dec 16, 2010 at 2:33 PM

I am using custom database trace listener whatever you sent us.

1. For event logging works perfect, but for exception handling am unable to catpure the my fileds[user id, method name etc..]

catch(Exception ex)
{
            bool rethrow = exceptionPolicy.HandleException(ex, "policy");
      if(rethrow)
           throw;
}

2. For wcf , am using exception shieldling how to capture my fields[user id ...] logg in to db.

I got very much stuck in exception handling, Could u provide the sample code or suggestions,

Thanks,

Prakash

 

 

Dec 17, 2010 at 12:03 PM

Hi,

Are you logging in the Application level right? If it is, then same approach as mentioned by Sarah from her last response should still be applicable. Though a slight change given that you are now handling a fault contract came from your WCF Exception Shielding. You can mapped from your FaultContract properties then add it in the Data collection property of the exception you'll log: 

            catch (FaultException<CompositeType> ex)
            {
                CompositeType fault = (CompositeType)ex.Detail;

                Exception exceptionToLog = new Exception(fault.ExceptionMessage);
                exceptionToLog.Data.Add("UserId", fault.UserId);
                exceptionToLog.Data.Add("MethodName", fault.MethodName);

                bool rethrow = exceptionToLog.HandleException(ex, "policy");
                if (rethrow)
                    throw; 
            }
Hope this helps.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Dec 19, 2010 at 10:17 AM
Edited Dec 19, 2010 at 10:39 AM

1. What i need to change in my sp to get these values above values[exceptionToLog.Data.Add("UserId", fault.UserId)].

2. Can i use logentry.write(log) in catch block instead of

bool rethrow = exceptionToLog.HandleException(ex, "policy");
                if (rethrow)

Dec 20, 2010 at 2:17 AM

You need to add parameters for the information you expect to be present in the .Data property of the exception and include those parameters in the Insert method.  If you use my custom database trace listener and just replace it with the parameter you expect for each of the item you add in the .Data property, then it should already work. 

On your second question, yes, it will work but why would you want to do it?  Using HandlException already takes care of automatically converting the exception to a LogEntry object.

 

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

Apr 20, 2011 at 6:17 PM

Hi Sarah

Ca we do like this.Instead of adding the the fields to exception data. Can we point it to AuditLogEntry classs that is derived from Logentry that is can we use custom Logentry and use that logentry class to use for exception logging to mysql database.

Apr 24, 2011 at 11:17 AM

Hi,

So to confirm your question, is my understanding correct that you would want to have something like “Logger.Write(CustomLogEntry)” in catch block instead of “exceptionToLog.HandleException(ex, "policy");” ? Is that right?

If yes, then yes you can also use this approach on logging your exception and it may look to be more appropriate in your case since you’re using a custom/extended LogEntry. Though, note that since you will not be using the traditional approach of using the EHAB then you’ll be not benefitted on some capability the EHAB can offer especially having only a specific Exception Type which you only need to handle and do your specific actions, which may require you to do this manually in case you need it.

Hope this helps.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

May 5, 2011 at 7:27 AM

Thanks a lot for your response.

May 10, 2011 at 3:51 AM

Hi Sarah,

            Is there a chance that I can get a copy of this custom database trace listener as well ?

Thanks,

Raheel

May 10, 2011 at 3:57 AM

Hi,

Yes, kindly provide us your email address.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

May 10, 2011 at 4:03 AM
It is raheelchohan@gmail.com. Thank You

On Mon, May 9, 2011 at 10:57 PM, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

Hi,

Yes, kindly provide us your email address.

Noel Angelo Bolasoc
Global Technologies and 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


May 10, 2011 at 4:08 AM

We have sent you the sample project.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

May 10, 2011 at 4:10 AM
thank you very much ..

On Mon, May 9, 2011 at 11:08 PM, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

We have sent you the sample project.

Noel Angelo Bolasoc
Global Technologies and 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