Unity container 写入多个文件企业库记录器

Unity container 写入多个文件企业库记录器,unity-container,enterprise-library,logging,Unity Container,Enterprise Library,Logging,是否可以使用企业库记录器登录到按日期分组的多个文件?例如Trace-08-22-2011.log、Trace-08-23-2011.log。是,但不适用于企业库附带的TraceListener。您需要的是一个自定义TraceListener或至少是TraceListenerData,它被间接用作跟踪侦听器工厂(GetCreationExpression) 要完成您所描述的任务,最简单的方法是继承RollingFlatFileTraceListenerData并覆盖GetCreationExpre

是否可以使用企业库记录器登录到按日期分组的多个文件?例如Trace-08-22-2011.log、Trace-08-23-2011.log。

是,但不适用于企业库附带的TraceListener。您需要的是一个自定义TraceListener或至少是TraceListenerData,它被间接用作跟踪侦听器工厂(GetCreationExpression)

要完成您所描述的任务,最简单的方法是继承RollingFlatFileTraceListenerData并覆盖GetCreationExpression。它有这个.FileName受保护的字段,可以设置为您想要的任何值。例如,您可以解析自定义标记(时间戳)

//
///返回一个lambda表达式,该表达式表示此
///配置对象。
/// 
///用于创建跟踪侦听器的lambda表达式。
受保护的重写表达式GetCreationExpression()
{
//解析文件名中的标记
字符串文件名=解析令牌(this.fileName);
返回
() =>
新的RollingFlatFileTraceListener(
文件名,
这个,头,,
这个。页脚,
Container.ResolvedIfNotNull(this.Formatter),
这个,RollSizeKB,
这种模式,,
this.RollFileExistsBehavior,
这个时间间隔,
此文件为.maxarcivedviles);
}
以及您的配置:

<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" />


<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" />
<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>