Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Commit e94e3dc

Browse files
committed
adding some logging statements
1 parent 3343bb4 commit e94e3dc

File tree

7 files changed

+65
-11
lines changed

7 files changed

+65
-11
lines changed

samples/StaticFileSample/Startup.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
using Microsoft.AspNet.Builder;
22
using Microsoft.AspNet.FileSystems;
33
using Microsoft.AspNet.StaticFiles;
4+
using Microsoft.Framework.Logging;
5+
using Microsoft.Framework.Logging.Console;
46

57
namespace StaticFilesSample
68
{
79
public class Startup
810
{
9-
public void Configure(IApplicationBuilder app)
11+
public void Configure(IApplicationBuilder app, ILoggerFactory factory)
1012
{
13+
// Displays all log levels
14+
factory.AddConsole(LogLevel.Verbose);
15+
1116
app.UseFileServer(new FileServerOptions()
1217
{
1318
EnableDirectoryBrowsing = true,

samples/StaticFileSample/StaticFileSample.kproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
</PropertyGroup>
1313
<PropertyGroup>
1414
<SchemaVersion>2.0</SchemaVersion>
15+
<DevelopmentServerPort>16758</DevelopmentServerPort>
1516
</PropertyGroup>
1617
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
1718
</Project>

samples/StaticFileSample/project.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.Urls http://localhost:12345/"
44
},
55
"dependencies": {
6+
"Kestrel": "1.0.0-*",
67
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
78
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
8-
"Kestrel": "1.0.0-*",
9-
"Microsoft.AspNet.StaticFiles": "1.0.0-*"
9+
"Microsoft.AspNet.StaticFiles": "1.0.0-*",
10+
"Microsoft.Framework.Logging.Console": "1.0.0-*"
1011
},
1112
"frameworks": {
1213
"aspnet50": { },

src/Microsoft.AspNet.StaticFiles/SendFileMiddleware.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.AspNet.Builder;
99
using Microsoft.AspNet.Http;
1010
using Microsoft.AspNet.HttpFeature;
11+
using Microsoft.Framework.Logging;
1112

1213
namespace Microsoft.AspNet.StaticFiles
1314
{
@@ -20,22 +21,25 @@ namespace Microsoft.AspNet.StaticFiles
2021
public class SendFileMiddleware
2122
{
2223
private readonly RequestDelegate _next;
24+
private readonly ILogger _logger;
2325

2426
/// <summary>
2527
/// Creates a new instance of the SendFileMiddleware.
2628
/// </summary>
2729
/// <param name="next">The next middleware in the pipeline.</param>
28-
public SendFileMiddleware([NotNull] RequestDelegate next)
30+
/// <param name="loggerFactory">An <see cref="ILoggerFactory"/> instance used to create loggers.</param>
31+
public SendFileMiddleware([NotNull] RequestDelegate next, [NotNull] ILoggerFactory loggerFactory)
2932
{
3033
_next = next;
34+
_logger = loggerFactory.Create<SendFileMiddleware>();
3135
}
3236

3337
public Task Invoke(HttpContext context)
3438
{
3539
// Check if there is a SendFile feature already present
3640
if (context.GetFeature<IHttpSendFileFeature>() == null)
3741
{
38-
context.SetFeature<IHttpSendFileFeature>(new SendFileWrapper(context.Response.Body));
42+
context.SetFeature<IHttpSendFileFeature>(new SendFileWrapper(context.Response.Body, _logger));
3943
}
4044

4145
return _next(context);
@@ -44,10 +48,12 @@ public Task Invoke(HttpContext context)
4448
private class SendFileWrapper : IHttpSendFileFeature
4549
{
4650
private readonly Stream _output;
51+
private readonly ILogger _logger;
4752

48-
internal SendFileWrapper(Stream output)
53+
internal SendFileWrapper(Stream output, ILogger logger)
4954
{
5055
_output = output;
56+
_logger = logger;
5157
}
5258

5359
// Not safe for overlapped writes.
@@ -86,6 +92,10 @@ public async Task SendFileAsync(string fileName, long offset, long? length, Canc
8692
try
8793
{
8894
fileStream.Seek(offset, SeekOrigin.Begin);
95+
if (_logger.IsEnabled(LogLevel.Verbose))
96+
{
97+
_logger.WriteVerbose(string.Format("Copying bytes {0}-{1} of file {2} to response body", offset, length != null ? (offset + length).ToString() : "*", fileName));
98+
}
8999
await StreamCopyOperation.CopyToAsync(fileStream, _output, length, cancel);
90100
}
91101
finally

src/Microsoft.AspNet.StaticFiles/StaticFileContext.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.AspNet.Http;
1212
using Microsoft.AspNet.HttpFeature;
1313
using Microsoft.AspNet.StaticFiles.Infrastructure;
14+
using Microsoft.Framework.Logging;
1415

1516
namespace Microsoft.AspNet.StaticFiles
1617
{
@@ -21,6 +22,7 @@ internal struct StaticFileContext
2122
private readonly PathString _matchUrl;
2223
private readonly HttpRequest _request;
2324
private readonly HttpResponse _response;
25+
private readonly ILogger _logger;
2426
private string _method;
2527
private bool _isGet;
2628
private bool _isHead;
@@ -40,13 +42,14 @@ internal struct StaticFileContext
4042

4143
private IList<Tuple<long, long>> _ranges;
4244

43-
public StaticFileContext(HttpContext context, StaticFileOptions options, PathString matchUrl)
45+
public StaticFileContext(HttpContext context, StaticFileOptions options, PathString matchUrl, ILogger logger)
4446
{
4547
_context = context;
4648
_options = options;
4749
_matchUrl = matchUrl;
4850
_request = context.Request;
4951
_response = context.Response;
52+
_logger = logger;
5053

5154
_method = null;
5255
_isGet = false;
@@ -83,6 +86,11 @@ public bool IsRangeRequest
8386
{
8487
get { return _ranges != null; }
8588
}
89+
90+
public string SubPath
91+
{
92+
get { return _subPath.Value; }
93+
}
8694

8795
public bool ValidateMethod()
8896
{
@@ -220,6 +228,7 @@ private void ComputeRange()
220228
if (ranges.Count > 1)
221229
{
222230
// multiple range headers not yet supported
231+
_logger.WriteWarning("Multiple range headers not yet supported, {0} ranges in header", ranges.Count.ToString());
223232
return;
224233
}
225234

@@ -308,6 +317,10 @@ public Task SendStatusAsync(int statusCode)
308317
{
309318
ApplyResponseHeaders(statusCode);
310319

320+
if (_logger.IsEnabled(LogLevel.Verbose))
321+
{
322+
_logger.WriteVerbose(string.Format("Handled. Status code: {0} File: {1}", statusCode, SubPath));
323+
}
311324
return Constants.CompletedTask;
312325
}
313326

@@ -350,6 +363,7 @@ internal async Task SendRangeAsync()
350363
// the current length of the selected resource. e.g. */length
351364
_response.Headers[Constants.ContentRange] = "bytes */" + _length.ToString(CultureInfo.InvariantCulture);
352365
ApplyResponseHeaders(Constants.Status416RangeNotSatisfiable);
366+
_logger.WriteWarning("Range not satisfiable for {0}", SubPath);
353367
return;
354368
}
355369

@@ -365,6 +379,10 @@ internal async Task SendRangeAsync()
365379
var sendFile = _context.GetFeature<IHttpSendFileFeature>();
366380
if (sendFile != null && !string.IsNullOrEmpty(physicalPath))
367381
{
382+
if (_logger.IsEnabled(LogLevel.Verbose))
383+
{
384+
_logger.WriteVerbose(string.Format("Sending {0} of file {1}", _response.Headers[Constants.ContentRange], physicalPath));
385+
}
368386
await sendFile.SendFileAsync(physicalPath, start, length, _context.RequestAborted);
369387
return;
370388
}
@@ -373,6 +391,10 @@ internal async Task SendRangeAsync()
373391
try
374392
{
375393
readStream.Seek(start, SeekOrigin.Begin); // TODO: What if !CanSeek?
394+
if (_logger.IsEnabled(LogLevel.Verbose))
395+
{
396+
_logger.WriteVerbose(string.Format("Copying {0} of file {1} to the response body", _response.Headers[Constants.ContentRange], SubPath));
397+
}
376398
await StreamCopyOperation.CopyToAsync(readStream, _response.Body, length, _context.RequestAborted);
377399
}
378400
finally

src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNet.FileSystems;
88
using Microsoft.AspNet.Hosting;
99
using Microsoft.AspNet.Http;
10+
using Microsoft.Framework.Logging;
1011

1112
namespace Microsoft.AspNet.StaticFiles
1213
{
@@ -18,13 +19,15 @@ public class StaticFileMiddleware
1819
private readonly StaticFileOptions _options;
1920
private readonly PathString _matchUrl;
2021
private readonly RequestDelegate _next;
22+
private readonly ILogger _logger;
2123

2224
/// <summary>
2325
/// Creates a new instance of the StaticFileMiddleware.
2426
/// </summary>
2527
/// <param name="next">The next middleware in the pipeline.</param>
2628
/// <param name="options">The configuration options.</param>
27-
public StaticFileMiddleware([NotNull] RequestDelegate next, [NotNull] IHostingEnvironment hostingEnv, [NotNull] StaticFileOptions options)
29+
/// <param name="loggerFactory">An <see cref="ILoggerFactory"/> instance used to create loggers.</param>
30+
public StaticFileMiddleware([NotNull] RequestDelegate next, [NotNull] IHostingEnvironment hostingEnv, [NotNull] StaticFileOptions options, [NotNull] ILoggerFactory loggerFactory)
2831
{
2932
if (options.ContentTypeProvider == null)
3033
{
@@ -35,6 +38,7 @@ public StaticFileMiddleware([NotNull] RequestDelegate next, [NotNull] IHostingEn
3538
_next = next;
3639
_options = options;
3740
_matchUrl = options.RequestPath;
41+
_logger = loggerFactory.Create<StaticFileMiddleware>();
3842
}
3943

4044
/// <summary>
@@ -44,7 +48,7 @@ public StaticFileMiddleware([NotNull] RequestDelegate next, [NotNull] IHostingEn
4448
/// <returns></returns>
4549
public Task Invoke(HttpContext context)
4650
{
47-
var fileContext = new StaticFileContext(context, _options, _matchUrl);
51+
var fileContext = new StaticFileContext(context, _options, _matchUrl, _logger);
4852
if (fileContext.ValidateMethod()
4953
&& fileContext.ValidatePath()
5054
&& fileContext.LookupContentType()
@@ -64,16 +68,26 @@ public Task Invoke(HttpContext context)
6468
{
6569
return fileContext.SendRangeAsync();
6670
}
71+
if (_logger.IsEnabled(LogLevel.Verbose))
72+
{
73+
_logger.WriteVerbose(string.Format("Copying file {0} to the response body", fileContext.SubPath));
74+
}
6775
return fileContext.SendAsync();
6876

6977
case StaticFileContext.PreconditionState.NotModified:
78+
if (_logger.IsEnabled(LogLevel.Verbose))
79+
{
80+
_logger.WriteVerbose(string.Format("{0} not modified", fileContext.SubPath));
81+
}
7082
return fileContext.SendStatusAsync(Constants.Status304NotModified);
7183

7284
case StaticFileContext.PreconditionState.PreconditionFailed:
7385
return fileContext.SendStatusAsync(Constants.Status412PreconditionFailed);
7486

7587
default:
76-
throw new NotImplementedException(fileContext.GetPreconditionState().ToString());
88+
var exception = new NotImplementedException(fileContext.GetPreconditionState().ToString());
89+
_logger.WriteError("No precondition state specified", exception);
90+
throw exception;
7791
}
7892
}
7993

src/Microsoft.AspNet.StaticFiles/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"Microsoft.AspNet.Hosting": { "version": "1.0.0-*", "type": "build" },
77
"Microsoft.AspNet.Http": "1.0.0-*",
88
"Microsoft.AspNet.Http.Extensions": "1.0.0-*",
9-
"Microsoft.AspNet.HttpFeature": { "version": "1.0.0-*", "type": "build" }
9+
"Microsoft.AspNet.HttpFeature": { "version": "1.0.0-*", "type": "build" },
10+
"Microsoft.Framework.Logging": "1.0.0-*"
1011
},
1112
"frameworks": {
1213
"aspnet50": { },

0 commit comments

Comments
 (0)