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

Remove logger name in partition key through configuration #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void IncludePartitionKeyDatePrefix()
{
_logger.Log(LogLevel.Trace, "this entity's partition key should be prefixed with a date");
var entity = GetLogEntities().Single();
Assert.True(entity.PartitionKey.StartsWith(DateTime.Now.ToString("yyyy-MM-dd")));
Assert.True(entity.PartitionKey.StartsWith(DateTime.UtcNow.ToString("yyyy-MM-dd")));
}

[Fact]
Expand Down Expand Up @@ -156,7 +156,23 @@ public void IncludeGuidAndTimeComponentInRowKey()
Assert.True(long.TryParse(segments[0], out timeComponent));
}

[Fact]
public void ShouldNotRemoveLoggerNameIfNoPrefixIsDefinedEvenWhenItSetToTrue()
{
var exception = new NullReferenceException();
_logger.Log(LogLevel.Trace, "LoggerName", (Exception)exception);
var entity = GetLogEntities().Single();
Assert.Equal(GetType().ToString(), entity.PartitionKey);
}

[Fact]
public void ShouldRemoveLoggerNameIfPrefixIsDefinedAndItSetToTrue()
{
var exception = new NullReferenceException();
_logger.Log(LogLevel.Trace, "PrefixOnly", (Exception)exception);
var entity = GetLogEntities().Single();
Assert.Equal(DateTime.UtcNow.ToString("yyyy-MM-dd"), entity.PartitionKey);
}

private string GetStorageAccountConnectionString()
{
Expand Down
26 changes: 24 additions & 2 deletions NLog.Extensions.AzureTableStorage.Tests/NLog.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,34 @@
</extensions>
<targets>
<target name="AzureTableStorage" xsi:type="AzureTableStorage" PartitionKeyPrefix="customPrefix" connectionStringKey="StorageAccountConnectionString" tableName="TempAzureTableStorageTargetTestsLogs" />
<target name="AzureTableStorage2" xsi:type="AzureTableStorage" connectionStringKey="StorageAccountConnectionString"

<target name="AzureTableStorage2" xsi:type="AzureTableStorage" connectionStringKey="StorageAccountConnectionString"
tableName="TempAzureTableStorageTargetTestsLogs" PartitionKeyPrefixDateFormat="yyyy-MM-dd" LogTimestampFormatString="yyyy-MM-dd hh:mm:ss.000"/>

<target name="AzureTableStorage3" xsi:type="AzureTableStorage" connectionStringKey="StorageAccountConnectionString" RemoveLoggerName="true"
tableName="TempAzureTableStorageTargetTestsLogs" LogTimestampFormatString="yyyy-MM-dd hh:mm:ss.000"/>

<target name="AzureTableStorage4" xsi:type="AzureTableStorage" connectionStringKey="StorageAccountConnectionString" RemoveLoggerName="true"
tableName="TempAzureTableStorageTargetTestsLogs" PartitionKeyPrefixDateFormat="yyyy-MM-dd" LogTimestampFormatString="yyyy-MM-dd hh:mm:ss.000"/>

</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="AzureTableStorage" />
<logger name="*" minlevel="Trace" maxlevel="Trace" writeTo="AzureTableStorage2" />
<logger name="*" minlevel="Trace" maxlevel="Trace" writeTo="AzureTableStorage2">
<filters>
<when condition="contains('${message}', 'LoggerName')" action="Ignore" />
<when condition="contains('${message}', 'PrefixOnly')" action="Ignore" />
</filters>
</logger>
<logger name="*" minlevel="Trace" maxlevel="Trace" writeTo="AzureTableStorage3">
<filters>
<when condition="not contains('${message}', 'LoggerName')" action="Ignore" />
</filters>
</logger>
<logger name="*" minlevel="Trace" maxlevel="Trace" writeTo="AzureTableStorage4">
<filters>
<when condition="not contains('${message}', 'PrefixOnly')" action="Ignore" />
</filters>
</logger>
</rules>
</nlog>
6 changes: 3 additions & 3 deletions NLog.Extensions.AzureTableStorage/AzureTableStorageTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class AzureTableStorageTarget : TargetWithLayout
public string PartitionKeyPrefix { get; set; }
public string PartitionKeyPrefixKey { get; set; }
public string PartitionKeyPrefixDateFormat { get; set; }

public bool RemoveLoggerName { get; set; }
public string LogTimestampFormatString { get; set; }

protected override void InitializeTarget()
Expand Down Expand Up @@ -66,11 +66,11 @@ protected override void Write(LogEventInfo logEvent)

if (string.IsNullOrEmpty(LogTimestampFormatString))
{
_tableStorageManager.Add(new LogEntity(PartitionKeyPrefix, logEvent, layoutMessage));
_tableStorageManager.Add(new LogEntity(PartitionKeyPrefix, logEvent, layoutMessage, RemoveLoggerName));
}
else
{
_tableStorageManager.Add(new LogEntity(PartitionKeyPrefix, logEvent, layoutMessage, LogTimestampFormatString));
_tableStorageManager.Add(new LogEntity(PartitionKeyPrefix, logEvent, layoutMessage, RemoveLoggerName, LogTimestampFormatString));
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions NLog.Extensions.AzureTableStorage/LogEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class LogEntity : TableEntity
{
private readonly object _syncRoot = new object();

public LogEntity(string partitionKeyPrefix, LogEventInfo logEvent, string layoutMessage, string timestampFormatString = "g")
public LogEntity(string partitionKeyPrefix, LogEventInfo logEvent, string layoutMessage, bool removeLoggerName, string timestampFormatString = "g")
{
lock (_syncRoot)
{
Expand All @@ -36,8 +36,9 @@ public LogEntity(string partitionKeyPrefix, LogEventInfo logEvent, string layout
}
RowKey = String.Format("{0}__{1}", (DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks).ToString("d19"), Guid.NewGuid());
PartitionKey = !string.IsNullOrWhiteSpace(partitionKeyPrefix)
? partitionKeyPrefix + "." + LoggerName
? removeLoggerName ? partitionKeyPrefix : partitionKeyPrefix + "." + LoggerName
: LoggerName;

MachineName = Environment.MachineName;
}

Expand Down