Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #74 from KieranSasseSolarGain/RequestTimeLogLevel
Browse files Browse the repository at this point in the history
Request time log level
  • Loading branch information
tsimbalar authored Apr 20, 2021
2 parents f4cea2b + b4ef2d5 commit f15beac
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
19 changes: 9 additions & 10 deletions src/SerilogWeb.Classic/Classic/ApplicationLifecycleModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static ILogger Logger
get => Config.Logger;
set => SerilogWebClassic.Configuration = new SerilogWebClassicConfiguration(
isEnabled: Config.IsEnabled,
requestLoggingLevel: Config.RequestLoggingLevel,
logLevelEvaluator: Config.LogLevelEvaluator,
requestFilter: Config.RequestFilter,
formDataLoggingLevel: Config.FormDataLoggingLevel,
customLogger: value,
Expand Down Expand Up @@ -71,7 +71,7 @@ public static Func<HttpContextBase, bool> RequestFilter
if (value == null) throw new ArgumentNullException(nameof(value));
SerilogWebClassic.Configuration = new SerilogWebClassicConfiguration(
isEnabled: Config.IsEnabled,
requestLoggingLevel: Config.RequestLoggingLevel,
logLevelEvaluator: Config.LogLevelEvaluator,
requestFilter: value,
formDataLoggingLevel: Config.FormDataLoggingLevel,
customLogger: Config.CustomLogger,
Expand All @@ -98,7 +98,7 @@ public static LogPostedFormDataOption LogPostedFormData
get => Config.LogPostedFormData;
set => SerilogWebClassic.Configuration = new SerilogWebClassicConfiguration(
isEnabled: Config.IsEnabled,
requestLoggingLevel: Config.RequestLoggingLevel,
logLevelEvaluator: Config.LogLevelEvaluator,
requestFilter: Config.RequestFilter,
formDataLoggingLevel: Config.FormDataLoggingLevel,
customLogger: Config.CustomLogger,
Expand All @@ -119,7 +119,7 @@ public static bool FilterPasswordsInFormData
get => Config.FilterPasswordsInFormData;
set => SerilogWebClassic.Configuration = new SerilogWebClassicConfiguration(
isEnabled: Config.IsEnabled,
requestLoggingLevel: Config.RequestLoggingLevel,
logLevelEvaluator: Config.LogLevelEvaluator,
requestFilter: Config.RequestFilter,
formDataLoggingLevel: Config.FormDataLoggingLevel,
customLogger: Config.CustomLogger,
Expand All @@ -140,7 +140,7 @@ public static IEnumerable<String> FilteredKeywordsInFormData
get => Config.FilteredKeywordsInFormData;
set => SerilogWebClassic.Configuration = new SerilogWebClassicConfiguration(
isEnabled: Config.IsEnabled,
requestLoggingLevel: Config.RequestLoggingLevel,
logLevelEvaluator: Config.LogLevelEvaluator,
requestFilter: Config.RequestFilter,
formDataLoggingLevel: Config.FormDataLoggingLevel,
customLogger: Config.CustomLogger,
Expand All @@ -161,7 +161,7 @@ public static bool IsEnabled
get => Config.IsEnabled;
set => SerilogWebClassic.Configuration = new SerilogWebClassicConfiguration(
isEnabled: value,
requestLoggingLevel: Config.RequestLoggingLevel,
logLevelEvaluator: Config.LogLevelEvaluator,
requestFilter: Config.RequestFilter,
formDataLoggingLevel: Config.FormDataLoggingLevel,
customLogger: Config.CustomLogger,
Expand All @@ -178,10 +178,9 @@ public static bool IsEnabled
[Obsolete("Obsolete since v4.1 - Use SerilogWebClassic.Configure(cfg => cfg.LogAtLevel(level))")]
public static LogEventLevel RequestLoggingLevel
{
get => Config.RequestLoggingLevel;
set => SerilogWebClassic.Configuration = new SerilogWebClassicConfiguration(
isEnabled: Config.IsEnabled,
requestLoggingLevel: value,
logLevelEvaluator: (httpContext, elapsed) => value,
requestFilter: Config.RequestFilter,
formDataLoggingLevel: Config.FormDataLoggingLevel,
customLogger: Config.CustomLogger,
Expand All @@ -202,7 +201,7 @@ public static LogEventLevel FormDataLoggingLevel
get => Config.FormDataLoggingLevel;
set => SerilogWebClassic.Configuration = new SerilogWebClassicConfiguration(
isEnabled: Config.IsEnabled,
requestLoggingLevel: Config.RequestLoggingLevel,
logLevelEvaluator: Config.LogLevelEvaluator,
requestFilter: Config.RequestFilter,
formDataLoggingLevel: value,
customLogger: Config.CustomLogger,
Expand All @@ -227,7 +226,7 @@ public static Func<HttpContextBase, bool> ShouldLogPostedFormData
if (value == null) throw new ArgumentNullException(nameof(value));
SerilogWebClassic.Configuration = new SerilogWebClassicConfiguration(
isEnabled: Config.IsEnabled,
requestLoggingLevel: Config.RequestLoggingLevel,
logLevelEvaluator: Config.LogLevelEvaluator,
requestFilter: Config.RequestFilter,
formDataLoggingLevel: Config.FormDataLoggingLevel,
customLogger: Config.CustomLogger,
Expand Down
10 changes: 5 additions & 5 deletions src/SerilogWeb.Classic/Classic/SerilogWebClassicConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal sealed class SerilogWebClassicConfiguration

internal SerilogWebClassicConfiguration(
bool isEnabled,
LogEventLevel requestLoggingLevel,
Func<HttpContextBase, TimeSpan, LogEventLevel> logLevelEvaluator,
Func<HttpContextBase, bool> requestFilter,
LogEventLevel formDataLoggingLevel,
ILogger customLogger,
Expand All @@ -28,7 +28,7 @@ internal SerilogWebClassicConfiguration(
IEnumerable<string> filteredKeywordsInFormData)
{
IsEnabled = isEnabled;
RequestLoggingLevel = requestLoggingLevel;
LogLevelEvaluator = logLevelEvaluator;
RequestFilter = requestFilter;
FormDataLoggingLevel = formDataLoggingLevel;
CustomLogger = customLogger;
Expand Down Expand Up @@ -65,7 +65,7 @@ internal SerilogWebClassicConfiguration Edit(Func<SerilogWebClassicConfiguration

internal bool IsEnabled { get; }

internal LogEventLevel RequestLoggingLevel { get; }
internal Func<HttpContextBase, TimeSpan, LogEventLevel> LogLevelEvaluator { get; }
internal ILogger CustomLogger { get; }
internal Func<HttpContextBase, bool> RequestFilter { get; }

Expand All @@ -78,10 +78,10 @@ internal SerilogWebClassicConfiguration Edit(Func<SerilogWebClassicConfiguration

internal ILogger Logger => (CustomLogger ?? Log.Logger).ForContext<ApplicationLifecycleModule>();


internal Func<HttpContextBase, bool> FormLoggingStrategy { get; }


/// <summary>
/// Filters configured keywords from being logged
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class SerilogWebClassicConfigurationBuilder

private bool IsEnabled { get; set; } = true;

private LogEventLevel RequestLoggingLevel { get; set; }
private Func<HttpContextBase, TimeSpan, LogEventLevel> LogLevelEvaluator { get; set; }
private ILogger CustomLogger { get; set; }
private Func<HttpContextBase, bool> RequestFilter { get; set; }

Expand All @@ -38,7 +38,7 @@ internal SerilogWebClassicConfigurationBuilder(SerilogWebClassicConfiguration co
if (configToCopy == null) throw new ArgumentNullException(nameof(configToCopy));
CustomLogger = configToCopy.CustomLogger;
IsEnabled = configToCopy.IsEnabled;
RequestLoggingLevel = configToCopy.RequestLoggingLevel;
LogLevelEvaluator = configToCopy.LogLevelEvaluator;
RequestFilter = configToCopy.RequestFilter;
FormDataLoggingLevel = configToCopy.FormDataLoggingLevel;
LogPostedFormData = configToCopy.LogPostedFormData;
Expand All @@ -51,7 +51,7 @@ private void Reset()
{
CustomLogger = null;
IsEnabled = true;
RequestLoggingLevel = LogEventLevel.Information;
LogLevelEvaluator = (ctx, elapsed) => LogEventLevel.Information;
RequestFilter = AlwaysFalse;
ResetFormDataLogging();
}
Expand All @@ -69,7 +69,7 @@ internal SerilogWebClassicConfiguration Build()
{
return new SerilogWebClassicConfiguration(
isEnabled: IsEnabled,
requestLoggingLevel: RequestLoggingLevel,
logLevelEvaluator: LogLevelEvaluator,
requestFilter: RequestFilter,
formDataLoggingLevel: FormDataLoggingLevel,
customLogger: CustomLogger,
Expand Down Expand Up @@ -109,7 +109,18 @@ public SerilogWebClassicConfigurationBuilder Enable()
/// <returns>A configuration object to allow chaining</returns>
public SerilogWebClassicConfigurationBuilder LogAtLevel(LogEventLevel level)
{
RequestLoggingLevel = level;
LogLevelEvaluator = (ctx, elapsed) => level;
return this;
}

/// <summary>
/// Configure at which level HTTP requests are logged dynamically depending on the context
/// </summary>
/// <param name="logLevelEvaluator">Set the log level based on the current http context and total request time</param>
/// <returns>A configuration object to allow chaining</returns>
public SerilogWebClassicConfigurationBuilder LogAtLevel(Func<HttpContextBase, TimeSpan, LogEventLevel> logLevelEvaluator)
{
LogLevelEvaluator = logLevelEvaluator;
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/SerilogWeb.Classic/Classic/WebRequestLoggingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal void OnLogRequest(SerilogWebClassicConfiguration configuration)

var error = _application.Context.GetLastSerilogWebError() ?? _application.Server.GetLastError();

var level = error != null || _application.Response.StatusCode >= 500 ? LogEventLevel.Error : configuration.RequestLoggingLevel;
var level = error != null || _application.Response.StatusCode >= 500 ? LogEventLevel.Error : configuration.LogLevelEvaluator(_application.Context, stopwatch.Elapsed);

if (level == LogEventLevel.Error && error == null && _application.Context.AllErrors != null)
{
Expand Down
34 changes: 34 additions & 0 deletions test/SerilogWeb.Classic.Tests/WebRequestLoggingHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,40 @@ public void RequestLoggingLevel(LogEventLevel requestLoggingLevel)
Assert.Equal(requestLoggingLevel, evt.Level);
}

[Theory]
[InlineData(LogEventLevel.Verbose)]
[InlineData(LogEventLevel.Debug)]
[InlineData(LogEventLevel.Information)]
[InlineData(LogEventLevel.Warning)]
[InlineData(LogEventLevel.Error)]
[InlineData(LogEventLevel.Fatal)]
public void RequestLogLevelEvaluator(LogEventLevel requestLoggingLevel)
{
SerilogWebClassic.Configure(cfg => cfg
.LogAtLevel((context, elapsed) => requestLoggingLevel)
);

TestContext.SimulateRequest();

var evt = LastEvent;
Assert.NotNull(evt);
Assert.Equal(requestLoggingLevel, evt.Level);
}

[Fact]
public void DynamicRequestLogLevelEvaluator()
{
SerilogWebClassic.Configure(cfg => cfg
.LogAtLevel((context, elapsed) => elapsed.TotalMilliseconds > 3000 ? LogEventLevel.Warning : LogEventLevel.Information)
);

TestContext.SimulateRequest(sleepDurationMilliseconds: 4000);

var evt = LastEvent;
Assert.NotNull(evt);
Assert.Equal(LogEventLevel.Warning, evt.Level);
}

[Fact]
public void LogPostedFormData()
{
Expand Down

0 comments on commit f15beac

Please sign in to comment.