public async Task NoopWhenLoggingDisabled() { var options = CreateOptionsAccessor(); options.CurrentValue.LoggingFields = W3CLoggingFields.None; var logger = new TestW3CLogger(options, new HostingEnvironment(), NullLoggerFactory.Instance); var middleware = new W3CLoggingMiddleware( c => { c.Response.StatusCode = 200; return(Task.CompletedTask); }, options, logger); var httpContext = new DefaultHttpContext(); httpContext.Request.Protocol = "HTTP/1.0"; httpContext.Request.Method = "GET"; httpContext.Request.Path = new PathString("/foo"); httpContext.Request.QueryString = new QueryString("?foo"); httpContext.Request.Headers["Referer"] = "bar"; await middleware.Invoke(httpContext); Assert.Empty(logger.Processor.Lines); }
public async Task WritesDateTime() { var path = Path.GetTempFileName() + "_"; var now = DateTime.Now; var options = new W3CLoggerOptions() { LoggingFields = W3CLoggingFields.Date | W3CLoggingFields.Time | W3CLoggingFields.TimeTaken, LogDirectory = path }; try { await using (var logger = new TestW3CLogger(new OptionsWrapperMonitor <W3CLoggerOptions>(options), new HostingEnvironment(), NullLoggerFactory.Instance)) { var elements = new string[W3CLoggingMiddleware._fieldsLength]; AddToList(elements, W3CLoggingMiddleware._dateIndex, _timestampOne.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)); AddToList(elements, W3CLoggingMiddleware._timeIndex, _timestampOne.ToString("HH:mm:ss", CultureInfo.InvariantCulture)); logger.Log(elements); await logger.Processor.WaitForWrites(4).DefaultTimeout(); var lines = logger.Processor.Lines; Assert.Equal("#Version: 1.0", lines[0]); Assert.StartsWith("#Start-Date: ", lines[1]); var startDate = DateTime.Parse(lines[1].Substring(13), CultureInfo.InvariantCulture); // Assert that the log was written in the last 10 seconds Assert.True(now.Subtract(startDate).TotalSeconds < 10); Assert.Equal("#Fields: date time time-taken", lines[2]); Assert.StartsWith("2021-01-02 03:04:05 ", lines[3]); // Assert that the log's time-taken is within 10 seconds of DateTime.Now minus our arbitary start time (01/02/21 at 3:04:05) Assert.True(now.Subtract(_timestampOne).TotalSeconds - Convert.ToDouble(lines[3].Substring(20), CultureInfo.InvariantCulture) < 10); } } finally { Helpers.DisposeDirectory(path); } }
public async Task HandlesNullValuesAsync() { var path = Path.GetTempFileName() + "_"; var now = DateTime.UtcNow; var options = new W3CLoggerOptions() { LoggingFields = W3CLoggingFields.UriQuery | W3CLoggingFields.Host | W3CLoggingFields.ProtocolStatus, LogDirectory = path }; try { await using (var logger = new TestW3CLogger(new OptionsWrapperMonitor <W3CLoggerOptions>(options), new HostingEnvironment(), NullLoggerFactory.Instance)) { var elements = new string[W3CLoggingMiddleware._fieldsLength]; AddToList(elements, W3CLoggingMiddleware._uriQueryIndex, null); AddToList(elements, W3CLoggingMiddleware._hostIndex, null); AddToList(elements, W3CLoggingMiddleware._protocolStatusIndex, null); logger.Log(elements); await logger.Processor.WaitForWrites(4).DefaultTimeout(); var lines = logger.Processor.Lines; Assert.Equal("#Version: 1.0", lines[0]); Assert.StartsWith("#Start-Date: ", lines[1]); var startDate = DateTime.Parse(lines[1].Substring(13), CultureInfo.InvariantCulture); // Assert that the log was written in the last 10 seconds Assert.True(now.Subtract(startDate).TotalSeconds < 10); Assert.Equal("#Fields: cs-uri-query sc-status cs-host", lines[2]); Assert.Equal("- - -", lines[3]); } } finally { Helpers.DisposeDirectory(path); } }
public async Task DefaultDoesNotLogOptionalFields() { var options = CreateOptionsAccessor(); var logger = new TestW3CLogger(options, new HostingEnvironment(), NullLoggerFactory.Instance); var middleware = new W3CLoggingMiddleware( c => { c.Response.StatusCode = 200; return(Task.CompletedTask); }, options, logger); var httpContext = new DefaultHttpContext(); httpContext.Request.Protocol = "HTTP/1.0"; httpContext.Request.Headers["Cookie"] = "Snickerdoodle"; httpContext.Response.StatusCode = 200; var now = DateTime.Now; await middleware.Invoke(httpContext); await logger.Processor.WaitForWrites(4).DefaultTimeout(); var lines = logger.Processor.Lines; Assert.Equal("#Version: 1.0", lines[0]); Assert.StartsWith("#Start-Date: ", lines[1]); var startDate = DateTime.Parse(lines[1].Substring(13), CultureInfo.InvariantCulture); // Assert that the log was written in the last 10 seconds Assert.True(now.Subtract(startDate).TotalSeconds < 10); Assert.Equal("#Fields: date time c-ip s-computername s-ip s-port cs-method cs-uri-stem cs-uri-query sc-status time-taken cs-version cs-host cs(User-Agent) cs(Referer)", lines[2]); Assert.DoesNotContain(lines[2], "Snickerdoodle"); }
public async Task TimeTakenIsInMilliseconds() { var options = CreateOptionsAccessor(); options.CurrentValue.LoggingFields = W3CLoggingFields.TimeTaken; var logger = new TestW3CLogger(options, new HostingEnvironment(), NullLoggerFactory.Instance); var middleware = new W3CLoggingMiddleware( c => { c.Response.StatusCode = 200; return(Task.CompletedTask); }, options, logger); var httpContext = new DefaultHttpContext(); var now = DateTime.Now; await middleware.Invoke(httpContext); await logger.Processor.WaitForWrites(4).DefaultTimeout(); var lines = logger.Processor.Lines; Assert.Equal("#Version: 1.0", lines[0]); Assert.StartsWith("#Start-Date: ", lines[1]); var startDate = DateTime.Parse(lines[1].Substring(13), CultureInfo.InvariantCulture); // Assert that the log was written in the last 10 seconds Assert.True(now.Subtract(startDate).TotalSeconds < 10); Assert.Equal("#Fields: time-taken", lines[2]); double num; Assert.True(Double.TryParse(lines[3], NumberStyles.Number, CultureInfo.InvariantCulture, out num)); }