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

Commit 5f168e3

Browse files
author
Victor Hurdugaci
committed
Improve logging and add more messages
1 parent 4310fa1 commit 5f168e3

File tree

4 files changed

+87
-14
lines changed

4 files changed

+87
-14
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
namespace Microsoft.AspNet.StaticFiles
4+
{
5+
internal static class LoggingEventIds
6+
{
7+
public const int PathNotModified = 1;
8+
public const int CopyingFileToBody = 2;
9+
public const int NoPreconditionState = 3;
10+
public const int CopyingBytesToResponse = 4;
11+
public const int Handled = 5;
12+
public const int SendingFileRange = 6;
13+
public const int CopyingFileRange = 7;
14+
public const int MultipleFileRanges = 8;
15+
public const int RangeNotSatisfiable = 9;
16+
public const int FileServed = 10;
17+
public const int PreconditionFailed = 11;
18+
public const int MethodMismatch = 12;
19+
public const int PathMismatch = 13;
20+
public const int ContentMismatch = 14;
21+
public const int FileNotFound = 15;
22+
}
23+
}

src/Microsoft.AspNet.StaticFiles/SendFileMiddleware.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ public async Task SendFileAsync(string fileName, long offset, long? length, Canc
105105
fileStream.Seek(offset, SeekOrigin.Begin);
106106
if (_logger.IsEnabled(LogLevel.Verbose))
107107
{
108-
_logger.LogVerbose(string.Format("Copying bytes {0}-{1} of file {2} to response body", offset, length != null ? (offset + length).ToString() : "*", fileName));
108+
_logger.LogVerbose(
109+
LoggingEventIds.CopyingBytesToResponse,
110+
string.Format("Copying bytes {0}-{1} of file {2} to response body", offset, length != null ? (offset + length).ToString() : "*", fileName));
109111
}
110112
await StreamCopyOperation.CopyToAsync(fileStream, _output, length, cancel);
111113
}

src/Microsoft.AspNet.StaticFiles/StaticFileContext.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ internal enum PreconditionState
8080
PreconditionFailed,
8181
}
8282

83+
public string ContentType
84+
{
85+
get { return _contentType; }
86+
}
87+
8388
public bool IsHeadMethod
8489
{
8590
get { return _isHead; }
@@ -226,7 +231,10 @@ private void ComputeRange()
226231
// The spec allows for multiple ranges but we choose not to support them because the client may request
227232
// very strange ranges (e.g. each byte separately, overlapping ranges, etc.) that could negatively
228233
// impact the server. Ignore the header and serve the response normally.
229-
_logger.LogWarning("Multiple ranges are not allowed: '{0}'", rangeHeader.ToString());
234+
_logger.LogWarning(
235+
LoggingEventIds.MultipleFileRanges,
236+
"Multiple ranges are not allowed: '{0}'",
237+
rangeHeader.ToString());
230238
return;
231239
}
232240

@@ -314,7 +322,7 @@ public Task SendStatusAsync(int statusCode)
314322

315323
if (_logger.IsEnabled(LogLevel.Verbose))
316324
{
317-
_logger.LogVerbose(string.Format("Handled. Status code: {0} File: {1}", statusCode, SubPath));
325+
_logger.LogVerbose(LoggingEventIds.Handled, string.Format("Handled. Status code: {0} File: {1}", statusCode, SubPath));
318326
}
319327
return Constants.CompletedTask;
320328
}
@@ -358,7 +366,10 @@ internal async Task SendRangeAsync()
358366
// the current length of the selected resource. e.g. */length
359367
_responseHeaders.ContentRange = new ContentRangeHeaderValue(_length);
360368
ApplyResponseHeaders(Constants.Status416RangeNotSatisfiable);
361-
_logger.LogWarning("Range not satisfiable for {0}", SubPath);
369+
_logger.LogWarning(
370+
LoggingEventIds.RangeNotSatisfiable,
371+
"Range not satisfiable for {0}",
372+
SubPath);
362373
return;
363374
}
364375

@@ -376,7 +387,9 @@ internal async Task SendRangeAsync()
376387
{
377388
if (_logger.IsEnabled(LogLevel.Verbose))
378389
{
379-
_logger.LogVerbose(string.Format("Sending {0} of file {1}", _response.Headers[HeaderNames.ContentRange], physicalPath));
390+
_logger.LogVerbose(
391+
LoggingEventIds.SendingFileRange,
392+
string.Format("Sending {0} of file {1}", _response.Headers[HeaderNames.ContentRange], physicalPath));
380393
}
381394
await sendFile.SendFileAsync(physicalPath, start, length, _context.RequestAborted);
382395
return;
@@ -388,7 +401,9 @@ internal async Task SendRangeAsync()
388401
readStream.Seek(start, SeekOrigin.Begin); // TODO: What if !CanSeek?
389402
if (_logger.IsEnabled(LogLevel.Verbose))
390403
{
391-
_logger.LogVerbose(string.Format("Copying {0} of file {1} to the response body", _response.Headers[HeaderNames.ContentRange], SubPath));
404+
_logger.LogVerbose(
405+
LoggingEventIds.CopyingFileRange,
406+
string.Format("Copying {0} of file {1} to the response body", _response.Headers[HeaderNames.ContentRange], SubPath));
392407
}
393408
await StreamCopyOperation.CopyToAsync(readStream, _response.Body, length, _context.RequestAborted);
394409
}

src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,38 @@ public StaticFileMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv
6868
public Task Invoke(HttpContext context)
6969
{
7070
var fileContext = new StaticFileContext(context, _options, _matchUrl, _logger);
71-
if (fileContext.ValidateMethod()
72-
&& fileContext.ValidatePath()
73-
&& fileContext.LookupContentType()
74-
&& fileContext.LookupFileInfo())
71+
72+
if (!fileContext.ValidateMethod())
7573
{
74+
if (_logger.IsEnabled(LogLevel.Verbose))
75+
{
76+
_logger.LogVerbose(LoggingEventIds.MethodMismatch, $"{context.Request.Method} requests are not supported");
77+
}
78+
}
79+
else if (!fileContext.ValidatePath())
80+
{
81+
if (_logger.IsEnabled(LogLevel.Verbose))
82+
{
83+
_logger.LogVerbose(LoggingEventIds.PathMismatch, $"The path {fileContext.SubPath} doesn't match the path filter");
84+
}
85+
}
86+
else if (!fileContext.LookupContentType())
87+
{
88+
if (_logger.IsEnabled(LogLevel.Verbose))
89+
{
90+
_logger.LogVerbose(LoggingEventIds.ContentMismatch, $"The path {fileContext.SubPath} is not for a supported file type");
91+
}
92+
}
93+
else if (!fileContext.LookupFileInfo())
94+
{
95+
if (_logger.IsEnabled(LogLevel.Verbose))
96+
{
97+
_logger.LogVerbose(LoggingEventIds.FileNotFound, $"The path {fileContext.SubPath} doesn't match an existing file");
98+
}
99+
}
100+
else
101+
{
102+
// If we get here, we can try to serve the file
76103
fileContext.ComprehendRequestHeaders();
77104

78105
switch (fileContext.GetPreconditionState())
@@ -87,25 +114,31 @@ public Task Invoke(HttpContext context)
87114
{
88115
return fileContext.SendRangeAsync();
89116
}
90-
if (_logger.IsEnabled(LogLevel.Verbose))
117+
if (_logger.IsEnabled(LogLevel.Debug))
91118
{
92-
_logger.LogVerbose(string.Format("Copying file {0} to the response body", fileContext.SubPath));
119+
_logger.LogDebug(LoggingEventIds.CopyingFileToBody, string.Format("Copying file {0} to the response body", fileContext.SubPath));
93120
}
121+
122+
_logger.LogInformation(LoggingEventIds.FileServed, $"Sending file {fileContext.SubPath}");
94123
return fileContext.SendAsync();
95124

96125
case StaticFileContext.PreconditionState.NotModified:
97126
if (_logger.IsEnabled(LogLevel.Verbose))
98127
{
99-
_logger.LogVerbose(string.Format("{0} not modified", fileContext.SubPath));
128+
_logger.LogVerbose(LoggingEventIds.PathNotModified, string.Format("The file {0} was not modified", fileContext.SubPath));
100129
}
101130
return fileContext.SendStatusAsync(Constants.Status304NotModified);
102131

103132
case StaticFileContext.PreconditionState.PreconditionFailed:
133+
if (_logger.IsEnabled(LogLevel.Verbose))
134+
{
135+
_logger.LogVerbose(LoggingEventIds.PreconditionFailed, string.Format("{0} precondition failed", fileContext.SubPath));
136+
}
104137
return fileContext.SendStatusAsync(Constants.Status412PreconditionFailed);
105138

106139
default:
107140
var exception = new NotImplementedException(fileContext.GetPreconditionState().ToString());
108-
_logger.LogError("No precondition state specified", exception);
141+
_logger.LogError(LoggingEventIds.NoPreconditionState, "No precondition state specified", exception);
109142
throw exception;
110143
}
111144
}

0 commit comments

Comments
 (0)