2010-12-09 44 views
0

我希望每次應用程序啓動時創建一個新的日誌文件。如果日誌文件已經存在,我希望現有的文件重命名。這可能嗎?我現在的監聽器入口看起來像這樣如何使用滾動記錄器強制新文件

<add name="QueueDiagListener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
    fileName="C:\Logs\QueueDiag.log" footer="" formatter="MessageOnlyFormatter" 
    header="" rollFileExistsBehavior="Increment" rollSizeKB="1024" /> 

我失去了什麼?

回答

1

我已經有很多次相同的問題。似乎沒有一種以配置方式解決這個問題的方法。我總是在包裝類的代碼中解決這個問題。

我建議將timeStampPattern="yyyy-MM-dd"添加到您的配置。代碼沒有它的工作,只是更容易找到所需的日誌文件。

代碼:

`

private static string _loggingOutputFileName; 
    private static string _loggingRollFileFormat; 

    public static void StartNewLogFile() 
    { 
     string fileName = LoggingOutputDirectory.Substring(0, LoggingOutputDirectory.LastIndexOf("\\")) + "\\" + _loggingOutputFileName; 

     if (File.Exists(fileName)) 
     { 
      string backupFileName; 
      int fileIncrement = 1; 

      while (true) 
      { 
       backupFileName = LoggingOutputDirectory.Substring(0, LoggingOutputDirectory.LastIndexOf("\\")) + "\\" + _loggingOutputFileName.Insert(_loggingOutputFileName.LastIndexOf('.'), "." + DateTime.Now.ToString(_loggingRollFileFormat) + "." + fileIncrement); 
       if (!File.Exists(backupFileName)) 
       { 
        break; 
       } 

       fileIncrement++; 
      } 

      File.Move(fileName, backupFileName); 
     } 
    } 

    /// <summary> 
    /// Gets the logging output from the app.config (loggingConfiguration section) 
    /// </summary> 
    private static void GetLoggingSettings() 
    { 
     IConfigurationSource configSource = ConfigurationSourceFactory.Create(); 

     LoggingSettings logSettings = LoggingSettings.GetLoggingSettings(configSource); 

     TraceListenerDataCollection dataCollection = logSettings.TraceListeners; 

     if (dataCollection.Count == 0) 
      return; 

     TraceListenerData traceListenerData = dataCollection.Get(0); 

     if (traceListenerData is RollingFlatFileTraceListenerData) 
     { 
      RollingFlatFileTraceListenerData tld = (RollingFlatFileTraceListenerData)traceListenerData; 
      _loggingOutputFileName = tld.FileName; 
      _loggingRollFileFormat = tld.TimeStampPattern; 
     } 
    } 

    private static string LoggingOutputDirectory 
    { 
     get 
     { 
      // Retrieve the logging settings on first call 
      if (_loggingOutputFileName == null) 
       GetLoggingSettings(); 

      // Guard against null dereference 
      if (_loggingOutputFileName == null) 
       return null; 

      // Get the directory name for logging output 
      FileInfo fileInfo = new FileInfo(_loggingOutputFileName); 
      return fileInfo.DirectoryName; 
     } 
    } 

`

希望這有助於。