2011-06-30 94 views
1

我正在使用Fluent API來處理使用EntLib進行日誌記錄的各種配置選項。企業庫Fluent API和滾動日誌文件不滾動

我在代碼中手動構建loggingConfiguration部分。它看起來很好,除了RollingFlatFileTraceListener實際上沒有滾動文件。它將遵守大小限制並適當限制寫入文件的數據量,但實際上並不會創建新文件並繼續日誌。

我已經用示例應用程序和app.config測試過它,它似乎工作。所以我猜我缺少一些東西,雖然每個配置選項似乎都需要它。

這裏是代碼的基本操作(帶硬編碼值表明,似乎沒有可正常工作的配置): //創建用於流利API VAR configBuilder的配置建設者=新ConfigurationSourceBuilder() ;

  //Start building the logging config section     
      var logginConfigurationSection = new LoggingSettings("loggingConfiguration", true, "General");     
      logginConfigurationSection.RevertImpersonation = false; 
      var _rollingFileListener = new RollingFlatFileTraceListenerData("Rolling Flat File Trace Listener", "C:\\tracelog.log", "----------------------", "", 
          10, "MM/dd/yyyy", RollFileExistsBehavior.Increment, 
          RollInterval.Day, TraceOptions.None, 
          "Text Formatter", SourceLevels.All); 


      _rollingFileListener.MaxArchivedFiles = 2; 

      //Add trace listener to current config 
      logginConfigurationSection.TraceListeners.Add(_rollingFileListener); 

      //Configure the category source section of config for flat file 
      var _rollingFileCategorySource = new TraceSourceData("General", SourceLevels.All); 

      //Must be named exactly the same as the flat file trace listener above. 
      _rollingFileCategorySource.TraceListeners.Add(new TraceListenerReferenceData("Rolling Flat File Trace Listener")); 

      //Add category source information to current config 
      logginConfigurationSection.TraceSources.Add(_rollingFileCategorySource);   

      //Add the loggingConfiguration section to the config. 
      configBuilder.AddSection("loggingConfiguration", logginConfigurationSection); 

      //Required code to update the EntLib Configuration with settings set above. 
      var configSource = new DictionaryConfigurationSource(); 
      configBuilder.UpdateConfigurationWithReplace(configSource); 

      //Set the Enterprise Library Container for the inner workings of EntLib to use when logging 
      EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource); 

任何幫助,將不勝感激!

回答

4

您的時間戳記模式是錯誤的。它應該是yyy-mm-dd而不是MM/dd/yyyy。 '/'字符不受支持。

此外,您可以更容易地使用fluent configuration interface來實現您的目標。方法如下:

ConfigurationSourceBuilder formatBuilder = new ConfigurationSourceBuilder(); 

    ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder(); 
    builder.ConfigureLogging().LogToCategoryNamed("General"). 
     SendTo. 
     RollingFile("Rolling Flat File Trace Listener") 
     .CleanUpArchivedFilesWhenMoreThan(2).WhenRollFileExists(RollFileExistsBehavior.Increment) 
     .WithTraceOptions(TraceOptions.None) 
     .RollEvery(RollInterval.Minute) 
     .RollAfterSize(10) 
     .UseTimeStampPattern("yyyy-MM-dd") 
     .ToFile("C:\\logs\\Trace.log") 
     .FormatWith(new FormatterBuilder().TextFormatterNamed("textFormatter")); 

    var configSource = new DictionaryConfigurationSource(); 
    builder.UpdateConfigurationWithReplace(configSource); 
    EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource); 
    var writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>(); 

    DateTime stopWritingTime = DateTime.Now.AddMinutes(10); 
    while (DateTime.Now < stopWritingTime) 
    { 
     writer.Write("test", "General"); 
    } 
相關問題