How to configure EntLib listeners dynamically

Topics: Logging Application Block
Jan 27, 2011 at 4:41 AM
Edited Jan 27, 2011 at 7:06 AM

Hi,

I want to use multiple listeners(based on client choice) like my all low prioriry logs should go into flatfile  and my critical logs should go into Eventlog  etc..

So could you help me to set the listener dynamically from code behind to fullfill above requirment ..

Client wants like we can have multiple listener in Web.config and some key filed to categorize the logs  and source ....

To fullfill above requirment  can we have multiple listener and provide log source dynamically or we will have to create custom listener ..

Please provide your input for same.

like we have code below :

<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="Enterprise Library Logging" formatter="Text Formatter"
        log="" machineName="." traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" />
      <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="c://trace.log" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" />

    </listeners>

 

In above code we have two listener and i want to use both of them depend on log category...

 

Thanks in advance ..

 

Jan 27, 2011 at 6:13 AM

"all low prioriry logs should go into flatfile  and my critical logs should go into Eventlog  etc.."

Are you pertaining to the Severity of the Log entry or not? If yes, I'm just wondering if utilizing the TraceListener's Severity filter property will help. If not, does separating each listener to have each it's own Log Category (so that programmatically you can choose what category your Log Entry should be using upon logging) will suffice? If these options will not work for you, try exploring Entlib 5's Fluent Configuration API which can provide you ways on programmatically configuring your logging setting. HTH.

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