How to customize logging format of the exceptions caught using EL 4.1?

Topics: Exception Handling Application Block, Logging Application Block
Jun 3, 2009 at 3:53 AM

Hi,

I'm new to EL.

Our requirement is to log exceptions in DB and EventLog in a specific customized format

ErrorCode

ErrorMessage

ErrorSource

CategoryID

StackTrace

How can this be achieved?

Also, when trying to Log into the DB, how can i find out whether the logging was successful or not? Could it be handled?

 

Jun 3, 2009 at 4:12 AM

To achieve the format you want, you only need to modify your tracelistener's Format.Template property.  There are tokens inserting new line - {newline} and tabs - {tab} there.   For your second question, there's no support for determining if logging succeeded but if there are exceptions during logging, it will be logged depending on what tracelistener you specify under the Logging Errors and Warnings section.

 

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

Jun 3, 2009 at 4:18 AM

Thanks for the reply Sarah.

Regarding the second part, currently i am logging the exception into the AppServer's EventLog if the DB logging failed. But by requirement, if the logging into the Server's eventlog failes due to some network issues, i am supposed to log into the Client machine's eventlog. Any inputs on how this culd be achieved?

Jun 3, 2009 at 4:24 AM

Then adding a FormattedEventLog TraceListener (which points to the client machine's eventlog) under the Logging Errors and Warnings would achieve the behavior you want.  You can also add the same FormattedEventLog TraceListener to the category to which your other formattedeventlog tracelistener belongs to but take note that it will log to both listeners regardless whether the first one fails or not.

 

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

 

Jun 3, 2009 at 4:51 AM

:) Thanks Sarah. Think this would solve my problem for now.

In the template of the trace listeners, we have predefined tokens. Is it possible to have any customized tokens in the template?

For eg: Say i need to show 2 more informations, say UserName and the company name in the template, how can i do this?

Jun 3, 2009 at 4:56 AM

Yes it is. Use the keyvalue token. 

e.g. 

{keyvalue(userId)}

This would look for an item in the .Data collection property of the logentry which has a "userId" key. 

So you should've add an entry like this in your LogEntry.Data property:

logEntry.Data.Add("userId", userId);

 

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

 

Jun 3, 2009 at 5:03 AM
Edited Jun 3, 2009 at 5:11 AM

So... are you saying that if i need a new token say CompanyMailID, i will have to use logEntry.Data.Add("XYZ@ABC.Com", comapnyMailID); before calling the handle exception method?

I tried this now, but i dont see the logEntry.Data.Add method in the LogEntry class. Could you pls help me

Jun 3, 2009 at 5:11 AM

Sorry, that's ExtendedProperties, not Data.

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

 

Jun 3, 2009 at 5:32 AM

I am new to .net

What is this ExtendedProperties? Could you please brief on the steps on how to add a new log entry? 

Jun 3, 2009 at 5:45 AM

The logEntry.Data.Add I posted a while ago is not a method of the LogEntry class.  It is an instance of a LogEntry.  There are a lot of ways to create a LogEntry.  The easiest way is to create it using an empty constructor:

LogEntry logEntry = new LogEntry();

logEntry.ExtendedProperties.Add("userId", 1);

If you explore a bit more, you can see other overloads of this constructor. 

The ExtendedProperties is just simply a collection.  You can put information there which you might want to add aside from those which are already provided in the LogEntry class as properties.  If you look at the template of your tracelistener, there is a token there for the Extended Properties.  Whatever you add in that collection, it will written in that section.

 

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

 

Jun 3, 2009 at 6:03 AM
Edited Jun 3, 2009 at 6:06 AM

"C#"
Exception ex_new;
LogEntry le = new LogEntry();<font size="2">
le.ExtendedProperties.Add("InfyID", "InfyID");
ExceptionPolicy.HandleException(ex, "ClientEventLogExceptionPolicy",out ex_new);
MessageBox.Show(ex_new.Message.ToString());

"Trace listener Template"
Extended Properties: {dictionary({key} - {value})}
Timestamp: {timestamp}
Message: {message}

"Event Log"
Extended Properties:
Timestamp: 6/3/2009 5:57:02 AM
Message: HandlingInstanceID: 2aeac7e6-f041-4c07-9407-9a0dc339cb84
An exception of type 'System.FormatException' occurred and was caught.

 

COULD YOU PLEASE BRIEF ON WHY I SEE THE EXTENDED PROPERTIES SECTION IN THE EVENT LOG BLANK?

 

Jun 3, 2009 at 6:14 AM

"C#"
Exception ex_new;
LogEntry le = new LogEntry();<font size="2">
le.ExtendedProperties.Add("InfyID", "InfyID");
ExceptionPolicy.HandleException(ex, "ClientEventLogExceptionPolicy",out ex_new);
MessageBox.Show(ex_new.Message.ToString());

"Trace listener Template"
Extended Properties: {dictionary({key} - {value})}
Timestamp: {timestamp}
Message: {message}

"Event Log"
Extended Properties:
Timestamp: 6/3/2009 5:57:02 AM
Message: HandlingInstanceID: 2aeac7e6-f041-4c07-9407-9a0dc339cb84
An exception of type 'System.FormatException' occurred and was caught.

 

COULD YOU PLEASE BRIEF ON WHY I SEE THE EXTENDED PROPERTIES SECTION IN THE EVENT LOG BLANK?

 


Jun 3, 2009 at 6:16 AM

My fault.  We should go back again to the .Data property.  This property is in the Exception object.  If you add entries to this property, it will automatically be added to the logentry object created when handling exception using ExceptionPolicy.HandleException.  If you noticed, you created a LogEntry object (le) but didn't really used it.  If you passed it to the Logger.Write method, you'll see the info you put in your logentry.  Anyway, again, I forgot your exact use case for logging which is originating from handling exception. 

Make sure you added a LoggingHandler in your ClientEventLogExceptionPolicy. 

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
valiant.a.dudan@avanade.com

Jun 3, 2009 at 6:49 AM

Thanks a lot for your support.

Your inputs helped me solve the issues i was facing. Thanks again :)