Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for scopes #85

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion SumoLogic.Logging.AspNetCore/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
return;
}
var line = $"{formatter(state, exception)}";
provider.WriteLine(line, categoryName);
provider.WriteLine(line, categoryName, logLevel);
}
}
}
14 changes: 14 additions & 0 deletions SumoLogic.Logging.AspNetCore/LoggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,19 @@ public string Uri
/// </summary>
public ILogger DebuggingLogger { get; set; }

/// <summary>
/// Gets or sets a flag to indicate whether to include scopes from <c>System.Extensions.Logging.IExternalScopeProvider</c>.
/// </summary>
public bool IncludeScopes { get; set; }

/// <summary>
/// Gets or sets the separator to use when appending multiple scope values to a log message.
/// </summary>
public string ScopeSeparator { get; set; } = " => ";

/// <summary>
/// Gets or sets a flag to indicate whether to include the category provided to the log request.
/// </summary>
public bool IncludeCategory { get; set; }
}
}
53 changes: 45 additions & 8 deletions SumoLogic.Logging.AspNetCore/LoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using SumoLogic.Logging.Common.Queue;
using SumoLogic.Logging.Common.Sender;
using System;
using System.Text;
using System.Threading;

namespace SumoLogic.Logging.AspNetCore
Expand All @@ -37,7 +38,7 @@ namespace SumoLogic.Logging.AspNetCore
/// Sumo Logic Logger Provider implementation
/// </summary>
[ProviderAlias("SumoLogic")]
public class LoggerProvider : ILoggerProvider
public class LoggerProvider : ILoggerProvider, ISupportExternalScope
{
public LoggerOptions LoggerOptions { get; private set; }

Expand All @@ -50,6 +51,12 @@ public class LoggerProvider : ILoggerProvider
private SumoLogicMessageSenderBufferFlushingTask flushBufferTask = null;

private volatile BufferWithEviction<string> messagesQueue = null;
private bool includeScopes;
private bool includeCategory;
private string scopeSeparator;
private IExternalScopeProvider scopeProvider;

private IExternalScopeProvider ScopeProvider => includeScopes ? scopeProvider : null;

public LoggerProvider(IOptionsMonitor<LoggerOptions> options)
{
Expand Down Expand Up @@ -90,7 +97,7 @@ public void Dispose()
/// </summary>
/// <param name="message">the message line to be sent</param>
/// <param name="categoryName">not used for now</param>
public void WriteLine(String message, String categoryName)
public void WriteLine(string message, string categoryName, LogLevel logLevel)
{
if (null == message)
{
Expand All @@ -103,17 +110,40 @@ public void WriteLine(String message, String categoryName)
return;
}

String line = string.Concat(
message.TrimEnd(Environment.NewLine.ToCharArray()),
Environment.NewLine);
var builder = new StringBuilder();
if (includeCategory)
{
builder.Append(" [");
builder.Append(logLevel.ToString());
builder.Append("] ");
builder.Append(categoryName);
}

var scopeProvider = ScopeProvider;
if (scopeProvider != null)
{
scopeProvider.ForEachScope((scope, stringBuilder) =>
{
stringBuilder.Append(this.scopeSeparator).Append(scope);
}, builder);

builder.Append(this.scopeSeparator);
}
else if (includeCategory)
{
builder.Append(this.scopeSeparator);
}


builder.AppendLine(message.Trim());
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we changing the default logic from

String line = string.Concat(
                message.TrimEnd(Environment.NewLine.ToCharArray()),
                Environment.NewLine);

to just
message.Trim()?

Copy link
Author

Choose a reason for hiding this comment

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

I'm trying to remember my exact reason - it's been a while :)

The changes were based off of the ConsoleLogger implementation of scopes:
https://github.com/aspnet/Extensions/blob/master/src/Logging/Logging.Console/src/ConsoleLogger.cs

Specifically, they replace all new lines in message, not just the beginning and end. I know I encountered a need for this during testing, but I can't remember the case anymore.

What are your concerns in regards to trimming the start?

Copy link
Contributor

Choose a reason for hiding this comment

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

I am unsure on why the initial code was written like that. There might have been a reason too! :) So I am just worried about touching something which we are not completely aware of. And if we can't justify this change right now, I'll prefer to revert this change. We can obviously add this change later on in some other PR, if needed.


if (LoggerOptions.IsBuffered)
{
messagesQueue.Add(line);
messagesQueue.Add(builder.ToString());
}
else
{
WriteLineToSumo(line);
WriteLineToSumo(builder.ToString());
}
}

Expand All @@ -130,6 +160,9 @@ private void ReConfig(LoggerOptions options)
{
InitBuffer(options);
}
includeScopes = options.IncludeScopes;
includeCategory = options.IncludeCategory;
scopeSeparator = options.ScopeSeparator;
LoggerOptions = options;
}

Expand Down Expand Up @@ -167,7 +200,7 @@ private void InitBuffer(LoggerOptions options)
flushBufferTimer = new Timer(
callback: _ => flushBufferTask.Run(), // No task await to avoid unhandled exception
state: null,
dueTime: TimeSpan.FromMilliseconds(0),
dueTime: TimeSpan.FromMilliseconds(0),
period: options.FlushingAccuracy);

DebuggingLogger?.Debug("InitBuffer::Completed");
Expand All @@ -184,5 +217,9 @@ private void WriteLineToSumo(String body)
.GetResult();
}

void ISupportExternalScope.SetScopeProvider(IExternalScopeProvider scopeProvider)
{
this.scopeProvider = scopeProvider;
}
}
}