Logging filter

Topics: Logging Application Block
Jul 1, 2008 at 7:37 PM
Hello guys!

I am trying to do a simple thing:

I want to log all log messages to a Flat file listener, and to log only the warnings to the EventLog.

I've created a filter, but you cant connect between a filter and a trace listener, so there is no way to write all the logs to one listener, and some of them to another one.

This is a common scenario, and i am sure than someone figured it out.

Can someone help me?

Thanks,

Ron
Jul 1, 2008 at 7:57 PM
Hi,

You can use the per-listener filters as described in http://msdn.microsoft.com/en-us/library/cc511699.aspx. These are really the .NET Framework's System.Diagnostics.EventTypeFilter exposed through EntLib's configuration, and are unrelated to the Logging Block's top level filters. Also, they don't work quite as you describe your expected behavior: entries with a severity equal or higher to that specified will be allowed through.

Fernando


rondana wrote:
Hello guys!

I am trying to do a simple thing:

I want to log all log messages to a Flat file listener, and to log only the warnings to the EventLog.

I've created a filter, but you cant connect between a filter and a trace listener, so there is no way to write all the logs to one listener, and some of them to another one.

This is a common scenario, and i am sure than someone figured it out.

Can someone help me?

Thanks,

Ron



Jul 2, 2008 at 8:08 AM
Is there a way to do it on EntLib 3.1? The Filter property of a TraceListner is implemented only on 4.0.
Jul 2, 2008 at 11:31 AM
The closest thing you have in 3.1 are trace-source-level filters, which force your application to include additional information in your log requests. For example, you could have "General" and "General-warnings only" categories with appropriate filters. Of course, this is not as good as the trace-listener-level filters.

Fernando

rondana wrote:
Is there a way to do it on EntLib 3.1? The Filter property of a TraceListner is implemented only on 4.0.



Jul 2, 2008 at 12:59 PM
Thanks dude, but it doesn't work :(

I've created a simple app and added to the "General" category a Formatted EventLog Listener. I've modified the Filter property of the trace listener to "Critical".
Unfortunately, All message are written to the event log! verbose, information, all of them.

When I write a log entry , I set the severity like this:
 LogEntry entry = new LogEntry();
entry.Severity = TraceEventType.Verbose;

I expected that this entry won't be written to the EventLog because of the Filter of the TraceListener, but it is.

What am i doing wrong?

Thanks,

Ron

Jul 2, 2008 at 1:08 PM
I'm confused - are you using 3.1 or 4.0? You said 3.1 originally, but now you mention the Filter property on a trace listener which is available on 4.0...

What does your configuration file look like?

Fernando


rondana wrote:
Thanks dude, but it doesn't work :(

I've created a simple app and added to the "General" category a Formatted EventLog Listener. I've modified the Filter property of the trace listener to "Critical".
Unfortunately, All message are written to the event log! verbose, information, all of them.

When I write a log entry , I set the severity like this:
 LogEntry entry = new LogEntry();
entry.Severity = TraceEventType.Verbose;

I expected that this entry won't be written to the EventLog because of the Filter of the TraceListener, but it is.

What am i doing wrong?

Thanks,

Ron




Jul 2, 2008 at 1:17 PM
I could repro it on v4...

fsimonazzi wrote:
I'm confused - are you using 3.1 or 4.0? You said 3.1 originally, but now you mention the Filter property on a trace listener which is available on 4.0...

What does your configuration file look like?

Fernando


rondana wrote:
Thanks dude, but it doesn't work :(

I've created a simple app and added to the "General" category a Formatted EventLog Listener. I've modified the Filter property of the trace listener to "Critical".
Unfortunately, All message are written to the event log! verbose, information, all of them.

When I write a log entry , I set the severity like this:
 LogEntry entry = new LogEntry();
entry.Severity = TraceEventType.Verbose;

I expected that this entry won't be written to the EventLog because of the Filter of the TraceListener, but it is.

What am i doing wrong?

Thanks,

Ron







Jul 2, 2008 at 2:05 PM
I am using version 4.0,

this is the config file:

 <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add source="Enterprise Library Logging" formatter="Text Formatter"
        log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="Critical" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="TESTAPP" />
    </listeners>
    <formatters>
      <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=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="TESTAPP" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="TESTAPP" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

What do you say?
Jul 2, 2008 at 2:25 PM
It's a problem with the FormattedEventLogTraceListener itself. The easiest thing to do until it's fixed is to use the .NET Framework's own EventLogTraceListener; it wouldn't support a formatter (it will use the default format) or let you write to a remote log but it will respect the filter.

      <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.SystemDiagnosticsTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

        traceOutputOptions="None" filter="Error" type="System.Diagnostics.EventLogTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

        name="System.Diagnostics TraceListener" initializeData="Enterprise Library Logging" />

<!--EndFragment-->
Fernando

rondana wrote:
I am using version 4.0,

this is the config file:

 <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add source="Enterprise Library Logging" formatter="Text Formatter"
        log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="Critical" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="TESTAPP" />
    </listeners>
    <formatters>
      <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=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="TESTAPP" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="TESTAPP" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

What do you say?



Jul 3, 2008 at 8:52 AM
Thanks, it worked!