Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public void FileAppenderCache_Empty()
        {
            FileAppenderCache cache = FileAppenderCache.Empty;

            // An empty FileAppenderCache will have Size = 0 as well as Factory and CreateFileParameters parameters equal to null.
            Assert.True(cache.Size == 0);
            Assert.Null(cache.Factory);
            Assert.Null(cache.CreateFileParameters);
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        public void FileAppenderCache_GetFileInfo()
        {
            DateTime lastWriteTime;
            long     fileLength;

            // Invoke GetFileInfo() on an Empty FileAppenderCache.
            FileAppenderCache emptyCache = FileAppenderCache.Empty;

            emptyCache.GetFileInfo("file.txt", out lastWriteTime, out fileLength);
            // Default values will be returned.
            Assert.True(lastWriteTime == DateTime.MinValue);
            Assert.True(fileLength == -1);

            IFileAppenderFactory  appenderFactory = SingleProcessFileAppender.TheFactory;
            ICreateFileParameters fileTarget      = new FileTarget();
            FileAppenderCache     cache           = new FileAppenderCache(3, appenderFactory, fileTarget);

            // Invoke GetFileInfo() on non-empty FileAppenderCache - Before allocating any appenders.
            cache.GetFileInfo("file.txt", out lastWriteTime, out fileLength);
            // Default values will be returned.
            Assert.True(lastWriteTime == DateTime.MinValue);
            Assert.True(fileLength == -1);

            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.
            cache.GetFileInfo(tempFile, out lastWriteTime, out fileLength);
            Assert.False(lastWriteTime == DateTime.MinValue);
            Assert.True(fileLength == 34);

            // Clean up.
            appender.Flush();
            appender.Close();
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Initializes file logging by creating data structures that
        /// enable efficient multi-file logging.
        /// </summary>
        protected override void InitializeTarget()
        {
            base.InitializeTarget();
            this.appenderFactory = GetFileAppenderFactory();

            this.fileAppenderCache = new FileAppenderCache(this.OpenFileCacheSize, this.appenderFactory, this);
            RefreshArchiveFilePatternToWatch();

            if ((this.OpenFileCacheSize > 0 || this.EnableFileDelete) && this.OpenFileCacheTimeout > 0)
            {
                this.autoClosingTimer = new Timer(
                    this.AutoClosingTimerCallback,
                    null,
                    this.OpenFileCacheTimeout * 1000,
                    this.OpenFileCacheTimeout * 1000);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FileTarget" /> class.
        /// </summary>
        /// <remarks>
        /// The default value of the layout is: <code>${longdate}|${level:uppercase=true}|${logger}|${message}</code>
        /// </remarks>
        public FileTarget()
        {
            this.ArchiveNumbering = ArchiveNumberingMode.Sequence;
            this.maxArchiveFiles = 0;
            this.ConcurrentWriteAttemptDelay = 1;
            this.ArchiveEvery = FileArchivePeriod.None;
            this.ArchiveAboveSize = FileTarget.ArchiveAboveSizeDisabled;
            this.ConcurrentWriteAttempts = 10;
            this.ConcurrentWrites = true;
#if SILVERLIGHT
            this.Encoding = Encoding.UTF8;
#else
            this.Encoding = Encoding.Default;
#endif
            this.BufferSize = 32768;
            this.AutoFlush = true;
#if !SILVERLIGHT
            this.FileAttributes = Win32FileAttributes.Normal;
#endif
            this.LineEnding = LineEndingMode.Default;
            this.EnableFileDelete = true;
            this.OpenFileCacheTimeout = -1;
            this.OpenFileCacheSize = 5;
            this.CreateDirs = true;
            this.fileArchive = new DynamicFileArchive(MaxArchiveFiles);
            this.ForceManaged = false;
            this.ArchiveDateFormat = string.Empty;

            this.maxLogFilenames = 20;
            this.previousFileNames = new Queue<string>(this.maxLogFilenames);
            this.fileAppenderCache = FileAppenderCache.Empty;
            this.CleanupFileName = true;
        }