-
Notifications
You must be signed in to change notification settings - Fork 41
Adding support for scopes #85
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
d1b999f
50323db
0588fde
0591684
1a0a63e
d276c0e
23dd3ed
5f5a943
fa2164d
a366e8c
b4eef14
c03c7ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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; } | ||
|
|
||
|
|
@@ -50,6 +51,11 @@ public class LoggerProvider : ILoggerProvider | |
| private SumoLogicMessageSenderBufferFlushingTask flushBufferTask = null; | ||
|
|
||
| private volatile BufferWithEviction<string> messagesQueue = null; | ||
| private bool includeScopes; | ||
| private bool includeCategory; | ||
| private IExternalScopeProvider scopeProvider; | ||
|
|
||
| internal IExternalScopeProvider ScopeProvider => includeScopes ? scopeProvider : null; | ||
|
|
||
| public LoggerProvider(IOptionsMonitor<LoggerOptions> options) | ||
| { | ||
|
|
@@ -80,7 +86,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) | ||
| { | ||
|
|
@@ -93,17 +99,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(" => ").Append(scope); | ||
| }, builder); | ||
|
|
||
| builder.AppendLine(":"); | ||
| } | ||
| else if(includeCategory) | ||
spasarto marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| builder.Append(": "); | ||
| } | ||
|
|
||
|
|
||
| builder.AppendLine(message.Trim()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we changing the default logic from to just
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -120,6 +149,8 @@ private void ReConfig(LoggerOptions options) | |
| { | ||
| InitBuffer(options); | ||
| } | ||
| includeScopes = options.IncludeScopes; | ||
| includeCategory = options.IncludeCategory; | ||
| LoggerOptions = options; | ||
| } | ||
|
|
||
|
|
@@ -174,5 +205,9 @@ private void WriteLineToSumo(String body) | |
| .GetResult(); | ||
| } | ||
|
|
||
| void ISupportExternalScope.SetScopeProvider(IExternalScopeProvider scopeProvider) | ||
| { | ||
| this.scopeProvider = scopeProvider; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.