0
votes

I'm using Microsoft Enterprise Library 5.0 for Exception Handling in asp.net. The errors are stored in the Event Viewer of the system. Instead of Event Viewer I need to store these errors in a log File (Text File) using Enterprise Library. How can I implement this?

2

2 Answers

0
votes

I assume that you have a configuration like this for the event log:

        <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            source="Enterprise Library Logging" formatter="Text Formatter"
            log="" machineName="." traceOutputOptions="None" />

Just replace that configuration with something for a flat file. E.g.:

        <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            fileName="trace.log" />

And then change your categories to use the new "Flat File Trace Listener". If you aren't using the latest version then you would need to change the version above from 5.0.505.0 to 5.0.414.0.

Also, you can use the configuration tool EntLibConfig.exe to simplify making these changes without worrying about the XML involved. Or use the config tool to generate the initial XML and then you can manually tweak the XML within your configuration file.

Or, as another alternative, you can use the Fluent Configuration API to configure logging using code instead of configuration.

0
votes

What is your problem with that?

You basically have to an a new sink called File that writes the log into a flat file and configure it appropriately. That's all.

Here is a nice article dedicated to logging to Enterprise Library, albeit a little outdated.

And here are the samples and hands-on labs directly from Microsoft that includes also logging examples.