diff --git a/src/Microsoft.AspNet.Session/DistributedSession.cs b/src/Microsoft.AspNet.Session/DistributedSession.cs index 134dfcbe47..04c2c3aed1 100644 --- a/src/Microsoft.AspNet.Session/DistributedSession.cs +++ b/src/Microsoft.AspNet.Session/DistributedSession.cs @@ -139,7 +139,7 @@ namespace Microsoft.AspNet.Session } else if (!_isNewSessionKey) { - _logger.LogWarning("Accessing expired session {0}", _sessionId); + _logger.AccessingExpiredSession(_sessionId); } _loaded = true; } @@ -152,7 +152,7 @@ namespace Microsoft.AspNet.Session var data = await _cache.GetAsync(_sessionId); if (_logger.IsEnabled(LogLevel.Information) && data == null) { - _logger.LogInformation("Session {0} started", _sessionId); + _logger.SessionStarted(_sessionId); } _isModified = false; diff --git a/src/Microsoft.AspNet.Session/LoggingExtensions.cs b/src/Microsoft.AspNet.Session/LoggingExtensions.cs new file mode 100644 index 0000000000..d9cc0b8654 --- /dev/null +++ b/src/Microsoft.AspNet.Session/LoggingExtensions.cs @@ -0,0 +1,45 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; + +namespace Microsoft.Extensions.Logging +{ + internal static class LoggingExtensions + { + private static Action _errorClosingTheSession; + private static Action _accessingExpiredSession; + private static Action _sessionStarted; + + static LoggingExtensions() + { + _errorClosingTheSession = LoggerMessage.Define( + eventId: 1, + logLevel: LogLevel.Error, + formatString: "Error closing the session."); + _accessingExpiredSession = LoggerMessage.Define( + eventId: 2, + logLevel: LogLevel.Warning, + formatString: "Accessing expired session {SessionId}"); + _sessionStarted = LoggerMessage.Define( + eventId: 3, + logLevel: LogLevel.Information, + formatString: "Session {SessionId} started"); + } + + public static void ErrorClosingTheSession(this ILogger logger, Exception exception) + { + _errorClosingTheSession(logger, exception); + } + + public static void AccessingExpiredSession(this ILogger logger, string sessionId) + { + _accessingExpiredSession(logger, sessionId, null); + } + + public static void SessionStarted(this ILogger logger, string sessionId) + { + _sessionStarted(logger, sessionId, null); + } + } +} diff --git a/src/Microsoft.AspNet.Session/SessionMiddleware.cs b/src/Microsoft.AspNet.Session/SessionMiddleware.cs index 1abdcf4db4..b0ce4444ff 100644 --- a/src/Microsoft.AspNet.Session/SessionMiddleware.cs +++ b/src/Microsoft.AspNet.Session/SessionMiddleware.cs @@ -106,7 +106,7 @@ namespace Microsoft.AspNet.Session } catch (Exception ex) { - _logger.LogError("Error closing the session.", ex); + _logger.ErrorClosingTheSession(ex); } } }