Maintains a collection of file appenders usually associated with file targets.
コード例 #1
0
ファイル: FileAppenderCacheTests.cs プロジェクト: NLog/NLog
        public void FileAppenderCache_Allocate()
        {
            // Allocate on an Empty FileAppenderCache.
            FileAppenderCache emptyCache = FileAppenderCache.Empty;
            Assert.Throws<NullReferenceException>(() => emptyCache.AllocateAppender("file.txt"));

            // Construct a on non-empty FileAppenderCache.
            IFileAppenderFactory appenderFactory = SingleProcessFileAppender.TheFactory;
            ICreateFileParameters fileTarget = new FileTarget();
            String tempFile = Path.Combine(
                    Path.GetTempPath(),
                    Path.Combine(Guid.NewGuid().ToString(), "file.txt")
            );
            
            // Allocate an appender.
            FileAppenderCache cache = new FileAppenderCache(3, appenderFactory, fileTarget);
            BaseFileAppender appender = cache.AllocateAppender(tempFile);

            //
            // Note: Encoding is ASSUMED to be Unicode. There is no explicit reference to which encoding will be used 
            //      for the file. 
            //

            // Write, flush the content into the file and release the file.
            // We need to release the file before invoking AssertFileContents() method.
            appender.Write(StringToBytes("NLog test string."));
            appender.Flush();
            appender.Close();
            // Verify the appender has been allocated correctly.
            AssertFileContents(tempFile, "NLog test string.", Encoding.Unicode);
        }
コード例 #2
0
        public void FileAppenderCache_Construction()
        {
            IFileAppenderFactory appenderFactory = SingleProcessFileAppender.TheFactory;
            ICreateFileParameters fileTarget = new FileTarget();
            FileAppenderCache cache = new FileAppenderCache(3, appenderFactory, fileTarget);

            Assert.True(cache.Size == 3);
            Assert.NotNull(cache.Factory);           
            Assert.NotNull(cache.CreateFileParameters);
        }
コード例 #3
0
        public void FileAppenderCache_GetFileCharacteristics()
        {
            // Invoke GetFileCharacteristics() on an Empty FileAppenderCache.
            FileAppenderCache emptyCache = FileAppenderCache.Empty;
            Assert.Null(emptyCache.GetFileCreationTimeUtc("file.txt", false));
            Assert.Null(emptyCache.GetFileLastWriteTimeUtc("file.txt", false));
            Assert.Null(emptyCache.GetFileLength("file.txt", false));

            IFileAppenderFactory appenderFactory = SingleProcessFileAppender.TheFactory;
            ICreateFileParameters fileTarget = new FileTarget() {ArchiveNumbering = ArchiveNumberingMode.Date};
           
            FileAppenderCache cache = new FileAppenderCache(3, appenderFactory, fileTarget);
            // Invoke GetFileCharacteristics() on non-empty FileAppenderCache - Before allocating any appenders. 
            Assert.Null(emptyCache.GetFileCreationTimeUtc("file.txt", false));
            Assert.Null(emptyCache.GetFileLastWriteTimeUtc("file.txt", false));
            Assert.Null(emptyCache.GetFileLength("file.txt", false));


            String tempFile = Path.Combine(
                    Path.GetTempPath(),
                    Path.Combine(Guid.NewGuid().ToString(), "file.txt")
            );

            // Allocate an appender.
            BaseFileAppender appender = cache.AllocateAppender(tempFile);
            appender.Write(StringToBytes("NLog test string."));

            //
            // Note: Encoding is ASSUMED to be Unicode. There is no explicit reference to which encoding will be used 
            //      for the file. 
            //

            // File information should be returned.
           
            var fileCreationTimeUtc = cache.GetFileCreationTimeUtc(tempFile, false);
            Assert.NotNull(fileCreationTimeUtc);
            Assert.True(fileCreationTimeUtc > DateTime.UtcNow.AddMinutes(-2),"creationtime is wrong");

            var fileLastWriteTimeUtc = cache.GetFileLastWriteTimeUtc(tempFile, false);
            Assert.NotNull(fileLastWriteTimeUtc);
            Assert.True(fileLastWriteTimeUtc > DateTime.UtcNow.AddMinutes(-2), "lastwrite is wrong");

            Assert.Equal(34, cache.GetFileLength(tempFile, false));

            // Clean up.
            appender.Flush();
            appender.Close();
        }
コード例 #4
0
        public void FileAppenderCache_CloseAppenders()
        {
            // Invoke CloseAppenders() on an Empty FileAppenderCache.
            FileAppenderCache emptyCache = FileAppenderCache.Empty;
            emptyCache.CloseAppenders();

            
            IFileAppenderFactory appenderFactory = SingleProcessFileAppender.TheFactory;
            ICreateFileParameters fileTarget = new FileTarget();
            FileAppenderCache cache = new FileAppenderCache(3, appenderFactory, fileTarget);
            // Invoke CloseAppenders() on non-empty FileAppenderCache - Before allocating any appenders. 
            cache.CloseAppenders();            

            // Invoke CloseAppenders() on non-empty FileAppenderCache - After allocating N appenders. 
            cache.AllocateAppender("file1.txt");
            cache.AllocateAppender("file2.txt");
            cache.CloseAppenders();

            // Invoke CloseAppenders() on non-empty FileAppenderCache - After allocating N appenders. 
            cache.AllocateAppender("file1.txt");
            cache.AllocateAppender("file2.txt");
            cache.CloseAppenders();

            FileAppenderCache cache2 = new FileAppenderCache(3, appenderFactory, fileTarget);
            // Invoke CloseAppenders() on non-empty FileAppenderCache - Before allocating any appenders. 
            cache2.CloseAppenders(DateTime.Now);

            // Invoke CloseAppenders() on non-empty FileAppenderCache - After allocating N appenders. 
            cache.AllocateAppender("file1.txt");
            cache.AllocateAppender("file2.txt");
            cache.CloseAppenders(DateTime.Now);
        }