private RollingFlatFileSinkData GetRollingFlatFileSinkDataFromCursor()
        {
            RollingFlatFileSinkData sinkData = loggingConfigurationView.GetSinkData(ConfigurationName) as RollingFlatFileSinkData;

            ArgumentValidation.CheckExpectedType(sinkData, typeof(RollingFlatFileSinkData));

            return((RollingFlatFileSinkData)sinkData);
        }
        /// <summary>
        /// Writes log message after evaluating rollover criteria and rolling over files if needed.
        /// </summary>
        /// <param name="logEntry">Base log file name.</param>
        private void WriteMessageToFile(LogEntry logEntry)
        {
            bool     newLog                    = false;
            string   logFile                   = "";
            string   directory                 = "";
            string   header                    = "";
            string   footer                    = "";
            int      rollOverByDays            = 0;
            int      rollOverByMinutes         = 0;
            double   rollOverByMegabytes       = 0;
            int      rollOverFilesToKeep       = 0;
            string   rollOverOneFilePerWeekDay = null;
            DateTime dayOfWeekDate             = DateTime.Now;

            RollingFlatFileSinkData rollingFlatFileSinkData = GetRollingFlatFileSinkDataFromCursor();             //Retrieve sink parameters

            try
            {             //Get configuration data. If any fail then throw exception and block will log to default.
                logFile                   = rollingFlatFileSinkData.FileName;
                directory                 = Path.GetDirectoryName(logFile);
                header                    = rollingFlatFileSinkData.Header;
                footer                    = rollingFlatFileSinkData.Footer;
                rollOverByDays            = rollingFlatFileSinkData.RolloverByDays;
                rollOverByMinutes         = rollingFlatFileSinkData.RolloverByMinutes;
                rollOverByMegabytes       = rollingFlatFileSinkData.RolloverByMegabytes;
                rollOverFilesToKeep       = rollingFlatFileSinkData.NumFilesToKeep;
                rollOverOneFilePerWeekDay = rollingFlatFileSinkData.OneFilePerWeekDay;
            }
            catch (Exception e)
            {
                throw new Exception("Configuration data was unable to be retrieved.  Please configure FileName, RolloverByDays, RolloverByMinutes, RolloverByMegabytes, etc.", e);
            }
            if (directory.Length == 0)             //If directory is blank then get current directory
            {
                directory = AppDomain.CurrentDomain.BaseDirectory;
                logFile   = Path.Combine(directory, logFile);               //Append working directory and file name
            }
            lock (fileWriteSyncObject)
            {
                if (rollOverOneFilePerWeekDay.ToLower() == "true")
                {                       //Under load, using the current date here might write a message that was constructed milliseconds before day turnover into the next day's log.
                                        //If this is undesired the dayOfWeekDate = DateTime.Now could be replaced with dayOfWeekDate = logEntry.Timestamp but a caller might not set the logEntry.Timestamp causing log rollover problems.
                    dayOfWeekDate = DateTime.Now;
                    logFile       = Path.Combine(Path.GetDirectoryName(logFile), Path.GetFileNameWithoutExtension(logFile) + (int)dayOfWeekDate.DayOfWeek + Path.GetExtension(logFile));
                }
                if (File.Exists(logFile))                 //Make sure file exists before using it
                {
                    DateTime createdDate = File.GetCreationTime(logFile);
                    if (rollOverOneFilePerWeekDay.ToLower() == "true")                     //If special rollover by day of week
                    {
                        if (createdDate.Day != dayOfWeekDate.Day)
                        {
                            RolloverFileByDayOfWeek(logFile, dayOfWeekDate);                             //Delete and recreate the file
                        }
                    }
                    else
                    {
                        //Check for rollover on days
                        if (rollOverByDays > 0 && (createdDate.AddDays(rollOverByDays).Date < DateTime.Now.Date))
                        {
                            if (RolloverFile(logFile, createdDate, rollOverFilesToKeep))
                            {
                                newLog = true;
                            }
                        }
                        //Check for rollover on minutes if not already rolled
                        if (!newLog && rollOverByMinutes > 0 && (createdDate.AddMinutes(rollOverByMinutes) < DateTime.Now))
                        {
                            if (RolloverFile(logFile, createdDate, rollOverFilesToKeep))
                            {
                                newLog = true;
                            }
                        }
                        if (!newLog)                         //Check for rollover on size in MB (1MB = 1048576) if not already rolled
                        {
                            if (rollOverByMegabytes > 0 && (new FileInfo(logFile).Length / 1048576.0000000000) > rollOverByMegabytes)
                            {
                                if (RolloverFile(logFile, createdDate, rollOverFilesToKeep))
                                {
                                    newLog = true;
                                }
                            }
                        }
                    }
                }
                else                                  //File did not exist
                {
                    if (!Directory.Exists(directory)) //Create directory if doesn't exist
                    {
                        Directory.CreateDirectory(directory);
                    }
                    newLog = true;                     //File did not exist so set flag that triggers DateTime stamp
                }

                using (FileStream fileStream = new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    using (StreamWriter writer = new StreamWriter(fileStream))                     //Write message to the log
                    {
                        if (header.Length > 0)
                        {
                            writer.WriteLine(header);
                        }
                        writer.WriteLine(FormatEntry(logEntry));
                        if (footer.Length > 0)
                        {
                            writer.WriteLine(footer);
                        }
                        writer.Flush();
                        if (newLog)                         //If we created a new file make sure it has a new time stamp
                        {
                            File.SetCreationTime(logFile, DateTime.Now);
                        }
                    }
                }
            }
        }
Beispiel #3
0
 /// <summary>
 /// Creates node with specified configuration data.
 /// </summary>
 /// <param name="rollingFlatFileSinkData">Configuration data.</param>
 public RollingFlatFileSinkNode(RollingFlatFileSinkData rollingFlatFileSinkData) : base(rollingFlatFileSinkData)
 {
     this.rollingFlatFileSinkData = rollingFlatFileSinkData;
 }