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

Adding Logging #65

Closed
wants to merge 0 commits into from
Closed

Adding Logging #65

wants to merge 0 commits into from

Conversation

JunTaoLuo
Copy link
Contributor

@JunTaoLuo JunTaoLuo commented Oct 3, 2016

#42 Adding logging to response caching

cc @BrennanConroy @Tratcher

return false;
}

// Request allows stale values
if (age < context.RequestCacheControlHeaderValue.MaxStaleLimit)
var maxStaleLimit = context.RequestCacheControlHeaderValue.MaxStaleLimit;
if (maxStaleLimit.HasValue && age - lowestMaxAge < maxStaleLimit)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Misread the RFC previously, the stale limit is the difference between current age and the specified max-age.

return true;
}

if (cachedResponseHeaders.ETag != null)
{
foreach (var tag in ifNoneMatchHeader)
{
if (cachedResponseHeaders.ETag.Compare(tag, useStrongComparison: true))
if (cachedResponseHeaders.ETag.Compare(tag, useStrongComparison: false))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Misread the RFC previously, the comparison should be a weak comparison.

Copy link

@troydai troydai Oct 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to have this change singled out in a separate commit.

formatString: "A valid cached response was retrieved for this request. The cached response will be served.");
_log504GatewayTimeoutServed = LoggerMessage.Define(
logLevel: LogLevel.Information,
eventId: 23,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

TestUtils.AssertLoggedMessageContains(
sink.Writes[0],
LogLevel.Debug,
"Retrieving cache entry for base key");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it's probably better to use eventIDs instead of string matches. More specific and less verbose

eventId: 26,
formatString: "No valid response was retrieved for this request.");
_logVaryByRulesStored = LoggerMessage.Define<string>(
logLevel: LogLevel.Information,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an internal detail. Will set to Debug level

_log304NotModifiedServed = LoggerMessage.Define(
logLevel: LogLevel.Information,
eventId: 23,
formatString: $"The content requested is not modified. A {StatusCodes.Status304NotModified} {ReasonPhrases.GetReasonPhrase(StatusCodes.Status304NotModified)} response will be served.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The content requested has not been modified."
The 304 will be logged by hosting.

_logCachedResponseServed = LoggerMessage.Define(
logLevel: LogLevel.Information,
eventId: 24,
formatString: "A valid cached response was retrieved for this request. The cached response will be served.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Serving response from cache."

_log504GatewayTimeoutServed = LoggerMessage.Define(
logLevel: LogLevel.Information,
eventId: 25,
formatString: $"No valid response was retrieved for this request but a only-if-cached cache directive was specified. A {StatusCodes.Status504GatewayTimeout} {ReasonPhrases.GetReasonPhrase(StatusCodes.Status504GatewayTimeout)} response will be served.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The request could not be satisfied from cache and only-if-cached was specified."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"No cached response available for this request and the only-if-cached cache directive was specified."?

_logNoResponseServed = LoggerMessage.Define(
logLevel: LogLevel.Information,
eventId: 26,
formatString: "No valid response was retrieved for this request.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"No cached response available."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"No cached response available for this request."?

_logVaryByRulesStored = LoggerMessage.Define<string>(
logLevel: LogLevel.Information,
eventId: 27,
formatString: "Vary by rules were updated at key {Key}.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't give the key, give the components.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I purge the keys from the messages logged during retrieval as well? Those are debug levels so maybe it doesn't hurt to keep them?

_logResponseStored = LoggerMessage.Define<string>(
logLevel: LogLevel.Information,
eventId: 28,
formatString: "The response for this request was stored at key {Key}.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The response has been stored in the cache."

_logResponseNotStored = LoggerMessage.Define(
logLevel: LogLevel.Information,
eventId: 29,
formatString: "The response could not be stored for this request.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The response could not be cached." Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of reasons, response was not deemed cacheable, buffering was disabled due to invalid operations or exceeding the maximum body length. This is a generic message but I might add more debug messages that can indicate why the response was not stored.

_logResponseContentLengthMismatchNotStored = LoggerMessage.Define(
logLevel: LogLevel.Information,
eventId: 30,
formatString: $"The response could not be stored for this request. The content length specified in the {HeaderNames.ContentLength} header did not match the length of the response.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning? Error? This is an app error, though not a cache error. "The response could not be cached because the Content-Length did not match the body length."

_logRequestMethodNotCacheable = LoggerMessage.Define<string>(
logLevel: LogLevel.Debug,
eventId: 1,
formatString: "Request is not cacheable since it is a {Method} HTTP Method.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

due to the HTTP method: {Method}

_logRequestWithAuthorizationNotCacheable = LoggerMessage.Define(
logLevel: LogLevel.Debug,
eventId: 2,
formatString: $"Request is not cacheable since it contains an {HeaderNames.Authorization} header.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it contains an...

_logRequestWithNoCacheNotCacheable = LoggerMessage.Define(
logLevel: LogLevel.Debug,
eventId: 3,
formatString: "Request is not cacheable since it contains a no-cache cache directive.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because

_logRequestWithPragmaNoCacheNotCacheable = LoggerMessage.Define(
logLevel: LogLevel.Debug,
eventId: 4,
formatString: "Request is not cacheable since it contains a no-cache pragma directive.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because

_logExpirationMinFreshAdded = LoggerMessage.Define<TimeSpan>(
logLevel: LogLevel.Debug,
eventId: 5,
formatString: "A requirement of minimum freshness of {Duration} specified by min-fresh is added.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a min-fresh requirement of {Duration}

@JunTaoLuo
Copy link
Contributor Author

🆙📅

Copy link
Member

@Tratcher Tratcher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

public HttpContext HttpContext { get; }

public ILogger Logger { get; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public?

_logRequestMethodNotCacheable = LoggerMessage.Define<string>(
logLevel: LogLevel.Debug,
eventId: 1,
formatString: "Request is not cacheable due to the HTTP method: {Method}.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'Request is not cacheable'... is the request ever cacheable? 'The request cannot be served from cache...'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea that might be better. I just went with the current name of the method that generates these logs, which is also a little misleading.

_logResponseNotStored = LoggerMessage.Define(
logLevel: LogLevel.Information,
eventId: 27,
formatString: "The response could not be stored for this request.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'cached'

_logResponseContentLengthMismatchNotStored = LoggerMessage.Define(
logLevel: LogLevel.Warning,
eventId: 28,
formatString: $"The response could not be stored for this request because the {HeaderNames.ContentLength} did not match the body length.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'cached'

@@ -24,9 +25,11 @@ public class ResponseCacheContext
private bool _parsedResponseExpires;

internal ResponseCacheContext(
HttpContext httpContext)
HttpContext httpContext,
ILogger logger)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is short enough to be kept on same line.

@@ -37,6 +40,8 @@ public class ResponseCacheContext

public CachedVaryByRules CachedVaryByRules { get; internal set; }

internal ILogger Logger { get; }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not public? after all the context itself is public which means it is open to application consumption.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

talked with @JunTaoLuo and @Tratcher, I bought the explanation. keep it internal then.

return true;
}

if (cachedResponseHeaders.ETag != null)
{
foreach (var tag in ifNoneMatchHeader)
{
if (cachedResponseHeaders.ETag.Compare(tag, useStrongComparison: true))
if (cachedResponseHeaders.ETag.Compare(tag, useStrongComparison: false))
Copy link

@troydai troydai Oct 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to have this change singled out in a separate commit.

_logExpirationMustRevalidate = LoggerMessage.Define<TimeSpan, TimeSpan>(
logLevel: LogLevel.Debug,
eventId: 7,
formatString: "The age of the entry is {Age} and has exceeded the maximum age of {MaxAge} specified by max-age. It must be revalidated becauses must-revalidate is specified.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

becauses

_logResponseWithoutPublicNotCacheable = LoggerMessage.Define(
logLevel: LogLevel.Debug,
eventId: 11,
formatString: "Response is not cacheable because it does not contain the public cache directive.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be "... the 'public' cache directive"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering how we format all the header names and cache directives. In a few middlewares I didn't see any formatting. Guess I'll use single quotes here.

@BrennanConroy
Copy link
Member

:shipit:

_logResponseCached = LoggerMessage.Define(
logLevel: LogLevel.Information,
eventId: 26,
formatString: "The response has been cached in the cache.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The response has been cached."

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants