cant log to more than one tracelistener

Topics: Logging Application Block
Aug 11, 2010 at 1:34 PM
Edited Aug 11, 2010 at 2:21 PM

Hi

Im using EntLib V5.0 .

I'm trying to log all activity to the database tracelistener and errors and warnings to the event log, however I cant seem to get both to work - its either one or the other, not both.

I've tried setting both in the default logging settings.

I've tried setting 'All Events' category to the database tracelisterner, the 'Errors and Warnings' to the Eventlog tracelisterner etc, and various similar settings. But, like I say, I cannot get both to work - its one or the other.

 

Ive included my logging section - any help would be appreciated.

Cheers,

 

 

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General"
  revertImpersonation="true">
  <listeners>
   <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    source="Application" formatter="Text Formatter" log="Application"
    machineName="." traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack"
    filter="Warning" />
   <add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    databaseInstanceName="sqlServer" writeLogStoredProcName="WriteLog"
    addCategoryStoredProcName="AddCategory" formatter="Text Formatter"
    traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" />
  </listeners>
  <formatters>
   <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
    name="Text Formatter" />
  </formatters>
  <categorySources>
   <add switchValue="All" name="General">
    <listeners>
     <add name="Database Trace Listener" />
     <add name="Event Log Listener" />
    </listeners>
   </add>
  </categorySources>
  <specialSources>
   <allEvents switchValue="All" name="All Events">
    <listeners>
     <add name="Database Trace Listener" />
    </listeners>
   </allEvents>
   <notProcessed switchValue="Off" name="Unprocessed Category">
    <listeners>
     <add name="Event Log Listener" />
    </listeners>
   </notProcessed>
   <errors switchValue="All" autoFlush="true" name="Logging Errors &amp; Warnings">
    <listeners>
     <add name="Event Log Listener" />
    </listeners>
   </errors>
  </specialSources>
 </loggingConfiguration>
 <dataConfiguration defaultDatabase="sqlServer" />

 

Aug 12, 2010 at 2:19 AM
Edited Aug 12, 2010 at 3:29 AM

Hi,

I believe this is the designed behaviour and only a single sources will be able to use in the specialSources category. As I have also tried this personally.

Also as per the LogWriter class documentation it says the following statement.

"If the "all events" special log source is configured, the log entry will be traced through the log source regardles of other categories that might have matched.

If the "all events" special log source is not configured and the "unprocessed categories" special log source is configured, and the category specified in the logEntry being logged is not defined, then the logEntry will be logged to the "unprocessed categories" special log source.

If both the "all events" and "unprocessed categories" special log sources are not configured and the property LogWarningsWhenNoCategoriesMatch is set to true, then the logEntry is logged to the "logging errors and warnings" special log source."

Hope this clear things out.

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

Aug 12, 2010 at 8:13 AM
Thanks Gino Do you have any recommendations for how I can configure it so that warnings and above go the Event Log, and everything goes to the database tracelistener? Thanks for highlighting the designed behaviour. Regards
Aug 12, 2010 at 9:36 AM

Hmm the only way I can think of is that since you can only use a single source in the specialSources then you can retain the configured trace listener in the Logging Errors and Warning source. Then configure your database trace listener in a different category source. Actually this almost already look like look like  your configuration.

  <categorySources>
   <add switchValue="All" name="General">
    <listeners>
     <add name="Database Trace Listener" />
    </listeners>
   </add>
  </categorySources>
  <specialSources>
   <allEvents switchValue="All" name="All Events">
    <listeners>
    </listeners>
   </allEvents>
   <notProcessed switchValue="Off" name="Unprocessed Category">
    <listeners>
    </listeners>
   </notProcessed>
   <errors switchValue="All" autoFlush="true" name="Logging Errors &amp; Warnings">
    <listeners>
     <add name="Event Log Listener" />
    </listeners>
   </errors>
  </specialSources>

Just make sure that your LogEntry being logged is always utilizing the "General" category.

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

Aug 12, 2010 at 10:29 AM

Ok

Is it not possible to have multiple categorySources, for example, one for tracing, that goes to the db, and one for errors and warnings, that goes to the EventLog - and not even use the specialSources?

I'm surprised that it is not a common usage. I would have thought that in a typical scenario developers would want a flat file or db to monitor application activity, and IT Support use the Event Log to set alarms against application events. 

Aug 12, 2010 at 10:52 AM

Sorry for the confusion. What I meant is that you cannot use all the specialSources at the same time (all events, unprocessed and error and warnings).

It is possible to have multiple categorySources and yes you can actually have the option to not use the specialSources. Though, one best practice is to atleast configure the Error and Warning sources with a trace listener (notice that this is always has a default configured eventlog tracelistener) this is for the purpose to easily debug logging block if failure happens.

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

 

Sep 2, 2010 at 8:56 AM
Edited Sep 2, 2010 at 11:46 AM

 

Sep 2, 2010 at 8:57 AM
Magickode wrote:

Ok

Is it not possible to have multiple categorySources, for example, one for tracing, that goes to the db, and one for errors and warnings, that goes to the EventLog - and not even use the specialSources?

I'm surprised that it is not a common usage. I would have thought that in a typical scenario developers would want a flat file or db to monitor application activity, and IT Support use the Event Log to set alarms against application events. 

 

I have the same situation and the same question. Have you managed to find a solution for this?

Sep 2, 2010 at 11:15 AM

Hi profa,

As mentioned in the earlier post, this is more of a design behaviour. You can only use a single source (all events, unprocessed and error and warnings) in the specialSources category.

Though you are not limited to create multiple category in the Category Sources. Will this not work in your case?

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

Sep 2, 2010 at 12:20 PM

Hi,

no, I can't make it work. When I use multiple categories in categorySources still only the first one seems to be processed. I have the same target as Magickode: to send all the events to one place (Rolling Flat File in my case) and everything that above Warning - to another (XML file). 

I've managed to send events to both destinations but cannot apply the filter on XML listener, when I set filter property to "Warning" it just stops sending to XML file, i.e. it creates it but the content is empty.

I tried to play with logFilter as well - to deny "Critical", "Error", and "Warning" categories in CategoryFilter and setting (in specialSources) notProcessed source's listener to XML. No success. 

 

 

Sep 2, 2010 at 12:34 PM

To make things clear. Here is my client code:

loggerProvider.Write(string.Format("Error by {0}.", "profa"), LogCategory.Error);            //should go to Errors.xml (to Log.log - optional)

loggerProvider.Write(string.Format("Audit by {0}.", "profa"), LogCategory.Audit);            //should go to Log.log

 

Here are two configuration options that I thought should work:

1. filter in listener

<listeners>  

<add name="Rolling Flat File" fileName="C:\Log.log" filter="All" />  

<add name="XML" fileName="C:\Errors.xml" filter="Warning" />

</listeners>


<categorySources> 

  <add switchValue="All" name="General">    

<listeners>      

<add name="Rolling Flat File" />      

<add name="XML" />    

</listeners>  

</add>

</categorySources>

 

2. filter in category source

<listeners>  

  <add name="Rolling Flat File" fileName="C:\Log.log" filter="All" />  

  <add name="XML" fileName="C:\Errors.xml" filter="All" />

</listeners>


<categorySources> 

  <add switchValue="All" name="General">    

  <listeners>      

  <add name="Rolling Flat File" />      

  </listeners>  

</add>

<add switchValue="Warning" name="Errors">    

<listeners>      

<add name="XML" />    

</listeners>  

</add>

</categorySources>

 

None of them worked. 

 

 

Sep 3, 2010 at 6:27 AM

You didn't get any log in the xml file because you restricted your trace trace listener to accept only logs having a severity of Warning (and up which includes Critical and Error).  Your client code didn't specify what is the severity of the message you're logging and in cases like that, it is considered to be informational only (Severity = Information).

To be able to log to it, you should create a LogEntry object having a Severity property equal to either Warning, Critical, or Error. 

Take note also of the category name you specify when logging.  I'm not sure if it's just a typo but your client code shows you're logging to category "Error" and "Audit" but your first config only has a "General" category and the second one includes only "General" and "Errors" category.

 

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

Sep 3, 2010 at 9:32 AM

Thanks a lot! I was stupid enough to confuse between severity and category. I mean I expected filter to process according to category not to severity. I'll try it the right way when I get to work on Sunday.

And regarding the configs - I just omitted everything (seemingly) irrelevant. I have category logFilter down the file which lets through all the categories.

Thanks again.

Sep 20, 2010 at 10:06 AM

It still doesn't work. Here is my client code:

loggerProvider.Write(string.Format("Error by {0}.", "profa"), LogCategory.Error, TraceEventType.Error);            

loggerProvider.Write(string.Format("Audit by {0}.", "profa"), LogCategory.Audit);

My configuration is as earlier (option 2). I get both entries into my rolling flat file (while expecting error going to XML). What am I doing wrong this time?

Sep 20, 2010 at 10:16 AM

Can you post the exact config so as to avoid misunderstanding?  The one which has the Error and Audit category since these are the ones you're using as the categories. 

 

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

Sep 20, 2010 at 10:21 AM

Here it goes. I've just omitted formatters and irrelevant listeners.

 <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true" >
    <listeners>

          
      <add name="Rolling Flat File" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
        fileName="C:\Logs\ApplicationLog.log" footer="**********************************"
        formatter="Text Formatter" header="**********************************"
        rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="20"
        timeStampPattern="yyyy-MM-dd" maxArchivedFiles="6" traceOutputOptions="None"
        filter="All" />
     
      <add name="XML" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.XmlTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
        fileName="C:\Logs\Errors.xml" traceOutputOptions="Callstack"
        filter="All" />
    </listeners>
    
    <categorySources>
      <add switchValue="Warning" name="Errors">
        <listeners>
          <add name="XML" />
        </listeners>
      </add>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Rolling Flat File" />
        </listeners>
      </add>
    </categorySources>
    
    
    <specialSources>
      <allEvents switchValue="All" name="All Events" >
        <listeners>
          <add name="Rolling Flat File" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category" >
        <listeners>
          <add name="Unprocessed Flat File" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Flat File" />
        </listeners>
      </errors>
    </specialSources>
    <logFilters>
      <!-- Comment the categories that you do not want logged below.-->
      <add name="Category"
       type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
       categoryFilterMode="DenyAllExceptAllowed">
        <categoryFilters>
          <add name="Critical"></add>
          <add name="Error"></add>
          <add name="Security"></add>
          <add name="Warning"></add>
          <add name="Audit"></add>
          <add name="UI"></add>
          <add name="Service"></add>
          <add name="Information"></add>
          <add name="Debug"></add>
          <add name="Trace"></add>
          <add name="Unknown"></add>
        </categoryFilters>
      </add>
      <add name="LogEnabled Filter"
           type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
           enabled="true"
           />
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
       minimumPriority="-2" maximumPriority="99" name="Priority Filter" />
     
    </logFilters>
  </loggingConfiguration>
Sep 20, 2010 at 10:41 AM
Edited Sep 20, 2010 at 10:42 AM

The Audit category is missing in the config you posted.  Anyway, the problem is that the Errors category was not included in the categoryFilters.  You specified "Error" instead of "Errors".

 

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

Sep 20, 2010 at 10:59 AM

Thanks a lot!!! Indeed that was the problem. Just what do you mean by "audit category is missing"? You mean I should specify each and every category in categorySources? Then how come everything gets logged into my rolling flat file anyway?

Sep 20, 2010 at 11:02 AM

Nope,  I just meant that int the config you posted, there was no Audit category.  Only the Errors and General categories were included.   No issue on that now that your problem is solved.

 

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

Sep 20, 2010 at 11:11 AM

Ah, ok. I just don't need it, used it as an example of something that is not an error. Thanks again for the help!