Defines a class that allows the user to configure file naming.
Пример #1
0
 protected FileTargetBase(Layout layout)
     : base(layout)
 {
     FileNamingParameters = new FileNamingParameters();
     RetainDays = 30;
     KeepLogFilesOpenForWrite = true;
 }
Пример #2
0
        async Task CheckCleanupAsync()
        {
            var now = DateTime.UtcNow;

            if (now < NextCleanupUtc || RetainDays < 1)
            {
                return;
            }

            try
            {
                // threshold...
                var threshold = now.AddDays(0 - RetainDays);

                // walk...
                var regex = FileNamingParameters.GetRegex();

                await DoCleanup(regex, threshold);
            }
            finally
            {
                // reset...
                NextCleanupUtc = DateTime.UtcNow.AddHours(1);
            }
        }
Пример #3
0
 protected FileTargetBase(Layout layout)
     : base(layout)
 {
     FileNamingParameters     = new FileNamingParameters();
     RetainDays               = 30;
     KeepLogFilesOpenForWrite = true;
 }
Пример #4
0
        internal async Task ForceCleanupAsync()
        {
            // threshold...
            var threshold = DateTime.UtcNow.AddDays(0 - RetainDays);

            // walk...
            var regex = FileNamingParameters.GetRegex();

            await DoCleanup(regex, threshold);
        }
Пример #5
0
        sealed protected override async Task <LogWriteOperation> WriteAsyncCore(LogWriteContext context, LogEventInfo entry)
        {
            using (await _lock.LockAsync())
            {
                await EnsureInitialized();
                await CheckCleanupAsync();

                var filename = FileNamingParameters.GetFilename(context, entry);
                var contents = Layout.GetFormattedString(context, entry);

                return(await DoWriteAsync(filename, contents, entry));
            }
        }
        public void TestLoggerOn()
        {
            var naming = new FileNamingParameters()
            {
                IncludeLevel = false,
                IncludeLogger = true,
                IncludeSequence = false,
                IncludeSession = false,
                IncludeTimestamp = FileTimestampMode.None
            };

            // ok...
            var info = GetLogEventInfo();

            // check...
            var filename = naming.GetFilename(new LogWriteContext(), info);
            Assert.Equal("Log - foobar.log", filename);
        }
        public void TestEverythingOn()
        {
            var naming = new FileNamingParameters()
            {
                IncludeLevel = true,
                IncludeLogger = true,
                IncludeSequence = true,
                IncludeSession = true,
                IncludeTimestamp = FileTimestampMode.DateTime
            };

            // ok...
            var info = GetLogEventInfo();

            // check...
            var context = new LogWriteContext();
            var filename = naming.GetFilename(context, info);
            Assert.Equal(string.Format("Log - INFO - foobar - {0} - {1} - {2}.log", LogManager.GetDateTime().ToString("yyyyMMdd HHmmss"),
                context.Environment.SessionId, info.SequenceID), filename);
        }
Пример #8
0
        protected sealed override async Task <LogWriteOperation> WriteAsyncCore(LogWriteContext context, LogEventInfo entry)
        {
            using (await _lock.LockAsync().ConfigureAwait(false))
            {
                await EnsureInitialized().ConfigureAwait(false);
                await CheckCleanupAsync().ConfigureAwait(false);

                var filename = FileNamingParameters.GetFilename(context, entry);
                var contents = Layout.GetFormattedString(context, entry);

                var sw = await GetOrCreateStreamWriterForFile(filename).ConfigureAwait(false);

                var op = await DoWriteAsync(sw, contents, entry);

                if (!KeepLogFilesOpenForWrite)
                {
                    sw.Dispose();
                }

                return(op);
            }
        }
Пример #9
0
        private async Task ZipFolderContents(StorageFolder sourceFolder, ZipArchive archive, string baseDirPath)
        {
            IReadOnlyList <StorageFile> files = await sourceFolder.GetFilesAsync();

            Regex pattern = FileNamingParameters.GetRegex();

            foreach (StorageFile file in files)
            {
                if (pattern.Match(file.Name).Success)
                {
                    ZipArchiveEntry readmeEntry = archive.CreateEntry(file.Name);

                    byte[] buffer = await ReadStorageFileToByteBuffer(file);

                    Stream entryStream = readmeEntry.Open();
                    await entryStream.WriteAsync(buffer, 0, buffer.Length);

                    await entryStream.FlushAsync();

                    entryStream.Dispose();
                }
            }
        }
Пример #10
0
        public void TestTimestampDate()
        {
            var naming = new FileNamingParameters()
            {
                IncludeLevel = false,
                IncludeLogger = false,
                IncludeSequence = false,
                IncludeSession = false,
                IncludeTimestamp = FileTimestampMode.Date
            };

            // ok...
            var info = GetLogEventInfo();

            // check...
            var filename = naming.GetFilename(new LogWriteContext(), info);
            Assert.Equal($"Log - {LogManager.GetDateTime() .ToString("yyyyMMdd")}.log", filename);
        }
Пример #11
0
        public void TestRegexEverythingOn()
        {
            var naming = new FileNamingParameters()
            {
                IncludeLevel = true,
                IncludeLogger = true,
                IncludeSequence = true,
                IncludeSession = true,
                IncludeTimestamp = FileTimestampMode.DateTime
            };

            // get...
            var regex = naming.GetRegex();

            // create...
            var info = GetLogEventInfo();

            // check...
            var context = new LogWriteContext();
            var filename = naming.GetFilename(context, info);
  
            // check...
            Assert.True(regex.Match(filename).Success);
        }
Пример #12
0
        public void TestTimestampTime()
        {
            var naming = new FileNamingParameters()
            {
                IncludeLevel = false,
                IncludeLogger = false,
                IncludeSequence = false,
                IncludeSession = false,
                IncludeTimestamp = FileTimestampMode.Time
            };

            // ok...
            var info = GetLogEventInfo();

            // check...
            var filename = naming.GetFilename(new LogWriteContext(), info);
            Assert.Equal(string.Format("Log - {0}.log", LogManagerBase.GetDateTime().ToString("HHmmss")), filename);
        }
Пример #13
0
 protected FileTargetBase(Layout layout)
     : base(layout)
 {
     FileNamingParameters = new FileNamingParameters();
     RetainDays = 30;
 }