views:

3725

answers:

3

I m using Microsoft Enterprise Library 3.1 for Exception Handling in asp.net, the erors are stored in Event Viewer of the system . Instead of Event Viewer I need to store these errors in a log File using Enterprise Library. Suggest some methods to implement it. Thanks in advance.

A: 

You need to read about Flat File Trace Listener/Rolling Flat File Trace Listener and Trace Listeners in general.

RichardOD
If u give some examples like, it would be helpful i m confused implementing these listeners i tried Previously.plz
GayaMani
Why don't you paste some of the code you tried, and describe the issue(s) you were encountering? I'm not going to paste something the documentation provides.
RichardOD
+4  A: 

In Entlib a Category is connected to a listener, then a listener determines where the log information is written. To log to a file use a Flat File Listener.

http://msdn.microsoft.com/en-us/library/cc309257.aspx

<listeners>
  <add fileName="..\..\logfiles\BusinessActivity.log" rollSizeKB="10000" timeStampPattern="yyyy-MM-dd" rollFileExistsBehavior="Increment" rollInterval="None" formatter="Text Formatter" header="----------------------------------------" footer="----------------------------------------" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="Rolling Flat File BusinessActivity Listener" />
</listeners>
<formatters>
  <add template="Timestamp: {timestamp(local)}&#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=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="Text Formatter" />
</formatters>
<categorySources>
  <add switchValue="All" name="BusinessActivityLog">
    <listeners>
      <add name="Rolling Flat File BusinessActivity Listener" />
    </listeners>
</categorySources>
Shiraz Bhaiji
+2  A: 

Dear 2:30, You paste the following code inside configuration section at app.config or web.config file

  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="Tracing" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName="AppLog.log" rollSizeKB="1024" timeStampPattern="yyyy-MM-dd" rollFileExistsBehavior="Increment" rollInterval="None" formatter="Text Formatter" header="" footer="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" traceOutputOptions="LogicalOperationStack" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" name="AppLog" />
      <add fileName="Exception.log" rollSizeKB="1024" timeStampPattern="MM-dd-yyyy" rollFileExistsBehavior="Increment" rollInterval="None" formatter="Text Formatter" header="" footer="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" traceOutputOptions="Callstack" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" name="Exception" />
      <add fileName="trace.log" rollSizeKB="1024" timeStampPattern="yyyy-MM-dd" rollFileExistsBehavior="Increment" rollInterval="Month" formatter="Text Formatter" header="----------------------------------------" footer="----------------------------------------" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" name="Trace" />
    </listeners>
    <formatters>
      <add template="{timestamp} : {message}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="AppLog">
        <listeners>
          <add name="AppLog" />
        </listeners>
      </add>
      <add switchValue="Verbose" name="ExceptionHandling">
        <listeners>
          <add name="Exception" />
        </listeners>
      </add>
      <add switchValue="Information" name="Tracing">
        <listeners>
          <add name="Trace" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="Off" name="Logging Errors &amp; Warnings" />
    </specialSources>
  </loggingConfiguration>

Application log can be logged into applog.log file using following statement

Logger.Write("Application Started", "AppLog");

Application exception can be logged into Exception.log file using following statement

Logger.Write("Error: Invalid information you passed", "ExceptionHandling");

Note: You can find the Logger class in Microsoft.Practices.EnterpriseLibrary.Logging;

AppLog.log and Exception.log file will be created in bin folder automatically.

Sarathi1904
Hi Sarathi, Thanks For u post, Its Working fine. I used Rolling Flat File Trace Listeners and it is working... Thanks again
GayaMani