Adding logging event ids

This commit is contained in:
Brennan 2015-12-14 14:56:18 -08:00
parent 3b895f8a70
commit 09278f7013
3 changed files with 48 additions and 3 deletions

View File

@ -139,7 +139,7 @@ namespace Microsoft.AspNet.Session
} }
else if (!_isNewSessionKey) else if (!_isNewSessionKey)
{ {
_logger.LogWarning("Accessing expired session {0}", _sessionId); _logger.AccessingExpiredSession(_sessionId);
} }
_loaded = true; _loaded = true;
} }
@ -152,7 +152,7 @@ namespace Microsoft.AspNet.Session
var data = await _cache.GetAsync(_sessionId); var data = await _cache.GetAsync(_sessionId);
if (_logger.IsEnabled(LogLevel.Information) && data == null) if (_logger.IsEnabled(LogLevel.Information) && data == null)
{ {
_logger.LogInformation("Session {0} started", _sessionId); _logger.SessionStarted(_sessionId);
} }
_isModified = false; _isModified = false;

View File

@ -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<ILogger, Exception> _errorClosingTheSession;
private static Action<ILogger, string, Exception> _accessingExpiredSession;
private static Action<ILogger, string, Exception> _sessionStarted;
static LoggingExtensions()
{
_errorClosingTheSession = LoggerMessage.Define(
eventId: 1,
logLevel: LogLevel.Error,
formatString: "Error closing the session.");
_accessingExpiredSession = LoggerMessage.Define<string>(
eventId: 2,
logLevel: LogLevel.Warning,
formatString: "Accessing expired session {SessionId}");
_sessionStarted = LoggerMessage.Define<string>(
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);
}
}
}

View File

@ -106,7 +106,7 @@ namespace Microsoft.AspNet.Session
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError("Error closing the session.", ex); _logger.ErrorClosingTheSession(ex);
} }
} }
} }