Message routing based on Category

Topics: Logging Application Block
Jul 24, 2007 at 10:31 PM
Hi,

I have been trying to figure this out for a while. I have LogEntries that are created in my application and sent to a MSMQ where MSMQ distributor's send them to a database.

I want to do something new where based on the EventID value I want to send the message to the event log (if greater than 6000) and a database. So basically if the EventID value is greater than 6000 then

record to event log and database, otherwise just database.

I understand MSMQ distributors don't support routing based on EventID value but do support based on categories, which is just fine since I can control this.


Here is my test application which writes events to a MSMQ with different types of params.

for (int i = 0; i < 5; i++)
{
for (int eventLopper = 0; eventLopper < 5; eventLopper++)
{
string ticks = (DateTime.Now.AddDays((double)i)).Ticks.ToString();
LogEntry ent = new LogEntry();
ent.TimeStamp = DateTime.Now;
ent.Title = ticks;

switch (eventLopper)
{
case 0:
ent.EventId = 4092;
ent.Categories.Add("DataSource");
ent.Severity = System.Diagnostics.TraceEventType.Information;
ent.Message = "case 0";
break;
case 1:
ent.EventId = 4094;
ent.Categories.Add("DataSource");
ent.Severity = System.Diagnostics.TraceEventType.Warning;
ent.Message = "case 1";
break;
case 2:
ent.EventId = 5998;
ent.Categories.Add("DataSource");
ent.Severity = System.Diagnostics.TraceEventType.Error;
ent.Message = "case 2";
break;
case 3:
ent.EventId = 5999;
ent.Categories.Add("Verbose");
ent.Severity = System.Diagnostics.TraceEventType.Verbose;
ent.Message = "case 3";
break;
case 4:
ent.EventId = 6000;
ent.Categories.Add("Critical"); //Critical - this should get recorded to Event Log?
ent.Severity = System.Diagnostics.TraceEventType.Critical;
ent.Message = "case 4";
break;
case 5:
ent.EventId = 6001;
ent.Categories.Add("Critical"); //Critical - this should get recorded to Event Log?
ent.Severity = System.Diagnostics.TraceEventType.Critical;
ent.Message = "case 5";
break;
default:
break;
}

Logger.Write(ent, ent.Categories);
}
}


And here is my config for my distributor (sends all events to a flat file, except for Critical categories which are logged to both original flat file and 2nd critical events only flat file):



<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral,

PublicKeyToken=null" />
<section name="msmqDistributorSettings" type="Microsoft.Practices.EnterpriseLibrary.Logging.MsmqDistributor.Configuration.MsmqDistributorSettings, MsmqDistributor" />
</configSections>
<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
defaultCategory="" logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add fileName="D:\trace.log" header="---------------------------------------"
footer="---------------------------------------" formatter="Text Formatter"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral,

PublicKeyToken=null"
traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral,

PublicKeyToken=null"
name="FlatFile_1" />
<add fileName="D:\Critical_Trace.log" header="----------------------------------------"
footer="----------------------------------------" formatter="Text Formatter"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral,

PublicKeyToken=null"
traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral,

PublicKeyToken=null"
name="FlatFile_2" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.BinaryLogFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Binary Formatter" />
<add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity:

{severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id:

{win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="Critical" name="Critical">
<listeners>
<add name="FlatFile_2" />
<add name="FlatFile_1" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category">
<listeners>
<add name="FlatFile_1" />
</listeners>
</notProcessed>
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="FlatFile_1" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
<msmqDistributorSettings msmqPath="FormatName:DIRECT=TCP:10.90.91.55\private$\johntest" queueTimerInterval="1000" serviceName="MsmqDistributor-johntest" />
</configuration>





Except my 2nd flat file never has any messages in it and my original flat file never gets the critical messages. It is as if the distributor picks out the critical messages but doesnt put them anywhere.


Does my config and test code look valid? And is what I am trying to do reasonable?

much thanks in advance,

- John
Jul 25, 2007 at 4:59 PM
BTW, is it because i have two listeners in the critical category source? Am I allowed to only have one?
Jul 31, 2007 at 4:57 PM
No takers?
Jul 31, 2007 at 7:31 PM
Hi John,

It is allowed to have many trace listener for the same category.

Configuration looks fine. Can you try logging without MSMQ using the same test entries? If it works, you know there's something odd with the configuration of the Msmq trace listener or the distributor itself.

Regards,
Fernando