How do I programmatically specify loggingConfiguration?

Topics: Logging Application Block
Jan 14, 2011 at 6:52 PM

Trying to use logging application block with SharePoint 2010.  I have web parts in a solution and would like to write to their own log file.  For example, in web.config I put the following:

 <loggingConfiguration name="App1" tracingEnabled="true" defaultCategory="General">
        <listeners>
            <add name="Rolling Flat File Trace Listener"
                 type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 fileName="c:\logs\app1.log" formatter="Text Formatter" rollFileExistsBehavior="Increment"/>
        </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;Severity: {severity}{newline}&#xA;Machine: {localMachine}{newline}" name="Text Formatter"/>
        </formatters>
        <categorySources>
            <add switchValue="All" name="General">
                <listeners>
                    <add name="Rolling Flat File Trace Listener"/>
                </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="Rolling Flat File Trace Listener"/>
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
    <loggingConfiguration name="App2" tracingEnabled="true" defaultCategory="General">
        <listeners>
            <add name="Rolling Flat File Trace Listener"
                 type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 fileName="c:\logs\app2.log" formatter="Text Formatter" rollFileExistsBehavior="Increment"/>
        </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;Severity: {severity}{newline}&#xA;Machine: {localMachine}{newline}" name="Text Formatter"/>
        </formatters>
        <categorySources>
            <add switchValue="All" name="General">
                <listeners>
                    <add name="Rolling Flat File Trace Listener"/>
                </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="Rolling Flat File Trace Listener"/>
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
    

 

how do I specify which loggingConfiguration to use programmatically? 

Jan 14, 2011 at 6:52 PM

Or is there a better way to do this?

Jan 14, 2011 at 8:10 PM

Found the answer here http://www.davidhayden.com/blog/dave/archive/2006/02/18/2805.aspx

Jan 15, 2011 at 12:18 AM

If you're using Entlib 5.0, the fluent configuration interface is much, MUCH easier to use than the approach from Entlib 2.0.