如何使用Rolling logger强制新文件

时间:2010-12-09 18:30:09

标签: logging enterprise-library

我希望每次应用程序启动时都会创建一个新的日志文件。如果已存在日志文件,我希望重命名现有文件。这可能吗?我当前的Listener条目如下所示

  <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 个答案:

答案 0 :(得分: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;
        }
    }

`

希望这有帮助。