コード例 #1
0
        public static void DeleteOldLogFiles(LoggingData data, string dirFullName)
        {
            if (
                data.ConfigurationSettings.DaysToRetain <= 0 ||        //don't bother to check if we're not configured to delete files.
                _lastFileDeleteCheck > DateTime.Now.AddMinutes(-5)     //only check every 5 minutes, avoid costly call on every log entry.
                )
            {
                return;
            }

            Console.WriteLine($"Checking delete log files, dir: {dirFullName}");

            var numberOfDays = data.ConfigurationSettings.DaysToRetain;
            var cutoffDate   = DateTime.Now.Date.AddDays(numberOfDays * -1);

            var dirInfo             = new DirectoryInfo(dirFullName);
            var listOfFilesToDelete = dirInfo.GetFiles().Where(x => x.CreationTime.Date < cutoffDate)
                                      .Select(x => x)
                                      .ToList();

            foreach (var file in listOfFilesToDelete)
            {
                Console.WriteLine($"Deleting old log file: {file.Name}");
                file.Delete();
            }
            _lastFileDeleteCheck = DateTime.Now;
        }
コード例 #2
0
        //public async Task FatalAsync(string message) => await FatalAsync(message, null);
        //public async Task FatalAsync(string message, Exception exception) => await LogAsync(LogLevel.Fatal, message, exception);
        //-----------------------

        private void Log(LogLevel logLevel, string message, Exception exception)
        {
            if (logLevel < FileLogLevelAsEnum && logLevel < ConsoleLogLevelAsEnum)
            {
                return;
            }

            var data = new LoggingData
            {
                ConfigurationSettings = ConfigurationSettings,
                LogLevel        = logLevel,
                CurrentDateTime = DateTime.Now,
                Message         = message
            };

            var renderer = new ContentRenderer {
                LayoutData = data
            };

            //determine log file we're writing to
            var logFileFullName = LogFileManagement.SetUpLogFile(data);
            //format the string to write
            var logContent = renderer.Render(ConfigurationSettings.LogEntryLayout) + Environment.NewLine;

            if (exception != null)
            {
                logContent += $">>>Exception Message: {exception.Message}{Environment.NewLine}{exception.StackTrace}{Environment.NewLine}";
            }

            //if Console logging enabled, write to Console

            if (ConfigurationSettings.LogToConsole && logLevel >= ConsoleLogLevelAsEnum)
            {
                Console.Write(logContent);
            }

            //write/append, non locking
            if (logLevel >= FileLogLevelAsEnum)
            {
                LogFileManagement.WriteLog(logFileFullName, logContent);
            }
        }
コード例 #3
0
        public static string SetUpLogFile(LoggingData data)
        {
            CreateDirIfNotExists(data.ConfigurationSettings.LogDirectory);
            var dirFullName = Path.GetFullPath(data.ConfigurationSettings.LogDirectory);
            var dirInfo     = new DirectoryInfo(dirFullName);

            if (!dirInfo.Exists)
            {
                throw new ArgumentException($"Directory {data.ConfigurationSettings.LogDirectory} not exists and/or could not be created");
            }

            DeleteOldLogFiles(data, dirFullName);

            var renderer = new ContentRenderer
            {
                LayoutData = data
            };

            var filename = renderer.Render(data.ConfigurationSettings.LogFileName);

            return(Path.Combine(data.ConfigurationSettings.LogDirectory, filename));
        }