public void FileTarget_WithArchiveFileNameEndingInNumberPlaceholder_ShouldArchiveFile()
        {
            var tempPath = Path.Combine(GenerateRandomFileName(), Guid.NewGuid().ToString());
            var tempFile = Path.Combine(tempPath, "file.txt");
            var isft = new IsolatedStorageFileTarget
            {
                FileName = tempFile,
                ArchiveFileName = Path.Combine(tempPath, "archive/test.log.{####}"),
                ArchiveAboveSize = 1000
            };
            try
            {

                SimpleConfigurator.ConfigureForTargetLogging(isft, LogLevel.Debug);

                for (var i = 0; i < 100; ++i)
                {
                    logger.Debug("a");
                }

                LogManager.Configuration = null;
                Assert.IsTrue(isft.IsolatedStorageFile.FileExists(tempFile));
                Assert.IsTrue(isft.IsolatedStorageFile.FileExists(Path.Combine(tempPath, "archive/test.log.0000")));
            }
            finally
            {
                LogManager.Configuration = null;
                if (isft.IsolatedStorageFile.FileExists(tempFile))
                    isft.IsolatedStorageFile.DeleteFile(tempFile);
                if (isft.IsolatedStorageFile.DirectoryExists(tempPath))
                    DeleteDirectoryRecursive(isft.IsolatedStorageFile, tempPath);
            }
        }
        public void BatchErrorHandlingTest()
        {
            var fileTarget = new IsolatedStorageFileTarget 
            { 
                FileName = "${logger}", 
                Layout = "${message}" 
            };
            fileTarget.Initialize(null);

            // make sure that when file names get sorted, the asynchronous continuations are sorted with them as well
            var exceptions = new List<Exception>();
            var events = new[]
            {
                new LogEventInfo(LogLevel.Info, "file99.txt", "msg1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Info, "a/", "msg1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Info, "a/", "msg2").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Info, "a/", "msg3").WithContinuation(exceptions.Add)
            };

            fileTarget.WriteAsyncLogEvents(events);

            Assert.AreEqual(4, exceptions.Count);
            Assert.IsNull(exceptions[0]);
            Assert.IsNotNull(exceptions[1]);
            Assert.IsNotNull(exceptions[2]);
            Assert.IsNotNull(exceptions[3]);
        }
        public void DisposingFileTarget_WhenNotIntialized_ShouldNotThrow()
        {
            var exceptionThrown = false;
            var fileTarget = new IsolatedStorageFileTarget();

            try
            {
                fileTarget.Dispose();
            }
            catch
            {
                exceptionThrown = true;
            }

            Assert.IsFalse(exceptionThrown);
        }
        public void BufferedMultiFileWrite()
        {
            var tempPath = Path.Combine(GenerateRandomFileName(), Guid.NewGuid().ToString());
            var isft = new IsolatedStorageFileTarget
            {
                FileName = Path.Combine(tempPath, "${level}.txt"),
                LineEnding = LineEndingMode.LF,
                Layout = "${message}"
            };
            try
            {

                SimpleConfigurator.ConfigureForTargetLogging(new BufferingTargetWrapper(isft, 10), LogLevel.Debug);

                for (var i = 0; i < 250; ++i)
                {
                    logger.Trace("@@@");
                    logger.Debug("aaa");
                    logger.Info("bbb");
                    logger.Warn("ccc");
                    logger.Error("ddd");
                    logger.Fatal("eee");
                }

                LogManager.Configuration = null;

                Assert.IsFalse(isft.IsolatedStorageFile.FileExists(Path.Combine(tempPath, "Trace.txt")));

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, Path.Combine(tempPath, "Debug.txt"),
                    StringRepeat(250, "aaa\n"), Encoding.UTF8);

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, Path.Combine(tempPath, "Info.txt"),
                    StringRepeat(250, "bbb\n"), Encoding.UTF8);

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, Path.Combine(tempPath, "Warn.txt"),
                    StringRepeat(250, "ccc\n"), Encoding.UTF8);

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, Path.Combine(tempPath, "Error.txt"),
                    StringRepeat(250, "ddd\n"), Encoding.UTF8);

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, Path.Combine(tempPath, "Fatal.txt"),
                    StringRepeat(250, "eee\n"), Encoding.UTF8);
            }
            finally
            {
                //if (isft.IsolatedStorageFile.FileExists(tempFile))
                //    isolatedStorageFile.DeleteFile(tempFile);
                LogManager.Configuration = null;
                if (isft.IsolatedStorageFile.DirectoryExists(tempPath))
                    DeleteDirectoryRecursive(isft.IsolatedStorageFile, tempPath);
            }
        }
        public void AsyncMultiFileWrite()
        {
            var tempPath = Path.Combine(GenerateRandomFileName(), Guid.NewGuid().ToString());
            var isft = new IsolatedStorageFileTarget
            {
                FileName = Path.Combine(tempPath, "${level}.txt"),
                LineEnding = LineEndingMode.LF,
                Layout = "${message} ${threadid}"
            };
            try
            {

                // this also checks that thread-volatile layouts
                // such as ${threadid} are properly cached and not recalculated
                // in logging threads.

                var threadID = Thread.CurrentThread.ManagedThreadId.ToString();

                SimpleConfigurator.ConfigureForTargetLogging(new AsyncTargetWrapper(isft, 1000, AsyncTargetWrapperOverflowAction.Grow), LogLevel.Debug);
                LogManager.ThrowExceptions = true;

                for (var i = 0; i < 250; ++i)
                {
                    logger.Trace("@@@");
                    logger.Debug("aaa");
                    logger.Info("bbb");
                    logger.Warn("ccc");
                    logger.Error("ddd");
                    logger.Fatal("eee");
                }
                LogManager.Flush();
                LogManager.Configuration = null;

                Assert.IsFalse(isft.IsolatedStorageFile.FileExists(Path.Combine(tempPath, "Trace.txt")));

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, Path.Combine(tempPath, "Debug.txt"),
                    StringRepeat(250, "aaa " + threadID + "\n"), Encoding.UTF8);

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, Path.Combine(tempPath, "Info.txt"),
                    StringRepeat(250, "bbb " + threadID + "\n"), Encoding.UTF8);

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, Path.Combine(tempPath, "Warn.txt"),
                    StringRepeat(250, "ccc " + threadID + "\n"), Encoding.UTF8);

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, Path.Combine(tempPath, "Error.txt"),
                    StringRepeat(250, "ddd " + threadID + "\n"), Encoding.UTF8);

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, Path.Combine(tempPath, "Fatal.txt"),
                    StringRepeat(250, "eee " + threadID + "\n"), Encoding.UTF8);
            }
            finally
            {
                //if (isft.IsolatedStorageFile.FileExists(tempFile))
                //    isolatedStorageFile.DeleteFile(tempFile);
                LogManager.Configuration = null;
                if (isft.IsolatedStorageFile.DirectoryExists(tempPath))
                    DeleteDirectoryRecursive(isft.IsolatedStorageFile, tempPath);

                // Clean up configuration change, breaks onetimeonlyexceptioninhandlertest
                LogManager.ThrowExceptions = true;
            }
        }
        public void RollingArchiveTest1()
        {
            var tempPath = Path.Combine(GenerateRandomFileName(), Guid.NewGuid().ToString());
            var tempFile = Path.Combine(tempPath, "file.txt");
            var isft = new IsolatedStorageFileTarget
            {
                FileName = tempFile,
                ArchiveFileName = Path.Combine(tempPath, "archive", "{####}.txt"),
                ArchiveAboveSize = 1000,
                LineEnding = LineEndingMode.LF,
                ArchiveNumbering = ArchiveNumberingMode.Rolling,
                Layout = "${message}",
                MaxArchiveFiles = 3
            };
            try
            {

                SimpleConfigurator.ConfigureForTargetLogging(isft, LogLevel.Debug);

                // we emit 5 * 250 * (3 x aaa + \n) bytes
                // so that we should get a full file + 3 archives
                for (var i = 0; i < 250; ++i)
                {
                    logger.Debug("aaa");
                }
                for (var i = 0; i < 250; ++i)
                {
                    logger.Debug("bbb");
                }
                for (var i = 0; i < 250; ++i)
                {
                    logger.Debug("ccc");
                }
                for (var i = 0; i < 250; ++i)
                {
                    logger.Debug("ddd");
                }
                for (var i = 0; i < 250; ++i)
                {
                    logger.Debug("eee");
                }

                LogManager.Configuration = null;

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, tempFile,
                    StringRepeat(250, "eee\n"),
                    Encoding.UTF8);

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, 
                    Path.Combine(tempPath, "archive", "0000.txt"),
                    StringRepeat(250, "ddd\n"),
                    Encoding.UTF8);

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, 
                    Path.Combine(tempPath, "archive", "0001.txt"),
                    StringRepeat(250, "ccc\n"),
                    Encoding.UTF8);

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, 
                    Path.Combine(tempPath, "archive", "0002.txt"),
                    StringRepeat(250, "bbb\n"),
                    Encoding.UTF8);

                Assert.IsTrue(!isft.IsolatedStorageFile.FileExists(Path.Combine(tempPath, "archive", "0003.txt")));
            }
            finally
            {
                LogManager.Configuration = null;
                if (isft.IsolatedStorageFile.FileExists(tempFile))
                    isft.IsolatedStorageFile.DeleteFile(tempFile);
                if (isft.IsolatedStorageFile.DirectoryExists(tempPath))
                    DeleteDirectoryRecursive(isft.IsolatedStorageFile, tempPath);
            }
        }
        public void CreateDirsTest()
        {
            var tempPath = Path.Combine(GenerateRandomFileName(), Guid.NewGuid().ToString());
            var tempFile = Path.Combine(tempPath, "file.txt");
            var isft = new IsolatedStorageFileTarget
            {
                FileName = tempFile,
                LineEnding = LineEndingMode.LF,
                Layout = "${level} ${message}"
            };
            try
            {

                SimpleConfigurator.ConfigureForTargetLogging(isft, LogLevel.Debug);

                logger.Debug("aaa");
                logger.Info("bbb");
                logger.Warn("ccc");
                LogManager.Configuration = null;
                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, tempFile, "Debug aaa\nInfo bbb\nWarn ccc\n", Encoding.UTF8);
            }
            finally
            {
                LogManager.Configuration = null;
                if (isft.IsolatedStorageFile.FileExists(tempFile))
                    isft.IsolatedStorageFile.DeleteFile(tempFile);
                if (isft.IsolatedStorageFile.DirectoryExists(tempPath))
                    DeleteDirectoryRecursive(isft.IsolatedStorageFile, tempPath);
            }
        }
        public void DeleteFileOnStartTest()
        {
            var tempFile = GenerateRandomFileName();

            var isft = new IsolatedStorageFileTarget
            {
                FileName = SimpleLayout.Escape(tempFile),
                LineEnding = LineEndingMode.LF,
                Layout = "${level} ${message}"
            };

            try
            {

                SimpleConfigurator.ConfigureForTargetLogging(isft, LogLevel.Debug);

                logger.Debug("aaa");
                logger.Info("bbb");
                logger.Warn("ccc");

                LogManager.Configuration = null;

                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, tempFile, "Debug aaa\nInfo bbb\nWarn ccc\n", Encoding.UTF8);

                // configure again, without
                // DeleteOldFileOnStartup

                isft = new IsolatedStorageFileTarget
                {
                    FileName = SimpleLayout.Escape(tempFile),
                    LineEnding = LineEndingMode.LF,
                    Layout = "${level} ${message}"
                };

                SimpleConfigurator.ConfigureForTargetLogging(isft, LogLevel.Debug);

                logger.Debug("aaa");
                logger.Info("bbb");
                logger.Warn("ccc");

                LogManager.Configuration = null;
                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, tempFile, "Debug aaa\nInfo bbb\nWarn ccc\nDebug aaa\nInfo bbb\nWarn ccc\n", Encoding.UTF8);

                // configure again, this time with
                // DeleteOldFileOnStartup

                isft = new IsolatedStorageFileTarget
                {
                    FileName = SimpleLayout.Escape(tempFile),
                    LineEnding = LineEndingMode.LF,
                    Layout = "${level} ${message}",
                    DeleteOldFileOnStartup = true
                };

                SimpleConfigurator.ConfigureForTargetLogging(isft, LogLevel.Debug);
                logger.Debug("aaa");
                logger.Info("bbb");
                logger.Warn("ccc");

                LogManager.Configuration = null;
                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, tempFile, "Debug aaa\nInfo bbb\nWarn ccc\n", Encoding.UTF8);
            }
            finally
            {
                LogManager.Configuration = null;
                if (isft.IsolatedStorageFile.FileExists(tempFile))
                    isft.IsolatedStorageFile.DeleteFile(tempFile);
            }
        }
        public void CsvHeaderTest()
        {
            var tempFile = GenerateRandomFileName();
            IsolatedStorageFile isolatedStorageFile = null;
            try
            {

                for (var i = 0; i < 2; i++)
                {
                    var layout = new CsvLayout
                    {
                        Delimiter = CsvColumnDelimiterMode.Semicolon,
                        WithHeader = true,
                        Columns =
                        {
                            new CsvColumn("name", "${logger}"),
                            new CsvColumn("level", "${level}"),
                            new CsvColumn("message", "${message}"),
                        }
                    };

                    var isft = new IsolatedStorageFileTarget
                    {
                        FileName = SimpleLayout.Escape(tempFile),
                        LineEnding = LineEndingMode.LF,
                        Layout = layout,
                        OpenFileCacheTimeout = 0,
                        ReplaceFileContentsOnEachWrite = false
                    };

                    SimpleConfigurator.ConfigureForTargetLogging(isft, LogLevel.Debug);

                    logger.Debug("aaa");
                    LogManager.Configuration = null;
                    isolatedStorageFile = isft.IsolatedStorageFile;
                }
                AssertIsolatedStorageFileContents(isolatedStorageFile, tempFile, "name;level;message\nNLog.UnitTests.Targets.FileTargetTests;Debug;aaa\nNLog.UnitTests.Targets.FileTargetTests;Debug;aaa\n", Encoding.UTF8);
            }
            finally
            {
                if (isolatedStorageFile.FileExists(tempFile))
                    isolatedStorageFile.DeleteFile(tempFile);
            }
        }
        public void SimpleFileTest1()
        {
            var tempFile = GenerateRandomFileName();
            var isft = new IsolatedStorageFileTarget
            {
                FileName = SimpleLayout.Escape(tempFile),
                LineEnding = LineEndingMode.LF,
                Layout = "${level} ${message}",
                OpenFileCacheTimeout = 0
            };
            try
            {

                SimpleConfigurator.ConfigureForTargetLogging(isft, LogLevel.Debug);

                logger.Debug("aaa");
                logger.Info("bbb");
                logger.Warn("ccc");
                LogManager.Configuration = null;
                AssertIsolatedStorageFileContents(isft.IsolatedStorageFile, tempFile, "Debug aaa\nInfo bbb\nWarn ccc\n", Encoding.UTF8);
            }
            finally
            {
                if (isft.IsolatedStorageFile.FileExists(tempFile))
                    isft.IsolatedStorageFile.DeleteFile(tempFile);
            }
        }