Category usage practices?

Topics: Logging Application Block
Jan 26, 2007 at 1:12 AM
I must be misunderstanding something...

I just want to send errors from the "Data Access" category to the Windows Event Log and warnings from the same category to a flat file log. I can't seem to create a config that supports this very common scenario.

Am I misunderstanding how the Logging block was meant to be used?
Jan 26, 2007 at 2:20 PM
The Categories route messages to particular TraceListeners, not the SourceLevels.

The SourceLevels are more of a filtering function within a Category that specify which SourceLevels are logged by the TraceListeners in that category.

Often when you first deploy your applications you log more aggressively to verify the health of your application. In which case, you set the SourceLevels on a category to "ALL" to log all messages.

As you gain more confidence with the application, you only want messages that are Warnings, Errors, etc., so you set the SourceLevel to only allow those types of messages to be logged.

You can check the documentation under "Entering Configuration Information" under the Logging Application Block for additional information, but the above hopefully clarifies the relationship between Categories and SourceLevels.

Regards,

Dave

___________________________

David Hayden
Microsoft MVP C#
Jan 26, 2007 at 5:59 PM
I have read the docs thoroughly and you seem to be right.
Just to clarify:
1) Log entries with a given category can not be sent to different listeners based on their severity or priority.
2) Log entries with a given category can not be sent to more than one listener.

Those seem like major limitations for an enterprise logging system. I have been creating enterprise applications for quite a while and support teams and sys admins have loved this kind of flexibility.

Here are some real examples:
1) Send all log entries from the "Data Access" category to a database listener. Send a copy of all critical errors from the same category to the Windows event log where a sys admin is more likely to see them.
2) Send all log entries (except debug) to the database listener. If we are diagnosing a problem, change the config to start sending all debug to a rolling log file. Everything else should continue going to the database listener.
3) Send all log entries to the database listener. If the sys admin has to leave the building for a while, change the config to send a copy of all critical errors to his pager (a pager listener).

It seems like just changing things so a category name could occur more than once as a category source would add a ton of flexibility. This would also lend credence to the attribute name "switchValue" since it would then be used as the true switch within a category.

For example:

<categorySources>
<add switchValue="Error" category="Data Access">
<listeners>
<add name="Database Listener" />
</listeners>
</add>
<add switchValue="Critical" category="Data Access">
<listeners>
<add name="Event Log Listener" />
</listeners>
</add>
</categorySources>

or even better:

<categorySources>
<add name="Data Access">
<listeners>
<add switchValue="Error" name="Database Listener" />
<add switchValue="Critical" name="Event Log Listener" />
</listeners>
</add>
</categorySources>

Think of the flexibility of this design!
Jan 26, 2007 at 7:25 PM
You can assign multiple TraceListeners to a given category. Just right-click on the category and choose New TraceListener Reference for each TraceListener you want to assign to the category.

One option is to change the name of your category from Data Access to Exception and then log all your exceptions to the Exception Category ( possibly regardless of application layer ). You could also filter the exceptions by priority and/or SourceLevels.

I see what you are saying in your example, but I think you can get what you want to work using the current block by changing the way you classify categories, assigning multiple TraceListeners to categories, and filtering based on priority and SourceLevels.

Regards,

Dave

____________________________

David Hayden
Microsoft MVP C#
Jan 26, 2007 at 8:01 PM
True, I can assign multiple TraceListeners to a given category. But that just means exactly the same events will go to both (since the switchValue is set per categorySource instead of per listener), and that's not what I want to happen.

And true, I can use categories that are basically a copy of the severity, but I shouldn't have to do that.

And true, I can "filter" based on priority and severity, but only for limiting, not for branching.

I appreciate your attention, but I guess I just need to accept the facts. I need to either change the logging block code or put anything I ever want to branch on (to different listeners) as a category.

Thanks David
Feb 11, 2007 at 12:45 PM
Hi, colleagues!
And what about the Event Log's "Category" field. I gess is there any way to assign a value to it?

Thanks
Feb 12, 2007 at 12:17 AM
Unfortunely the logging block doesn't support the Event Log's Category field. While it looks like a simple text field, it's actually a lot more complicated than that. Categories need to be registered separately, and each event log entry can include a numeric ID code that maps to the registered category. Since the logging block doesn't know which categories are registered and which IDs are used for each, we aren't able to levarage this feature. However the LogEntry's categories are included in the body of the logged message.

Tom
Feb 12, 2007 at 8:36 AM
Hi, joellyons!
It seams that you can add to the LogEntry's Categories more than one category and then for each category you can define different SourceLevels and attach different listeners. Will it solve your problem?
May 25, 2007 at 3:13 AM
We want to do the same thing as joellyons. If anyone has implemented a way to add filters to TraceListener references, could you share the details?


joellyons wrote:
True, I can assign multiple TraceListeners to a given category. But that just means exactly the same events will go to both (since the switchValue is set per categorySource instead of per listener), and that's not what I want to happen.

And true, I can use categories that are basically a copy of the severity, but I shouldn't have to do that.

And true, I can "filter" based on priority and severity, but only for limiting, not for branching.

I appreciate your attention, but I guess I just need to accept the facts. I need to either change the logging block code or put anything I ever want to branch on (to different listeners) as a category.

Thanks David

Jun 19, 2007 at 2:42 PM
I find this as a limitation of the Enterprise Logging library:

- for each Category source, I should be able to pick and choose which SourceLevel I want to include or log in a category. Currently, it allows only one SourceLevel. All or any other source level defined. If I have an application logging to Category "Error" and with possible log-entry severity levels: Warning, Critical, Error

I should be able to configure the enterprise logging library to show only Error and Critical or Error and Warning.