Implementing Log file using Enterprise Library in asp.net

前端 未结 3 846
被撕碎了的回忆
被撕碎了的回忆 2020-12-16 08:37

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

相关标签:
3条回答
  • 2020-12-16 08:58

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

    0 讨论(0)
  • 2020-12-16 09:09

    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.

    0 讨论(0)
  • 2020-12-16 09:14

    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>
    
    0 讨论(0)
提交回复
热议问题