Skip to content

Commit

Permalink
updated LogListenerParser to fix issue #19
Browse files Browse the repository at this point in the history
  • Loading branch information
catalin.gavan committed Oct 16, 2019
1 parent 3d9ed02 commit 4671608
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 35 deletions.
3 changes: 1 addition & 2 deletions src/KissLog.AspNetCore/IOptionsBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;

namespace KissLog.AspNetCore
{
public interface IOptionsBuilder
{
List<ILogListener> Listeners { get; }
ListenersContainer Listeners { get; }
Options Options { get; }
Action<string, LogLevel> InternalLog { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/KissLog.AspNetCore/OptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace KissLog.AspNetCore
{
internal class OptionsBuilder : IOptionsBuilder
{
public List<ILogListener> Listeners => KissLogConfiguration.Listeners;
public ListenersContainer Listeners => KissLogConfiguration.Listeners;
public Options Options => KissLogConfiguration.Options;
public Action<string, LogLevel> InternalLog
{
Expand Down
1 change: 1 addition & 0 deletions src/KissLog/Internal/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using KissLog.FlushArgs;
using KissLog.Web;

Expand Down
48 changes: 48 additions & 0 deletions src/KissLog/Internal/LogListenerDecorator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using KissLog.Web;
using System.Collections.Generic;

namespace KissLog.Internal
{
internal class LogListenerDecorator
{
public LogListenerDecorator(ILogListener listener)
{
Listener = listener;
}

public ILogListener Listener { get; private set; }
public List<string> SkipRequestIds = new List<string>();

public void Reset()
{
SkipRequestIds.Clear();
SkipRequestIds = new List<string>();
}

public bool ShouldSkipOnMessage(Logger logger)
{
string requestId = logger.DataContainer?.WebProperties?.Request?._KissLogRequestId;

if (string.IsNullOrEmpty(requestId))
return false;

if (SkipRequestIds.Contains(requestId))
return true;

return false;
}

public bool ShouldSkipOnFlush(HttpRequest request)
{
string requestId = request?._KissLogRequestId;

if (string.IsNullOrEmpty(requestId))
return false;

if (SkipRequestIds.Contains(requestId))
return true;

return false;
}
}
}
6 changes: 6 additions & 0 deletions src/KissLog/Internal/LoggerDataContainer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using KissLog.Web;
using System;
using System.Collections.Generic;
using System.Net;

Expand Down Expand Up @@ -66,6 +67,11 @@ public void Reset()

LoggerFiles.Dispose();
LoggerFiles = new LoggerFiles(_logger);

if (WebProperties?.Request != null)
{
WebProperties.Request._KissLogRequestId = Guid.NewGuid().ToString();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
using KissLog.Web;
using System.Linq;
using KissLog.FlushArgs;
using KissLog.Web;

namespace KissLog.Internal
{
internal static class NotifyOnBeginRequestService
{
public static void Notify(HttpRequest httpRequest, Logger logger)
{
if (KissLogConfiguration.Listeners == null || KissLogConfiguration.Listeners.Any() == false)
return;

foreach (ILogListener listener in KissLogConfiguration.Listeners)
foreach (LogListenerDecorator decorator in KissLogConfiguration.Listeners.Get())
{
if (listener == null)
ILogListener listener = decorator.Listener;

BeginRequestArgs args = new BeginRequestArgs
{
IsCreatedByHttpRequest = logger.IsCreatedByHttpRequest(),
Request = httpRequest
};

if (ShouldUseListener(listener, args) == false)
{
// make the listener skip all the events for the current request
decorator.SkipRequestIds.Add(args.Request._KissLogRequestId);

continue;
}

listener.OnBeginRequest(httpRequest, logger);
}
}

private static bool ShouldUseListener(ILogListener listener, BeginRequestArgs args)
{
LogListenerParser parser = listener.Parser;
if (parser == null)
return true;

return parser.ShouldLog(args, listener);
}
}
}
17 changes: 14 additions & 3 deletions src/KissLog/Internal/NotifyListeners/NotifyOnFlushService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void Notify(ILogger[] loggers)
if (loggers == null || !loggers.Any())
return;

if (KissLogConfiguration.Listeners == null || KissLogConfiguration.Listeners.Any() == false)
if (KissLogConfiguration.Listeners.Get().Any() == false)
return;

Logger[] theLoggers = loggers.OfType<Logger>().ToArray();
Expand All @@ -31,14 +31,20 @@ public static void Notify(ILogger[] loggers)

string defaultArgsJsonJson = JsonConvert.SerializeObject(defaultArgs);

foreach (ILogListener listener in KissLogConfiguration.Listeners)
foreach (LogListenerDecorator decorator in KissLogConfiguration.Listeners.Get())
{
ILogListener listener = decorator.Listener;

if (decorator.ShouldSkipOnFlush(defaultArgs.WebProperties.Request))
continue;

FlushLogArgs args = CreateFlushArgsForListener(defaultLogger, listener, defaultArgs, defaultArgsJsonJson, defaultFiles.ToList());

if (ShouldUseListener(listener, args) == false)
continue;

listener.Parser?.BeforeFlush(args, listener);
if(listener.Parser != null)
listener.Parser.BeforeFlush(args, listener);

listener.OnFlush(args, defaultLogger);
}
Expand All @@ -47,6 +53,11 @@ public static void Notify(ILogger[] loggers)
{
logger.Reset();
}

foreach (LogListenerDecorator decorator in KissLogConfiguration.Listeners.Get())
{
decorator.Reset();
}
}

internal static ArgsResult CreateArgs(ILogger[] loggers)
Expand Down
15 changes: 6 additions & 9 deletions src/KissLog/Internal/NotifyListeners/NotifyOnMessageService.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
using System.Linq;

namespace KissLog.Internal
namespace KissLog.Internal
{
internal static class NotifyOnMessageService
{
public static void Notify(LogMessage message, Logger logger)
{
if (KissLogConfiguration.Listeners == null || KissLogConfiguration.Listeners.Any() == false)
return;

foreach (ILogListener listener in KissLogConfiguration.Listeners)
foreach (LogListenerDecorator decorator in KissLogConfiguration.Listeners.Get())
{
if (listener == null)
ILogListener listener = decorator.Listener;

if (decorator.ShouldSkipOnMessage(logger))
continue;

if (listener.Parser != null && listener.Parser.ShouldLog(message, listener) == false)
continue;

listener.OnMessage(message, logger);
decorator.Listener.OnMessage(message, logger);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/KissLog/KissLogConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;

namespace KissLog
{
public static class KissLogConfiguration
{
public static List<ILogListener> Listeners = new List<ILogListener>();
public static ListenersContainer Listeners { get; } = new ListenersContainer();

public static Options Options { get; } = new Options();

Expand Down
35 changes: 35 additions & 0 deletions src/KissLog/ListenersContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using KissLog.Internal;
using System.Collections.Generic;

namespace KissLog
{
public class ListenersContainer
{
private readonly List<LogListenerDecorator> _listeners;
public ListenersContainer()
{
_listeners = new List<LogListenerDecorator>();
}

public void Add(ILogListener listener)
{
if (listener == null)
return;

var decorator = new LogListenerDecorator(listener);
_listeners.Add(decorator);
}

internal IList<LogListenerDecorator> Get()
{
List<LogListenerDecorator> result = new List<LogListenerDecorator>();

foreach(LogListenerDecorator decorator in _listeners)
{
result.Add(decorator);
}

return result;
}
}
}
12 changes: 1 addition & 11 deletions src/KissLog/LogListenerParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public virtual void AlterDataBeforePersisting(FlushLogArgs args)
"video/"
};

public virtual bool ShouldLog(BeginRequestArgs args)
public virtual bool ShouldLog(BeginRequestArgs args, ILogListener logListener)
{
if (args.Request == null)
return true;
Expand Down Expand Up @@ -88,16 +88,6 @@ public virtual bool ShouldLog(FlushLogArgs args, ILogListener logListener)
}
}

string localPath = args.WebProperties.Request.Url?.LocalPath.ToLowerInvariant();
if (string.IsNullOrEmpty(localPath) == false)
{
if (UrlsToIgnore?.Any() == true)
{
if (UrlsToIgnore.Any(p => localPath.Contains(p.ToLowerInvariant())))
return false;
}
}

return true;
}

Expand Down
3 changes: 3 additions & 0 deletions src/KissLog/Web/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace KissLog.Web
{
public class HttpRequest
{
internal string _KissLogRequestId { get; set; }

public Uri Url { get; set; }

public string UserAgent { get; set; }
Expand Down Expand Up @@ -31,6 +33,7 @@ public class HttpRequest
public HttpRequest()
{
Properties = new RequestProperties();
_KissLogRequestId = Guid.NewGuid().ToString();
}
}
}

0 comments on commit 4671608

Please sign in to comment.