public async Task WritesToNewFileOnOptionsChange()
    {
        var mockSystemDateTime = new MockSystemDateTime
        {
            Now = _today
        };

        var path = Path.Combine(TempPath, Path.GetRandomFileName());

        Directory.CreateDirectory(path);

        try
        {
            var options = new W3CLoggerOptions()
            {
                LogDirectory  = path,
                LoggingFields = W3CLoggingFields.Time,
                FileSizeLimit = 10000
            };
            var fileName1 = Path.Combine(path, FormattableString.Invariant($"{options.FileName}{_today.Year:0000}{_today.Month:00}{_today.Day:00}.0000.txt"));
            var fileName2 = Path.Combine(path, FormattableString.Invariant($"{options.FileName}{_today.Year:0000}{_today.Month:00}{_today.Day:00}.0001.txt"));
            var monitor   = new OptionsWrapperMonitor <W3CLoggerOptions>(options);

            await using (var logger = new FileLoggerProcessor(monitor, new HostingEnvironment(), NullLoggerFactory.Instance))
            {
                logger.SystemDateTime = mockSystemDateTime;
                logger.EnqueueMessage(_messageOne);
                await WaitForFile(fileName1, _messageOne.Length).DefaultTimeout();

                options.LoggingFields = W3CLoggingFields.Date;
                monitor.InvokeChanged();
                logger.EnqueueMessage(_messageTwo);
                // Pause for a bit before disposing so logger can finish logging
                await WaitForFile(fileName2, _messageTwo.Length).DefaultTimeout();
            }

            var actualFiles = new DirectoryInfo(path)
                              .GetFiles()
                              .Select(f => f.Name)
                              .OrderBy(f => f)
                              .ToArray();

            Assert.Equal(2, actualFiles.Length);

            Assert.True(File.Exists(fileName1));
            Assert.True(File.Exists(fileName2));

            Assert.Equal(_messageOne + Environment.NewLine, File.ReadAllText(fileName1));
            Assert.Equal(_messageTwo + Environment.NewLine, File.ReadAllText(fileName2));
        }
        finally
        {
            Helpers.DisposeDirectory(path);
        }
    }
Exemple #2
0
    public async Task WritesToNewFileOnOptionsChange(bool fieldsChanged, bool headersChanged)
    {
        var mockSystemDateTime = new MockSystemDateTime
        {
            Now = _today
        };

        var path = Path.Combine(TempPath, Path.GetRandomFileName());

        Directory.CreateDirectory(path);

        try
        {
            var options = new W3CLoggerOptions()
            {
                LogDirectory  = path,
                LoggingFields = W3CLoggingFields.Time,
                FileSizeLimit = 10000,
            };
            options.AdditionalRequestHeaders.Add("one");
            var filePath1 = GetLogFilePath(path, options.FileName, _today, 0);
            var filePath2 = GetLogFilePath(path, options.FileName, _today, 1);
            var monitor   = new OptionsWrapperMonitor <W3CLoggerOptions>(options);

            await using (var logger = new FileLoggerProcessor(monitor, new HostingEnvironment(), NullLoggerFactory.Instance))
            {
                logger.SystemDateTime = mockSystemDateTime;
                logger.EnqueueMessage(_messageOne);
                await WaitForFile(filePath1, _messageOne.Length).DefaultTimeout();

                if (fieldsChanged)
                {
                    options.LoggingFields = W3CLoggingFields.Date;
                }

                if (headersChanged)
                {
                    options.AdditionalRequestHeaders.Remove("one");
                    options.AdditionalRequestHeaders.Add("two");
                }
                monitor.InvokeChanged();
                logger.EnqueueMessage(_messageTwo);
                // Pause for a bit before disposing so logger can finish logging
                await WaitForFile(filePath2, _messageTwo.Length).DefaultTimeout();
            }

            var actualFiles = new DirectoryInfo(path)
                              .GetFiles()
                              .Select(f => f.Name)
                              .OrderBy(f => f)
                              .ToArray();

            Assert.Equal(2, actualFiles.Length);

            Assert.True(File.Exists(filePath1));
            Assert.True(File.Exists(filePath2));

            Assert.Equal(_messageOne + Environment.NewLine, File.ReadAllText(filePath1));
            Assert.Equal(_messageTwo + Environment.NewLine, File.ReadAllText(filePath2));
        }
        finally
        {
            Helpers.DisposeDirectory(path);
        }
    }