Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Libraries/src/Amazon.Lambda.Logging.AspNetCore/LambdaILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
}

// Format of the logged text, optional components are in {}
// {[LogLevel] }{Category: }MessageText{\n}
// {[LogLevel] }{Category: }{EventId: }MessageText {Exception}{\n}

var components = new List<string>(4);
if (_options.IncludeLogLevel)
Expand All @@ -56,11 +56,19 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
{
components.Add($"{_categoryName}:");
}
if (_options.IncludeEventId)
{
components.Add($"{eventId}:");
}

var text = formatter.Invoke(state, exception);
var text = formatter.Invoke(state, exception);
components.Add(text);

if (_options.IncludeNewline)
if(_options.IncludeException)
{
components.Add($"{exception}");
}
if (_options.IncludeNewline)
{
components.Add(Environment.NewLine);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class LambdaLoggerOptions
private const string INCLUDE_LOG_LEVEL_KEY = "IncludeLogLevel";
private const string INCLUDE_CATEGORY_KEY = "IncludeCategory";
private const string INCLUDE_NEWLINE_KEY = "IncludeNewline";
private const string INCLUDE_EXCEPTION_KEY = "IncludeException";
private const string INCLUDE_EVENT_ID_KEY = "IncludeEventId";
private const string LOG_LEVEL_KEY = "LogLevel";

/// <summary>
Expand All @@ -37,6 +39,18 @@ public class LambdaLoggerOptions
/// Default is true.
/// </summary>
public bool IncludeNewline { get; set; }

/// <summary>
/// Flag to indicate if Exception should be part of logged message.
/// Default is false.
/// </summary>
public bool IncludeException { get; set; }

/// <summary>
/// Flag to indicate if EventId should be part of logged message.
/// Default is false.
/// </summary>
public bool IncludeEventId { get; set; }

/// <summary>
/// Function used to filter events based on the log level.
Expand All @@ -54,6 +68,8 @@ public LambdaLoggerOptions()
IncludeCategory = true;
IncludeLogLevel = true;
IncludeNewline = true;
IncludeException = false;
IncludeEventId = false;
Filter = null;
}

Expand Down Expand Up @@ -102,26 +118,32 @@ public LambdaLoggerOptions(IConfiguration configuration, string loggingSectionNa

// Parse settings

string includeCategoryString;
if (TryGetString(loggerConfiguration, INCLUDE_CATEGORY_KEY, out includeCategoryString))
if (TryGetString(loggerConfiguration, INCLUDE_CATEGORY_KEY, out string includeCategoryString))
{
IncludeCategory = bool.Parse(includeCategoryString);
}

string includeLogLevelString;
if (TryGetString(loggerConfiguration, INCLUDE_LOG_LEVEL_KEY, out includeLogLevelString))
if (TryGetString(loggerConfiguration, INCLUDE_LOG_LEVEL_KEY, out string includeLogLevelString))
{
IncludeLogLevel = bool.Parse(includeLogLevelString);
}

if (TryGetString(loggerConfiguration, INCLUDE_EXCEPTION_KEY, out string includeExceptionString))
{
IncludeException = bool.Parse(includeExceptionString);
}

if (TryGetString(loggerConfiguration, INCLUDE_EVENT_ID_KEY, out string includeEventIdString))
{
IncludeEventId = bool.Parse(includeEventIdString);
}

string includeNewlineString;
if (TryGetString(loggerConfiguration, INCLUDE_NEWLINE_KEY, out includeNewlineString))
if (TryGetString(loggerConfiguration, INCLUDE_NEWLINE_KEY, out string includeNewlineString))
{
IncludeNewline = bool.Parse(includeNewlineString);
}

IConfiguration logLevelsSection;
if (TryGetSection(loggerConfiguration, LOG_LEVEL_KEY, out logLevelsSection))
if (TryGetSection(loggerConfiguration, LOG_LEVEL_KEY, out IConfiguration logLevelsSection))
{
Filter = CreateFilter(logLevelsSection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
</PropertyGroup>

<ItemGroup>
<None Update="appsettings.exceptions.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="appsettings.wildcard.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Loading