开发者

How to force a new file with Rolling logger

I would like a new log file created everytime the application starts. If a log file already exists, I would like the existing one renamed. Is this possible? My current Listener entry looks like this

  <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="M开发者_JAVA百科essageOnlyFormatter"
    header="" rollFileExistsBehavior="Increment" rollSizeKB="1024" />

What am i missing?


I've had the same issue numerous times. There doesn't seem to be a way of solving this in a configurational manner. I always solve this issue in code in the wrapper class.

I'd recommend adding timeStampPattern="yyyy-MM-dd" to your config. The code works without it'just easier to find the required log file.

The code:

`

    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;
        }
    }

`

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜