Write to multiple files Enterprise Library Logger

心已入冬 提交于 2019-12-11 05:49:16

问题


Is it possible using Enterprise Library Logger to log to multiple files grouped by date ? For example Trace-08-22-2011.log, Trace-08-23-2011.log.


回答1:


Yes, but not with the TraceListeners that ship with Enterprise-Library. What you need is a custom TraceListener or at least TraceListenerData which is used indirectly as a trace listener factory (GetCreationExpression).

The easiers way to do what you've described is to inherit from RollingFlatFileTraceListenerData and overrice GetCreationExpression. It has this.FileName protected field which can be set to whatever you want. For example you could resolve your custom tokens (timestamp)

    /// <summary>
    /// Returns a lambda expression that represents the creation of the trace listener described by this
    /// configuration object.
    /// </summary>
    /// <returns>A lambda expression to create a trace listener.</returns>
    protected override Expression<Func<TraceListener>> GetCreationExpression()
    {
        // Resolve tokens in FileName
        string fileName = ResolveTokens(this.FileName);

        return
            () =>
                new RollingFlatFileTraceListener(
                    fileName,
                    this.Header,
                    this.Footer,
                    Container.ResolvedIfNotNull<ILogFormatter>(this.Formatter),
                    this.RollSizeKB,
                    this.TimeStampPattern,
                    this.RollFileExistsBehavior,
                    this.RollInterval,
                    this.MaxArchivedFiles);
    }

And your configuration:

<add name="All Activities 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="Sample.CustomFlatFileTraceListenerData, Sample"
fileName="%TEMP%\{timestamp}.log"
footer="" formatter="Detail Text Formatter" rollFileExistsBehavior="Overwrite"
rollInterval="Day" timeStampPattern="yyyy-MM-dd" maxArchivedFiles="10" />



回答2:


<configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <!--Flat File Trace Listener-->

  <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="Default Category" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add source="Enterprise Library Logging" formatter="Text Formatter"
      log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      name="Formatted EventLog TraceListener" />
      <add fileName="D:\Works\GPIH\GPIAgent\Web\Log\Logger.log"
      formatter="One Line Formatter" rollFileExistsBehavior="Increment"
      rollInterval="Midnight" rollSizeKB="10000" timeStampPattern="yyyy-MM-dd"
      listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      name="Rolling Flat File" />
    </listeners>
    <formatters>

      <add template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}"
      type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      name="Text Formatter" />
      <add template="{timestamp(local)} Cat: {category} Pri: {priority} EId: {eventid}  Sev: {severity} {message}   Title:{title}   Machine: {machine}  Application Domain: {appDomain} Process Id: {processId} Process Name: {processName} Win32 Thread Id: {win32ThreadId}    Thread Name: {threadName}   Extended Properties: {dictionary({key} - {value}

)}"
      type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      name="One Line Formatter" />
    </formatters>
    <logFilters>
      <add name="LogEnabled Filter"
      type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      enabled="true" />
    </logFilters>
    <categorySources>
      <add switchValue="Warning" name="Default Category">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Rolling Flat File" />
        </listeners>
      </allEvents>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>


来源:https://stackoverflow.com/questions/7144063/write-to-multiple-files-enterprise-library-logger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!