0

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?

Alexander Galkin
  • 12,086
  • 12
  • 63
  • 115
Kanvas
  • 165
  • 2
  • 7
  • 23

2 Answers2

0

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.

Alexander Galkin
  • 12,086
  • 12
  • 63
  • 115
  • yes i can implement this using hand- on lab help but all the exception are logging into event viewer. i need to implement this in a flat file . – Kanvas Nov 23 '11 at 08:38
  • i just want to know about configuration part i have already written my code but just want to configure setting so that i can write this into flat file instead of event viewer. – Kanvas Nov 23 '11 at 08:53
0

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.

Randy Levy
  • 22,566
  • 4
  • 68
  • 94