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

User-defined log stream prefix (issue #83) #84

Merged
merged 1 commit into from
Jun 27, 2019
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
5 changes: 5 additions & 0 deletions src/AWS.Logger.AspNetCore/AWSLoggerConfigSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class AWSLoggerConfigSection
internal const string LOG_LEVEL = "LogLevel";
internal const string MAX_QUEUED_MESSAGES = "MaxQueuedMessages";
internal const string LOG_STREAM_NAME_SUFFIX = "LogStreamNameSuffix";
internal const string LOG_STREAM_NAME_PREFIX = "LogStreamNamePrefix";
internal const string LIBRARY_LOG_FILE_NAME = "LibraryLogFileName";
internal const string INCLUDE_SCOPES_NAME = "IncludeScopes";

Expand Down Expand Up @@ -96,6 +97,10 @@ public AWSLoggerConfigSection(IConfiguration loggerConfigSection)
{
Config.LogStreamNameSuffix = loggerConfigSection[LOG_STREAM_NAME_SUFFIX];
}
if (loggerConfigSection[LOG_STREAM_NAME_PREFIX] != null)
{
Config.LogStreamNamePrefix = loggerConfigSection[LOG_STREAM_NAME_PREFIX];
}
if (loggerConfigSection[LIBRARY_LOG_FILE_NAME] != null)
{
Config.LibraryLogFileName = loggerConfigSection[LIBRARY_LOG_FILE_NAME];
Expand Down
12 changes: 11 additions & 1 deletion src/AWS.Logger.Core/AWSLoggerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,23 @@ public AWSLoggerConfig(string logGroup)
/// <summary>
/// Gets and sets the LogStreamNameSuffix property. The LogStreamName consists of a DateTimeStamp as the prefix and a user defined suffix value that can
normj marked this conversation as resolved.
Show resolved Hide resolved
/// be set using the LogStreamNameSuffix property defined here.
/// The LogstreamName then follows the pattern '[DateTime.Now.ToString("yyyy/MM/ddTHH.mm.ss")]-[LogstreamNameSuffix]'
/// The LogstreamName then follows the pattern '[LogStreamNamePrefix]-[DateTime.Now.ToString("yyyy/MM/ddTHH.mm.ss")]-[LogStreamNameSuffix]'
/// <para>
/// The default is going to a Guid.
/// </para>
/// </summary>
public string LogStreamNameSuffix { get; set; } = Guid.NewGuid().ToString();

/// <summary>
/// Gets and sets the LogStreamNamePrefix property. The LogStreamName consists of an optional user-defined LogStreamNamePrefix (that can be set here)
/// followed by a DateTimeStamp as the prefix, and a user defined suffix value
/// The LogstreamName then follows the pattern '[LogStreamNamePrefix]-[DateTime.Now.ToString("yyyy/MM/ddTHH.mm.ss")]-[LogStreamNameSuffix]'
/// <para>
/// The default is an empty string.
/// </para>
/// </summary>
public string LogStreamNamePrefix { get; set; } = string.Empty;

/// <summary>
/// Gets and sets the LibraryLogFileName property. This is the name of the file into which errors from the AWS.Logger.Core library will be wriiten into.
/// <para>
Expand Down
18 changes: 17 additions & 1 deletion src/AWS.Logger.Core/Core/AWSLoggerCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ private async Task<string> LogEventTransmissionSetup(CancellationToken token)
}
}

var currentStreamName = DateTime.Now.ToString("yyyy/MM/ddTHH.mm.ss") + " - " + _config.LogStreamNameSuffix;
var currentStreamName = GenerateStreamName();

var streamResponse = await _client.CreateLogStreamAsync(new CreateLogStreamRequest
{
Expand All @@ -461,6 +461,22 @@ private async Task<string> LogEventTransmissionSetup(CancellationToken token)
return currentStreamName;
}

/// <summary>
/// generate a logstream name
normj marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <returns>logstream name that ALWAYS includes a unique date-based segment</returns>
private string GenerateStreamName()
{
var suffix = _config.LogStreamNameSuffix;
var prefix = _config.LogStreamNamePrefix ?? string.Empty;
if (!string.IsNullOrEmpty(prefix))
{
prefix += " - "; //if there WAS a user-specified prefix, let's use it, followed by a separator
}

return prefix + DateTime.Now.ToString("yyyy/MM/ddTHH.mm.ss") + " - " + suffix;
}

private static bool IsSuccessStatusCode(AmazonWebServiceResponse serviceResponse)
{
return (int)serviceResponse.HttpStatusCode >= 200 && (int)serviceResponse.HttpStatusCode <= 299;
Expand Down
18 changes: 16 additions & 2 deletions src/AWS.Logger.Log4net/AWSAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public int MaxQueuedMessages
}

/// <summary>
/// Gets and sets the LogStreamNameSuffix property. The LogStreamName consists of a DateTimeStamp as the prefix and a user defined suffix value that can
/// be set using the LogStreamNameSuffix property defined here.
/// Gets and sets the LogStreamNameSuffix property. The LogStreamName consists of an optional user-defined prefix segment, then a DateTimeStamp as the
/// system-defined prefix segment, and a user defined suffix value that can be set using the LogStreamNameSuffix property defined here.
/// <para>
/// The default is going to a Guid.
/// </para>
Expand All @@ -140,6 +140,19 @@ public string LogStreamNameSuffix
set { _config.LogStreamNameSuffix = value; }
}

/// <summary>
/// Gets and sets the LogStreamNamePrefix property. The LogStreamName consists of an optional user-defined prefix segment (defined here), then a
/// DateTimeStamp as the system-defined prefix segment, and a user defined suffix value that can be set using the LogStreamNameSuffix property.
/// <para>
/// The default will use an empty string for this user-defined portion, meaning the log stream name will start with the system-defined portion of the prefix (yyyy/MM/dd ... )
/// </para>
/// </summary>
public string LogStreamNamePrefix
{
get { return _config.LogStreamNamePrefix; }
set { _config.LogStreamNamePrefix = value; }
}

/// <summary>
/// Gets and sets the LibraryLogFileName property. This is the name of the file into which errors from the AWS.Logger.Core library will be wriiten into.
/// <para>
Expand Down Expand Up @@ -173,6 +186,7 @@ public override void ActivateOptions()
BatchSizeInBytes = BatchSizeInBytes,
MaxQueuedMessages = MaxQueuedMessages,
LogStreamNameSuffix = LogStreamNameSuffix,
LogStreamNamePrefix = LogStreamNamePrefix,
LibraryLogFileName = LibraryLogFileName
};
_core = new AWSLoggerCore(config, "Log4net");
Expand Down
5 changes: 5 additions & 0 deletions src/AWS.Logger.SeriLog/AWSLoggerSeriLogExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class AWSLoggerSeriLogExtension
internal const string BATCH_PUSH_SIZE_IN_BYTES = "Serilog:BatchPushSizeInBytes";
internal const string MAX_QUEUED_MESSAGES = "Serilog:MaxQueuedMessages";
internal const string LOG_STREAM_NAME_SUFFIX = "Serilog:LogStreamNameSuffix";
internal const string LOG_STREAM_NAME_PREFIX = "Serilog:LogStreamNamePrefix";
internal const string LIBRARY_LOG_FILE_NAME = "Serilog:LibraryLogFileName";

/// <summary>
Expand Down Expand Up @@ -58,6 +59,10 @@ public static LoggerConfiguration AWSSeriLog(
{
config.LogStreamNameSuffix = configuration[LOG_STREAM_NAME_SUFFIX];
}
if (configuration[LOG_STREAM_NAME_PREFIX] != null)
{
config.LogStreamNamePrefix = configuration[LOG_STREAM_NAME_PREFIX];
}
if (configuration[LIBRARY_LOG_FILE_NAME] != null)
{
config.LibraryLogFileName = configuration[LIBRARY_LOG_FILE_NAME];
Expand Down
18 changes: 16 additions & 2 deletions src/NLog.AWS.Logger/AWSTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ public int MaxQueuedMessages
}

/// <summary>
/// Gets and sets the LogStreamNameSuffix property. The LogStreamName consists of a DateTimeStamp as the prefix and a user defined suffix value that can
/// be set using the LogStreamNameSuffix property defined here.
/// Gets and sets the LogStreamNameSuffix property. The LogStreamName consists of an optional user-defined prefix segment, then a DateTimeStamp as the
/// system-defined prefix segment, and a user defined suffix value that can be set using the LogStreamNameSuffix property defined here.
/// <para>
/// The default is going to a Guid.
/// </para>
Expand All @@ -144,6 +144,19 @@ public string LogStreamNameSuffix
set { _config.LogStreamNameSuffix = value; }
}

/// <summary>
/// Gets and sets the LogStreamNamePrefix property. The LogStreamName consists of an optional user-defined prefix segment (defined here), then a
/// DateTimeStamp as the system-defined prefix segment, and a user defined suffix value that can be set using the LogStreamNameSuffix property.
/// <para>
/// The default will use an empty string for this user-defined portion, meaning the log stream name will start with the system-defined portion of the prefix (yyyy/MM/dd ... )
/// </para>
/// </summary>
public string LogStreamNamePrefix
{
get { return _config.LogStreamNamePrefix; }
set { _config.LogStreamNamePrefix = value; }
}

/// <summary>
/// Gets and sets the LibraryLogFileName property. This is the name of the file into which errors from the AWS.Logger.Core library will be wriiten into.
/// <para>
Expand Down Expand Up @@ -175,6 +188,7 @@ protected override void InitializeTarget()
BatchSizeInBytes = BatchSizeInBytes,
MaxQueuedMessages = MaxQueuedMessages,
LogStreamNameSuffix = RenderSimpleLayout(LogStreamNameSuffix, nameof(LogStreamNameSuffix)),
LogStreamNamePrefix = RenderSimpleLayout(LogStreamNamePrefix, nameof(LogStreamNamePrefix)),
LibraryLogFileName = LibraryLogFileName
};
_core = new AWSLoggerCore(config, "NLog");
Expand Down
1 change: 1 addition & 0 deletions test/AWS.Logger.AspNetCore.Tests/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"LogGroup": "AWSILogger",
"Region": "us-west-2",
"LogStreamNameSuffix": "Custom",
"LogStreamNamePrefix": "CustomPrefix",
"LogLevel": {
"Default": "Debug",
"System": "Information",
Expand Down
7 changes: 4 additions & 3 deletions test/AWS.Logger.Log4Net.Tests/log4net.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
<LogGroup>AWSLog4NetGroupLog4Net</LogGroup>
<Region>us-west-2</Region>
<LogStreamNameSuffix>Custom</LogStreamNameSuffix>
<layout type="log4net.Layout.PatternLayout">
<LogStreamNamePrefix>CustomPrefix</LogStreamNamePrefix>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>

<!-- Set root logger level to DEBUG and its only appender to A1 -->

<root>
<level value ="DEBUG"/>
<appender-ref ref="AWS" />
</root>

</log4net>
2 changes: 1 addition & 1 deletion test/AWS.Logger.NLog.Tests/Regular.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<add assembly ="NLog.AWS.Logger"/>
</extensions>
<targets>
<target name="AWSNLogGroup" type="AWSTarget" logGroup="AWSNLogGroup" region="us-west-2" logStreamNameSuffix="Custom"/>
<target name="AWSNLogGroup" type="AWSTarget" logGroup="AWSNLogGroup" region="us-west-2" logStreamNameSuffix="Custom" logStreamNamePrefix="CustomPrefix"/>
<target name="loggerRegular" xsi:type="Console" layout="${callsite} ${message}" />
</targets>
<rules>
Expand Down
1 change: 1 addition & 0 deletions test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroup.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"LogGroup": "AWSSeriLogGroup",
"Region": "us-west-2",
"LogStreamNameSuffix": "Custom",
"LogStreamNamePrefix": "CustomPrefix",
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
}
}
4 changes: 4 additions & 0 deletions test/AWS.Logger.TestUtils/BaseTestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public abstract class BaseTestClass : IClassFixture<TestFixture>
public const int THREAD_COUNT = 2;
public const string LASTMESSAGE = "LASTMESSAGE";
public const string CUSTOMSTREAMSUFFIX = "Custom";
public const string CUSTOMSTREAMPREFIX = "CustomPrefix";
public TestFixture _testFixture;
public AmazonCloudWatchLogsClient Client;

Expand Down Expand Up @@ -105,12 +106,15 @@ protected void SimpleLoggingTest(string logGroupName)

var customStreamSuffix = describeLogstreamsResponse.LogStreams[0].LogStreamName.Split('-').Last().Trim();
Assert.Equal(CUSTOMSTREAMSUFFIX, customStreamSuffix);
var customStreamPrefix = describeLogstreamsResponse.LogStreams[0].LogStreamName.Split('-').First().Trim();
Assert.Equal(CUSTOMSTREAMPREFIX, customStreamPrefix);
}
Assert.Equal(SIMPLELOGTEST_COUNT, getLogEventsResponse.Events.Count());


_testFixture.LogGroupNameList.Add(logGroupName);
}


protected void MultiThreadTestGroup(string logGroupName)
{
Expand Down