diff --git a/.vs/NLog.Extensions/v15/sqlite3/storage.ide b/.vs/NLog.Extensions/v15/sqlite3/storage.ide new file mode 100644 index 0000000..be4f435 Binary files /dev/null and b/.vs/NLog.Extensions/v15/sqlite3/storage.ide differ diff --git a/NLog.Extensions.AzureTableStorage.Tests/AzureTableStorageKeysTests.cs b/NLog.Extensions.AzureTableStorage.Tests/AzureTableStorageKeysTests.cs index 150635b..9ad0bed 100644 --- a/NLog.Extensions.AzureTableStorage.Tests/AzureTableStorageKeysTests.cs +++ b/NLog.Extensions.AzureTableStorage.Tests/AzureTableStorageKeysTests.cs @@ -11,19 +11,25 @@ namespace NLog.Extensions.AzureTableStorage.Tests public class AzureTableStorageKeysTests : IDisposable { #region Constants + //must match table name in AzureTableStorage target in NLog.config - private const string TargetTableName = "AzureTableStorageKeysTests"; + private const string TargetTableName = "AzureTableStorageKeysTests"; + #endregion Constants #region Fields + private readonly Logger _logger; private readonly CloudTable _cloudTable; + #endregion Fields #region Properties + #endregion Properties #region Constructors + public AzureTableStorageKeysTests() { try @@ -42,14 +48,67 @@ public AzureTableStorageKeysTests() throw new Exception("Failed to initialize tests, make sure Azure Storage Emulator is running.", ex); } } + #endregion Constructors #region Methods + + [Fact] + public void IncludeGdcInPatitionKey() + { + var gdcValue = Guid.NewGuid().ToString().Replace("-", string.Empty); + GlobalDiagnosticsContext.Set("item", gdcValue); + + // configure keys + var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName); + target.RowKey = "${gdc:item}__${guid}"; + LogManager.ReconfigExistingLoggers(); + + + // log something + _logger.Log(LogLevel.Info, "information"); + + var entity = GetLogEntities().First(); + var key = entity.RowKey.Split("__".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + Assert.Equal(2, key.Length); + Assert.Equal(gdcValue, key[0]); + } + + [Fact] + public void IncludeMultipleGdcInPatitionKey() + { + var gdcVariables = new Dictionary + { + {"item1", Guid.NewGuid().ToString().Replace("-", string.Empty)}, + {"item2", Guid.NewGuid().ToString().Replace("-", string.Empty)} + }; + + foreach (var pair in gdcVariables) + { + GlobalDiagnosticsContext.Set(pair.Key, pair.Value); + } + + // configure keys + var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName); + target.RowKey = "${gdc:item1}__${gdc:item2}__${guid}"; + LogManager.ReconfigExistingLoggers(); + + + // log something + _logger.Log(LogLevel.Info, "information"); + + var entity = GetLogEntities().First(); + var key = entity.RowKey.Split("__".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + Assert.Equal(3, key.Length); + Assert.True(gdcVariables.ContainsValue(key[0])); + Assert.True(gdcVariables.ContainsValue(key[1])); + } + [Fact] public void IncludeDateInRowKey() { // configure keys - var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName); + var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName); target.RowKey = "${date}__${guid}"; LogManager.ReconfigExistingLoggers(); @@ -68,7 +127,7 @@ public void IncludeDateInRowKey() public void IncludeTimeAndGuidInRowKey() { // configure keys - var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName); + var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName); target.RowKey = "${time}__${guid}"; LogManager.ReconfigExistingLoggers(); @@ -87,7 +146,7 @@ public void IncludeTimeAndGuidInRowKey() public void IncludeTicksAndLongDateInRowKey() { // configure keys - var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName); + var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName); target.RowKey = "${ticks}__${longdate}"; LogManager.ReconfigExistingLoggers(); @@ -106,7 +165,7 @@ public void IncludeTicksAndLongDateInRowKey() public void IncludeMicrosInRowKey() { // configure keys - var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName); + var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName); target.RowKey = "${micros}__${guid}"; LogManager.ReconfigExistingLoggers(); @@ -124,7 +183,7 @@ public void IncludeMicrosInRowKey() public void IncludeMachineInRowKey() { // configure keys - var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName); + var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName); target.RowKey = "${machine}__${guid}"; LogManager.ReconfigExistingLoggers(); @@ -142,7 +201,7 @@ public void IncludeMachineInRowKey() public void IncludeDescendingTicksInRowKey() { // configure keys - var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName); + var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName); target.RowKey = "${descticks}__${guid}"; LogManager.ReconfigExistingLoggers(); @@ -160,7 +219,7 @@ public void IncludeDescendingTicksInRowKey() public void IncludeLevelInRowKey() { // configure keys - var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName); + var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName); target.RowKey = "${level}__${guid}"; LogManager.ReconfigExistingLoggers(); @@ -178,7 +237,7 @@ public void IncludeLevelInRowKey() public void IncludeLevelUppercaseInRowKey() { // configure keys - var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName); + var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName); target.RowKey = "${level:uppercase=true}__${guid}"; LogManager.ReconfigExistingLoggers(); @@ -215,6 +274,7 @@ public void Dispose() { _cloudTable.DeleteIfExists(); } + #endregion Methods } } diff --git a/NLog.Extensions.AzureTableStorage.Tests/NLog.Extensions.AzureTableStorage.Tests.csproj b/NLog.Extensions.AzureTableStorage.Tests/NLog.Extensions.AzureTableStorage.Tests.csproj index 8934750..28d7a16 100644 --- a/NLog.Extensions.AzureTableStorage.Tests/NLog.Extensions.AzureTableStorage.Tests.csproj +++ b/NLog.Extensions.AzureTableStorage.Tests/NLog.Extensions.AzureTableStorage.Tests.csproj @@ -58,8 +58,8 @@ ..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll - - ..\packages\NLog.3.2.0.0\lib\net45\NLog.dll + + ..\packages\NLog.4.4.12\lib\net45\NLog.dll diff --git a/NLog.Extensions.AzureTableStorage.Tests/packages.config b/NLog.Extensions.AzureTableStorage.Tests/packages.config index 25571dc..5dacf95 100644 --- a/NLog.Extensions.AzureTableStorage.Tests/packages.config +++ b/NLog.Extensions.AzureTableStorage.Tests/packages.config @@ -5,7 +5,7 @@ - + diff --git a/NLog.Extensions.AzureTableStorage/LogEntity.cs b/NLog.Extensions.AzureTableStorage/LogEntity.cs index 4b0c543..9878a3f 100644 --- a/NLog.Extensions.AzureTableStorage/LogEntity.cs +++ b/NLog.Extensions.AzureTableStorage/LogEntity.cs @@ -1,8 +1,8 @@ using System; using System.Collections; using System.Globalization; -using System.Net.NetworkInformation; using System.Text; +using System.Text.RegularExpressions; using Microsoft.WindowsAzure.Storage.Table; namespace NLog.Extensions.AzureTableStorage @@ -13,14 +13,18 @@ namespace NLog.Extensions.AzureTableStorage public class LogEntity : TableEntity { #region Constants + #endregion Constants #region Fields + private readonly object _syncRoot = new object(); private static readonly CompareInfo InvariantCompareInfo = CultureInfo.InvariantCulture.CompareInfo; + #endregion Fields #region Properties + public string LogTimeStamp { get; set; } public string Level { get; set; } @@ -40,9 +44,11 @@ public class LogEntity : TableEntity public string ExceptionData { get; set; } public string MachineName { get; set; } + #endregion Properties #region Constructors + public LogEntity() { } @@ -81,12 +87,14 @@ public LogEntity(string partitionKey, string rowKey, LogEventInfo logEvent, stri RowKey = TransformKey(rowKey, logEvent); } } + #endregion Constructors #region Methods + private string TransformKey(string key, LogEventInfo logEvent) { - var capacity = key.Length * 3; // Guesstimate of a reasonable maximum length after transform + var capacity = key.Length * 3; // Guesstimate of a reasonable maximum length after transform var builder = new StringBuilder(key, capacity); var date = logEvent.TimeStamp.ToUniversalTime(); @@ -117,6 +125,16 @@ private string TransformKey(string key, LogEventInfo logEvent) if (InvariantCompareInfo.IndexOf(key, "${machine}", CompareOptions.Ordinal) >= 0) builder.Replace("${machine}", MachineName); + var gdcRegEx = new Regex(@"\$\{(gdc:)(\w{1,})\}", RegexOptions.Compiled); + foreach (Match match in gdcRegEx.Matches(key)) + { + var gdcItem = GlobalDiagnosticsContext.Get(match.Groups[2].Value); + if (!string.IsNullOrWhiteSpace(gdcItem)) + { + builder.Replace(match.Groups[0].Value, gdcItem); + } + } + var result = builder.ToString(); return result; } @@ -130,6 +148,7 @@ private static string GetExceptionDataAsString(Exception exception) } return data.ToString(); } + #endregion Methods } } diff --git a/NLog.Extensions.AzureTableStorage/NLog.Extensions.AzureTableStorage.csproj b/NLog.Extensions.AzureTableStorage/NLog.Extensions.AzureTableStorage.csproj index 5301c37..8089eeb 100644 --- a/NLog.Extensions.AzureTableStorage/NLog.Extensions.AzureTableStorage.csproj +++ b/NLog.Extensions.AzureTableStorage/NLog.Extensions.AzureTableStorage.csproj @@ -56,9 +56,8 @@ False ..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll - - False - ..\packages\NLog.3.2.0.0\lib\net45\NLog.dll + + ..\packages\NLog.4.4.12\lib\net45\NLog.dll diff --git a/NLog.Extensions.AzureTableStorage/Properties/AssemblyInfo.cs b/NLog.Extensions.AzureTableStorage/Properties/AssemblyInfo.cs index 54bfb97..e070654 100644 --- a/NLog.Extensions.AzureTableStorage/Properties/AssemblyInfo.cs +++ b/NLog.Extensions.AzureTableStorage/Properties/AssemblyInfo.cs @@ -32,6 +32,9 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.1.4.0")] -[assembly: AssemblyFileVersion("1.1.4.0")] +[assembly: AssemblyVersion("1.1.5.0")] +[assembly: AssemblyFileVersion("1.1.5.0")] [assembly: NeutralResourcesLanguage("en")] + +// actually used for nuget +[assembly: AssemblyInformationalVersion("1.1.5.0")] diff --git a/NLog.Extensions.AzureTableStorage/packages.config b/NLog.Extensions.AzureTableStorage/packages.config index 5c54981..7eeb126 100644 --- a/NLog.Extensions.AzureTableStorage/packages.config +++ b/NLog.Extensions.AzureTableStorage/packages.config @@ -5,7 +5,7 @@ - + \ No newline at end of file diff --git a/README.md b/README.md index e4bd348..af5e2f8 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ This package allows you to use the following macros to format your partition and - **${level}** will be replaced by the log level. Also supports ${level:uppercase=true}. - **${machine}** will be replaced by the value of Environment.Machine. - **${descticks}** will be replaced by the number of ticks *remaining* till DateTime.MaxValue. +- **${gdc:[keyName]} will be replaced by value from GlobalDiagnosticsContext by keyName Configuring the Target Dynamically ==================================