From ba87be25735dd84dd6b7805a73b3bb771fd6edf0 Mon Sep 17 00:00:00 2001 From: Pablo Vivera Date: Wed, 31 Jan 2018 21:28:26 -0500 Subject: [PATCH] RemoveLoggerName flag added in configuration to able to use only prefix as partition key --- .../AzureTableStorageTargetTests.cs | 18 ++++++++++++- .../NLog.config | 26 +++++++++++++++++-- .../AzureTableStorageTarget.cs | 6 ++--- .../LogEntity.cs | 5 ++-- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/NLog.Extensions.AzureTableStorage.Tests/AzureTableStorageTargetTests.cs b/NLog.Extensions.AzureTableStorage.Tests/AzureTableStorageTargetTests.cs index da4a0a8..2bf6130 100644 --- a/NLog.Extensions.AzureTableStorage.Tests/AzureTableStorageTargetTests.cs +++ b/NLog.Extensions.AzureTableStorage.Tests/AzureTableStorageTargetTests.cs @@ -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] @@ -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() { diff --git a/NLog.Extensions.AzureTableStorage.Tests/NLog.config b/NLog.Extensions.AzureTableStorage.Tests/NLog.config index b273bde..9a161df 100644 --- a/NLog.Extensions.AzureTableStorage.Tests/NLog.config +++ b/NLog.Extensions.AzureTableStorage.Tests/NLog.config @@ -6,12 +6,34 @@ - + + + + - + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/NLog.Extensions.AzureTableStorage/AzureTableStorageTarget.cs b/NLog.Extensions.AzureTableStorage/AzureTableStorageTarget.cs index d895bd6..7f2902e 100644 --- a/NLog.Extensions.AzureTableStorage/AzureTableStorageTarget.cs +++ b/NLog.Extensions.AzureTableStorage/AzureTableStorageTarget.cs @@ -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() @@ -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)); } } } diff --git a/NLog.Extensions.AzureTableStorage/LogEntity.cs b/NLog.Extensions.AzureTableStorage/LogEntity.cs index 8e92676..3ab646f 100644 --- a/NLog.Extensions.AzureTableStorage/LogEntity.cs +++ b/NLog.Extensions.AzureTableStorage/LogEntity.cs @@ -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) { @@ -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; }